Skip to content

Commit fb1c7da

Browse files
committed
Last modification to plot for memoire
1 parent e12d98d commit fb1c7da

8 files changed

Lines changed: 114 additions & 55 deletions

File tree

R/13_plotting.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library(ggridges)
33
library(hrbrthemes)
44
library(ggdist)
55
library(ggthemes)
6+
library(RColorBrewer)
67
source("lib/rsq.R")
78
source("lib/plot_functions.R")
89

@@ -20,7 +21,6 @@ fit4 <- readRDS("results/model_outputs/stanfit_model4.RDS") |>
2021
tidybayes::recover_types()
2122

2223
# Parameters ridge plots
23-
2424
alpha_trophic_2 <- alpha_ridge_plot(fit2, dataset = dataset)
2525

2626
alpha_trophic_3 <- alpha_ridge_plot(fit3, dataset = dataset)
@@ -116,22 +116,21 @@ ggsave("figures/oneone_plots.png", plot = plots, dpi = "retina")
116116
# Model2
117117

118118
alpha_bm2 <- alpha_bodymass_plot_lm(fit2, dataset = dataset) + theme(legend.position = "none") +
119-
labs(title = "A)") + ylim(c(-20, 10))
119+
labs(title = expression("Model 2 -" * " " * alpha)) + ylim(c(-20, 10))
120120

121121
# Model 3
122122
alpha_bm3 <- alpha_bodymass_plot(fit3, dataset = dataset) + theme(legend.position = "bottom") +
123-
labs(title = "B)") + ylim(c(-20, 10))
123+
labs(title = expression("Model 3 - " * " " * alpha)) + ylim(c(-20, 10))
124124

125125
ht_bm3 <- ht_bodymass_plot(fit3, dataset = dataset) + theme(legend.position = "none") +
126-
labs(title = "C)") + ylim(c(-20, 10))
127-
126+
labs(title = expression("Model 3 - " * " " * h[j])) + ylim(c(-20, 10))
128127

129128
mylegend <- g_legend(alpha_bm3)
130129

131130
plots <- gridExtra::grid.arrange(alpha_bm2, alpha_bm3 + theme(legend.position = "none"), ht_bm3, bottom = mylegend, nrow = 1, ncol = 3)
132131

133132
ggsave("figures/model2_3_relationship.png", plot = plots, dpi = "retina")
134-
133+
``
135134
# Model 4
136135
alpha_bm4 <- alpha_bodymass_plot(fit4, dataset = dataset) + theme(legend.position = "bottom") + ylim(c(-20,10))
137136

figures/alpha_comparison.png

268 KB
Loading

figures/ht_comparison.png

178 KB
Loading

figures/model2_3_relationship.png

-41.4 KB
Loading

figures/oneone_plots.png

175 KB
Loading

figures/rsq_plot.png

-5.08 KB
Loading

lib/plot_functions.R

Lines changed: 102 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,54 @@ model_number <- gsub("\\D", "", deparse(substitute(model)))
1313

1414
pred_ids <- unique(dataset[, c("pred_id", "habitat_type", "trophic_guild")])
1515

