-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbRepro_JM.R
121 lines (99 loc) · 4.53 KB
/
AbRepro_JM.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
## load libaries and custom functions
library(ggplot2)
library(dplyr)
library(tidyr)
library(lubridate)
library(reshape2)
source("C:/GitCode/AbResearch/stderr.r")
## load 'stagedat' datframe created by Craig Mundy from original compliction of reproductive data
# either add this script to end of his RMD file or create an RDS file which can be imported to this script.
## add month.yr variable to dataframe
stagedat <- stagedat %>%
mutate(sample_month = lubridate::month(sample_date, label = TRUE, abbr = TRUE),
sample_year = year(sample_date),
month.yr = interaction(sample_month, sample_year))
## summarise the stagedat dataframe for each gonad stage recorded in each month.yr
stagedat.summ <- stagedat %>%
group_by(sample_year, sample_month, month.yr) %>%
summarise_each(funs(mean), pc_rec_dev:pc_necrotic)
## ensure month.yr is a factor
stagedat.summ$month.yr <- as.factor(stagedat.summ$month.yr)
## transform the stagedat summary from wide to long format
stagedat.summ.long <- melt(stagedat.summ, id.vars = c('sample_year', 'sample_month', 'month.yr'))
## plot mean for each gonad stage for month.yr
stagedat.summ.long %>%
ggplot(aes(x = month.yr, y = value, colour = variable, group = variable)) +
geom_point()+
geom_line() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## plot mean for each gonad stage for month and year
stagedat.summ.long %>%
ggplot(aes(x = sample_month, y = value, colour = variable, group = 1)) +
geom_point()+
geom_line() +
facet_grid(variable ~ sample_year) +
ylab('Area of mature gonad (%)') +
xlab('Month') +
theme_bw() +
theme(legend.position = 'none')+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
## plot mean for each gonad stage for month and year with error bars
## select gonad pc stage data and convert to long format
stagedat.1 <- stagedat %>%
select(abalone_id,sample_year, sample_month, month.yr,
pc_rec_dev, pc_locules, pc_mature, pc_spawning, pc_necrotic) %>%
melt(id.vars = c('abalone_id', 'sample_year', 'sample_month', 'month.yr'))
## summarise gonad pc stage data for mean and se
stagedat.summ.long.1 <- stagedat.1 %>%
group_by(sample_year, sample_month, month.yr, variable) %>%
summarise(mean.pc = mean(value),
se.pc = stderr(value))
## plot mean for each gonad stage for month and year including error bars
stagedat.summ.long.1 %>%
ggplot(aes(x = sample_month, y = mean.pc, colour = variable, group = 1)) +
geom_point()+
geom_line() +
geom_errorbar(aes(ymin = mean.pc - se.pc, ymax = mean.pc + se.pc), width = 0.5)+
facet_grid(variable ~ sample_year) +
ylab('Area of mature gonad (%)') +
xlab('Month') +
theme_bw() +
theme(legend.position = 'none')+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
## plot mean pc of each gonad stage in each month (all years)
stagedat.summ.long.1 %>%
ggplot(aes(x = sample_month, y = mean.pc, fill = variable)) +
geom_bar(position = 'fill', stat = 'identity') +
scale_y_continuous(labels = scales::percent_format()) +
ylab('Area of mature gonad (%)') +
xlab('Month')
## plot mean pc of each gonand stage in each yr.month
stagedat.summ.long.1 %>%
ggplot(aes(x = month.yr, y = mean.pc, fill = variable)) +
geom_bar(position = 'fill', stat = 'identity') +
scale_y_continuous(labels = scales::percent_format()) +
ylab('Area of mature gonad (%)') +
xlab('Month') +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
## plot mean pc of vitellogenesis stage in each yr.month
# select vitellegenic data and convert to long format
vitsdat <- stagedat %>%
select(abalone_id,sample_year, sample_month, month.yr,
vits, previts) %>%
melt(id.vars = c('abalone_id', 'sample_year', 'sample_month', 'month.yr'))
# summarise vitellegenic data for mean and se
vitsdat.summ <- vitsdat %>%
filter(!is.na(value)) %>%
group_by(sample_year, sample_month, month.yr, variable) %>%
summarise(mean.vit = mean(value),
se.vit = stderr(value))
vitsdat.summ %>%
ggplot(aes(x = month.yr, y = mean.vit, fill = variable)) +
geom_bar(position = 'fill', stat = 'identity', colour = 'black') +
scale_y_continuous(labels = scales::percent_format()) +
ylab('Vitellogenesis (%)') +
xlab('Month') +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
theme(legend.title = element_blank(), legend.position = 'top') +
scale_fill_manual(values = c('grey', 'white'), labels = c('Vitellogenic', 'Pre-vitellogenic'))