-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fig_2_ab.R
199 lines (181 loc) · 5.37 KB
/
Fig_2_ab.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# run the following line to load libraries and add data.
source('1_DataPackages.R')
# Exploratory plots----
# Absolute weight
fig_e1_ab <-
ggplot(data = absolute_weight) +
geom_hline(yintercept = 0, linetype = 2) +
geom_point(
aes(
x = Treatment,
y = Weight,
group = Palatability,
fill = Palatability
),
size = 0.75,
position = position_jitterdodge(
jitter.width = 0.05,
jitter.height = 0,
dodge.width = 0.75,
seed = NA
)
) +
geom_boxplot(aes(x = Treatment,
y = Weight,
fill = Palatability),
position = position_dodge(width = .80)) +
theme_bw(base_size = 18) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(
t = 0.2,
r = 0.2,
b = 0.2,
l = 0.2,
unit = "cm"
),
plot.title = element_text(size = 18, hjust = 0.5),
strip.background = element_blank(),
legend.position = "none"
) +
ylim(-10, 800) +
ylab("Biomass weight (g)") + theme(legend.direction = "none") +
xlab("") +
labs(title = "Absolute biomass") +
scale_fill_manual(values = c("#f8bb49", '#a45200', "#4A2300")) +
theme(plot.caption = element_text(size = 8, face = "italic",
hjust = 0))
fig_e1_ab
# Analysis----
# ghats.ab_biomass <-
# brm(
# Weight ~ Treatment * Palatability +
# ( 1 | Site ) ,
# family = student(),
# data = absolute_weight,
# iter = 2000,
# warmup = 1000,
# cores = 4,
# chains = 4
# )
# save(ghats.ab_biomass, file = "ghats.ab_biomass.Rdata")
load("ghats.ab_biomass.Rdata")
color_scheme_set("darkgray")
fig_s1ab <- pp_check(ghats.ab_biomass) +
xlab("Functional group absolute biomass") + ylab("Density") +
labs(title = "Site-level",
subtitle = "a)") +
theme_classic() + xlim(-20,150) +
theme(plot.title = element_text(size = 18, hjust = 0.5),
legend.position = "none")# predicted vs. observed values
fig_s1ab
summary(ghats.ab_biomass)
ghats_ab_biomass <-
conditional_effects(
ghats.ab_biomass,
effects = 'Treatment:Palatability',
re_formula = NA,
method = 'fitted'
) # conditional effects
ghats_ab_biomass_df <-
as.data.frame(ghats_ab_biomass$`Treatment:Palatability`)# to make a df to report statistics results
# View(ghats_ab_biomass_df)
# Table_1 ----
# Biomass
TableS2 <- ghats_ab_biomass_df %>%
select(Treatment, Palatability, estimate__, lower__, upper__) %>%
rename(Estimate = estimate__,
Lower = lower__,
Upper = upper__) %>%
mutate_if(is.numeric, round, 2) %>%
gt() %>%
tab_options(column_labels.font.size = 11,
table.font.size = 10,
column_labels.font.weight = "bold") %>%
tab_header(subtitle = '', 'Absolute biomass') %>%
opt_table_font(default_fonts()) %>% # Fonts: Roboto Mono,IBM Plex Mono, Red Hat Mono
opt_table_outline(style = "solid", width = px(2))
TableS2
TableS2 %>% gtsave('Table_S2_ab_biomass.png', expand = 5) # expand to set white space
# Plot----
fig_ab_biomass <- ggplot() +
geom_point(
data = absolute_weight,
aes(
x = Treatment,
y = Weight,
group = Palatability,
colour = Palatability
),
size = 1,
alpha = 0.5,
position = position_jitterdodge(
jitter.width = 0.05,
jitter.height = 0.45,
dodge.width = 0.75,
seed = NA
)
) +
geom_point(
data = ghats_ab_biomass$`Treatment:Palatability`,
aes(
x = Treatment,
y = estimate__,
group = Palatability,
colour = Palatability
),
size = 3,
position = position_dodge(width = 0.75)
) +
geom_errorbar(
data = ghats_ab_biomass$`Treatment:Palatability`,
aes(
x = Treatment,
ymin = lower__,
ymax = upper__,
group = Palatability,
colour = Palatability
),
position = position_dodge(width = 0.75),
linewidth = 1,
width = 0
) + labs(x = '', y = '') +
scale_color_manual(values = c("#f8bb49", '#a45200', "#4A2300")) +
# scale_colour_grey()+
theme_bw(base_size = 12) + theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = margin(
t = 0.2,
r = 0.2,
b = 0.2,
l = 0.2,
unit = "cm"
),
plot.title = element_text(size = 14, hjust =
0.5),
strip.background = element_blank(),
legend.position = "bottom"
) + labs(subtitle = '') + ylab("Absolute biomass (g)") +
theme(
panel.grid.major = element_line(colour = "gray86", linewidth = 0.1),
panel.background = element_rect(fill = "white")
)
fig_ab_biomass + plot_annotation(title = "Absolute biomass",
theme = theme(plot.title = element_text(size = 14, hjust = 0.5)))
# add treatment icons to x axis
treats <- axis_canvas(fig_ab_biomass, axis = 'x') +
cowplot::draw_image('CPFP.png', x = 0.5, scale = 0.5) +
cowplot::draw_image('CPFA.png', x = 1.5, scale = 0.5) +
cowplot::draw_image('CAFA.png', x = 2.5, scale = 0.5)
Fig_2_ab <-
ggdraw(insert_xaxis_grob(fig_ab_biomass, treats, position = "center"))
Fig_2_ab <- Fig_2_ab + plot_annotation(title = "Absolute biomass",
theme = theme(plot.title = element_text(size = 14, hjust = 0.5)))
Fig_2_ab
# Save image (Biomass/Fig_2)
ggsave('Fig_2_ab.jpg',
width = 10,
height = 6,
dpi = 300)