16-
df <- model |> tidybayes::gather_draws(alpha[pred_id]) |>
17-
dplyr::left_join(pred_ids, by = "pred_id") |>
18-
dplyr::mutate(pred_id = as.factor(pred_id),
19-
habitat_type = as.factor(habitat_type),
20-
trophic_guild = as.factor(trophic_guild))
21-
22-
pred_ids <- pred_ids |> dplyr::mutate(pred_id = as.factor(pred_id),
23-
habitat_type = as.factor(habitat_type),
24-
trophic_guild = as.factor(trophic_guild))
25-
26-
pred_ids_guild <- pred_ids |> dplyr::arrange(trophic_guild)
27-
28-
ggplot(df, aes(x = `.value`, y = pred_id, fill = trophic_guild)) +
29-
geom_density_ridges_gradient(scale = 5, rel_min_height = 0.01) +
16+
model |>
17+
tidybayes::gather_draws(alpha[pred_id]) |>
18+
dplyr::left_join(pred_ids, by = "pred_id") |>
19+
dplyr::mutate(pred_id = as.factor(pred_id),
20+
habitat_type = as.factor(habitat_type),
21+
trophic_guild = factor(trophic_guild,
22+
levels = c(
23+
"Small demersal omnivore", "Medium demersal omnivore",
24+
"Small demersal carnivore", "Medium demersal carnivore",
25+
"Small pelagic omnivore", "Medium pelagic omnivore",
26+
"Small pelagic carnivore", "Medium pelagic carnivore",
27+
"Small reef-coast", "Medium reef-coast",
28+
"Sharks", "Cephalopods", "Mollusc and crustacea",
29+
"Shrimps", "Plankton", "Invertebrates",
30+
"Reptiles", "Non-predatory birds", "Predatory birds",
31+
"Small mammal herbivore", "Large mammal herbivore",
32+
"Small mammal predator", "Medium mammal predator",
33+
"Large mammal predator"
34+
)
35+
)
36+
) |>
37+
dplyr::rowwise() |>
38+
dplyr::mutate(
39+
pred_nb = which(trophic_guild == levels(trophic_guild))
40+
) |>
41+
dplyr::ungroup() |>
42+
dplyr::mutate(
43+
pred_id = reorder(pred_id, pred_nb)
44+
) |>
45+
ggplot() +
46+
aes(x = `.value`, y = pred_id, fill = trophic_guild) +
47+
geom_density_ridges(scale = 5, rel_min_height = 0.01, alpha = 0.9) +
48+
scale_fill_manual(values = c(
49+
"Small demersal omnivore" = "aquamarine4", "Medium demersal omnivore" = "aquamarine2",
50+
"Small demersal carnivore" = "deepskyblue4", "Medium demersal carnivore" = "deepskyblue2",
51+
"Small pelagic omnivore" = "mediumpurple4", "Medium pelagic omnivore" = "mediumpurple2",
52+
"Small pelagic carnivore" = "palevioletred4", "Medium pelagic carnivore" = "palevioletred2",
53+
"Small reef-coast" = "lightsalmon3", "Medium reef-coast" = "lightsalmon1",
54+
"Sharks" = "ivory3", "Cephalopods" = "greenyellow", "Mollusc and crustacea" = "lightseagreen",
55+
"Shrimps" = "powderblue" , "Plankton" = "brown", "Invertebrates" = "violetred",
56+
"Reptiles" = "palegoldenrod", "Non-predatory birds" = "yellow4", "Predatory birds" = "yellow2",
57+
"Small mammal herbivore" = "mistyrose4", "Large mammal herbivore" = "mistyrose2",
58+
"Small mammal predator" = "goldenrod4", "Medium mammal predator" = "goldenrod3",
59+
"Large mammal predator" = "goldenrod1")) +
3060
theme_hc() +
3161
theme(
3262
legend.position = "bottom",
33-
legend.text = element_text(size = 9),
63+
legend.text = element_text(size = 10),
3464
legend.title = element_text(size = 12),
3565
plot.title = element_text(size = 15),
3666
axis.title.y = element_blank(),
@@ -39,8 +69,7 @@ pred_ids_guild <- pred_ids |> dplyr::arrange(trophic_guild)
3969
axis.text.x = element_text(size = 15),
4070
) +
4171
labs(title = paste0("Model ", model_number), fill = "Trophic guilds") +
42-
xlim(c(-20, 10)) +
43-
scale_y_discrete(guide = guide_axis(n.dodge = 2), limits = pred_ids_guild$pred_id)
72+
xlim(c(-20.5, 10))
4473
}
4574

4675
ht_ridge_plot <- function(model, dataset) {
@@ -49,24 +78,54 @@ model_number <- gsub("\\D", "", deparse(substitute(model)))
4978

5079
pred_ids <- unique(dataset[, c("pred_id", "habitat_type", "trophic_guild")])
5180

52-
df <- model |> tidybayes::gather_draws(ht[pred_id]) |>
53-
dplyr::left_join(pred_ids, by = "pred_id") |>
54-
dplyr::mutate(pred_id = as.factor(pred_id),
55-
habitat_type = as.factor(habitat_type),
56-
trophic_guild = as.factor(trophic_guild))
57-
58-
pred_ids <- pred_ids |> dplyr::mutate(pred_id = as.factor(pred_id),
59-
habitat_type = as.factor(habitat_type),
60-
trophic_guild = as.factor(trophic_guild))
61-
62-
pred_ids_guild <- pred_ids |> dplyr::arrange(trophic_guild)
63-
64-
ggplot(df, aes(x = `.value`, y = pred_id, fill = trophic_guild)) +
65-
geom_density_ridges_gradient(scale = 5, rel_min_height = 0.01) +
81+
model |>
82+
tidybayes::gather_draws(ht[pred_id]) |>
83+
dplyr::left_join(pred_ids, by = "pred_id") |>
84+
dplyr::mutate(pred_id = as.factor(pred_id),
85+
habitat_type = as.factor(habitat_type),
86+
trophic_guild = factor(trophic_guild,
87+
levels = c(
88+
"Small demersal omnivore", "Medium demersal omnivore",
89+
"Small demersal carnivore", "Medium demersal carnivore",
90+
"Small pelagic omnivore", "Medium pelagic omnivore",
91+
"Small pelagic carnivore", "Medium pelagic carnivore",
92+
"Small reef-coast", "Medium reef-coast",
93+
"Sharks", "Cephalopods", "Mollusc and crustacea",
94+
"Shrimps", "Plankton", "Invertebrates",
95+
"Reptiles", "Non-predatory birds", "Predatory birds",
96+
"Small mammal herbivore", "Large mammal herbivore",
97+
"Small mammal predator", "Medium mammal predator",
98+
"Large mammal predator"
99+
)
100+
)
101+
) |>
102+
dplyr::rowwise() |>
103+
dplyr::mutate(
104+
pred_nb = which(trophic_guild == levels(trophic_guild))
105+
) |>
106+
dplyr::ungroup() |>
107+
dplyr::mutate(
108+
pred_id = reorder(pred_id, pred_nb)
109+
) |>
110+
ggplot() +
111+
aes(x = `.value`, y = pred_id, fill = trophic_guild) +
112+
geom_density_ridges(scale = 5, rel_min_height = 0.01, alpha = 0.9) +
113+
scale_fill_manual(values = c(
114+
"Small demersal omnivore" = "aquamarine4", "Medium demersal omnivore" = "aquamarine2",
115+
"Small demersal carnivore" = "deepskyblue4", "Medium demersal carnivore" = "deepskyblue2",
116+
"Small pelagic omnivore" = "mediumpurple4", "Medium pelagic omnivore" = "mediumpurple2",
117+
"Small pelagic carnivore" = "palevioletred4", "Medium pelagic carnivore" = "palevioletred2",
118+
"Small reef-coast" = "lightsalmon3", "Medium reef-coast" = "lightsalmon1",
119+
"Sharks" = "ivory3", "Cephalopods" = "greenyellow", "Mollusc and crustacea" = "lightseagreen",
120+
"Shrimps" = "powderblue" , "Plankton" = "brown", "Invertebrates" = "violetred",
121+
"Reptiles" = "palegoldenrod", "Non-predatory birds" = "yellow4", "Predatory birds" = "yellow2",
122+
"Small mammal herbivore" = "mistyrose4", "Large mammal herbivore" = "mistyrose2",
123+
"Small mammal predator" = "goldenrod4", "Medium mammal predator" = "goldenrod3",
124+
"Large mammal predator" = "goldenrod1")) +
66125
theme_hc() +
67126
theme(
68127
legend.position = "bottom",
69-
legend.text = element_text(size = 9),
128+
legend.text = element_text(size = 10),
70129
legend.title = element_text(size = 12),
71130
plot.title = element_text(size = 15),
72131
axis.title.y = element_blank(),
@@ -75,10 +134,7 @@ pred_ids_guild <- pred_ids |> dplyr::arrange(trophic_guild)
75134
axis.text.x = element_text(size = 15),
76135
) +
77136
labs(title = paste0("Model ", model_number), fill = "Trophic guilds") +
78-
xlim(c(-20, 10)) +
79-
scale_y_discrete(guide = guide_axis(n.dodge = 2), limits = pred_ids_guild$pred_id)
80-
81-
137+
xlim(c(-20.5, 10))
82138
}
83139

84140
# One-one plot for simulation and observations
@@ -95,7 +151,7 @@ one_one_plot <- function(model, dataset) {
95151
"Marine & freshwater" = "marine_freshwater")
96152

97153
df |> ggplot(aes(x= log(biomass_flow), dist=.value, col=habitat_type)) +
98-
stat_dist_pointinterval(alpha=0.8) +
154+
stat_pointinterval(point_interval = "mean_qi", alpha=0.8) +
99155
theme_minimal() +
100156
theme(
101157
legend.position = "bottom",
@@ -107,7 +163,7 @@ df |> ggplot(aes(x= log(biomass_flow), dist=.value, col=habitat_type)) +
107163
axis.text.x = element_text(size = 15),
108164
axis.text.y = element_text(size = 15)
109165
) +
110-
xlab("Observed") +
166+
xlab("Observed ") +
111167
ylab("Predicted") +
112168
ylim(c(-20, 20)) +
113169
scale_colour_manual(values=c("Terrestrial"="olivedrab",
@@ -129,7 +185,7 @@ plot_sim_noerror <- function(model, dataset) {
129185
"Marine & freshwater" = "marine_freshwater")
130186

131187
df |> ggplot(aes(x= log(biomass_prey) + log(abundance_predator), dist=.value, col="Model predictions")) +
132-
stat_dist_pointinterval() +
188+
stat_pointinterval() +
133189
geom_point(aes(x=log(biomass_prey) + log(abundance_predator),
134190
y=log(biomass_flow), col=habitat_type), inherit.aes=FALSE, size=3, alpha=0.8) +
135191
theme_minimal() +
@@ -164,7 +220,7 @@ plot_sim_error <- function(model, dataset) {
164220
"Marine & freshwater" = "marine_freshwater")
165221

166222
df |> ggplot(aes(x= log(biomass_prey) + log(abundance_predator), dist=.value, col="Model predictions")) +
167-
stat_dist_pointinterval() +
223+
stat_pointinterval() +
168224
geom_point(aes(x=log(biomass_prey) + log(abundance_predator),
169225
y=log(biomass_flow), col=habitat_type), inherit.aes=FALSE, size=3, alpha=0.8) +
170226
theme_minimal() +
@@ -200,7 +256,7 @@ parameter_bodymass[which(parameter_bodymass$habitat_type == "marine_freshwater")
200256

201257
parameter_bodymass |>
202258
ggplot(aes(x = log(bodymass_mean_predator), dist = .value, col = factor(habitat_type))) +
203-
stat_pointinterval() +
259+
stat_pointinterval(point_interval = "mean_qi") +
204260
geom_smooth(aes(x = log(bodymass_mean_predator), y = mean_alpha), method = "lm", color = "black") +
205261
theme_minimal() +
206262
theme(
@@ -215,7 +271,7 @@ parameter_bodymass |>
215271
) +
216272
ylim(-19,6) +
217273
xlim(-19,1) +
218-
xlab("Body mass (g, log-scale)") +
274+
xlab("Body mass (metric tons, log-scale)") +
219275
ylab("Space clearance rate (km²/ind*year, log-scale)") +
220276
scale_colour_manual(values=c("Terrestrial"="olivedrab",
221277
"Freshwater" = "sandybrown", "Marine"="deepskyblue3", "Marine & freshwater"="purple"),
@@ -237,7 +293,7 @@ parameter_bodymass[which(parameter_bodymass$habitat_type == "marine_freshwater")
237293

238294
parameter_bodymass |>
239295
ggplot(aes(x = log(bodymass_mean_predator), dist = .value, col = factor(habitat_type))) +
240-
stat_pointinterval() +
296+
stat_pointinterval(point_interval = "mean_qi") +
241297
theme_minimal() +
242298
theme(
243299
legend.position = "bottom",
@@ -251,7 +307,7 @@ parameter_bodymass |>
251307
) +
252308
ylim(-19,6) +
253309
xlim(-19,1) +
254-
xlab("Body mass (g, log-scale)") +
310+
xlab("Body mass (metric tons, log-scale)") +
255311
ylab("Space clearance rate (km²/ind*year, log-scale)") +
256312
scale_colour_manual(values=c("Terrestrial"="olivedrab",
257313
"Freshwater" = "sandybrown", "Marine"="deepskyblue3", "Marine & freshwater"="purple"),
@@ -273,7 +329,7 @@ parameter_bodymass[which(parameter_bodymass$habitat_type == "marine_freshwater")
273329

274330
parameter_bodymass |>
275331
ggplot(aes(x = log(bodymass_mean_predator), dist = .value, col = habitat_type)) +
276-
stat_dist_pointinterval() +
332+
stat_pointinterval(point_interval = "mean_qi") +
277333
theme_minimal() +
278334
theme(
279335
legend.position = "bottom",
@@ -287,7 +343,7 @@ parameter_bodymass |>
287343
) +
288344
ylim(-10,6) +
289345
xlim(-17,1) +
290-
xlab("Body mass (g, log-scale)") +
346+
xlab("Body mass (metric tons, log-scale)") +
291347
ylab("Handling time (year*km²/tons, log-scale)") +
292348
scale_colour_manual(values=c("Terrestrial"="olivedrab",
293349
"Freshwater" = "sandybrown", "Marine"="deepskyblue3", "Marine & freshwater"="purple"),

lib/rsq.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ plot_rsq <- function(list_model) {
2323

2424
df <- do.call(cbind, df_list)
2525

26-
rsq_plot <- bayesplot::mcmc_areas(df, area_method = "equal height")# +
27-
#ggplot2::xlim(0, 1)
26+
rsq_plot <- bayesplot::mcmc_areas(df, area_method = "equal height") +
27+
theme(
28+
axis.text = element_text(size = 15),
29+
axis.text.x = element_text(size = 15),
30+
axis.text.y = element_text(size = 15)
31+
)
2832

2933
return(rsq_plot)
30-
}
34+
}

0 commit comments

Comments
 (0)