# data
library(readr) # reading CSV files
library(here) # constructing file paths
library(ggeffects) # extracting model predictions
library(tibble) # creating tibbles (data frames)
library(dplyr) # data manipulation
# plots
library(scales) # scale transformations and labels
library(ggplot2) # data visualization
library(patchwork) # combining plots
09 - Results from generalized linear models (Figure 4)
09_V_model_Fig4.qmd
Model predictions and figures in the main text
Loading packages, data and functions
Note
The scales
package needs the version 1.3 @420 to work
Reading model results for counting, DR, DC and native and NBT turnover
# Data from 03_C_data_preparation.qmd
<- readr::read_csv(here::here("data", "processed", "df_country_complete6.csv"))
df_country_complete6
# Data from 04_D_model_NBTs.qmd
<- readRDS(here::here("output",
mod_counting_NBT "models",
"model_res_counting.rds")) # NBT total countings
<- readRDS(here::here("output",
mod_DC "models",
"model_res_dc.rds"))
<- readRDS(here::here("output",
mod_DR "models",
"model_res_dr.rds"))
<- readRDS(here::here("output",
mod_turnover_native "models",
"model_res_turnover_native.rds"))
<- readRDS(here::here("output",
mod_turnover_nbt "models",
"model_res_turnover_nbt.rds"))
# function
<- scales::trans_new("sqrt_with_zero",
sqrt_trans transform = function(x) ifelse(x == 0, 0, sqrt(x)),
inverse = function(x) x^2)
Total NBT counting
# Extracting predicted values
<-
pred_mod_counting_gdp ::ggpredict(mod_counting_NBT, "gdp.pad[-0.88:4 by=.05]")
ggeffects
<-
pred_mod_counting_richness ::ggpredict(mod_counting_NBT, "native.richness.pad[-0.4:8.6 by=.05]")
ggeffects
# scaling back response variables
source(here::here("R", "functions", "function_scale_back.R"))
## GDP
<- scale(df_country_complete6$e_migdppc)
scale_gdp
<- scale_back(data = pred_mod_counting_gdp, scaled = scale_gdp)
pred_mod_counting_gdp2
## Native Richness
<- scale(df_country_complete6$native_richness)
scale_native_richness
<- scale_back(data = pred_mod_counting_richness, scaled = scale_native_richness)
pred_mod_counting_richness2
# plotting figure 4a
<-
a tibble(
x= df_country_complete6$e_migdppc,
y = df_country_complete6$total_country_museum,
country = df_country_complete6$country,
region = df_country_complete6$region
|>
) ggplot(aes(x=x,y=y))+
geom_ribbon(data = pred_mod_counting_gdp2,
aes(x = x_original,
y = predicted,
ymin = conf.low,
ymax = conf.high),
alpha = 0.2)+
geom_line(data = pred_mod_counting_gdp2,
aes(x = x_original,
y = predicted))+
geom_point(aes(fill = region),
shape = 21)+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,40000,10000),
labels = scales::label_number(scale_cut = scales::cut_short_scale()
+
))scale_y_continuous(trans = sqrt_trans,
expand = expansion(mult = c(0,0)),
breaks = c(0,100,1000,5000),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
+
)guides(
y = guide_axis_logticks()
+
)scale_fill_manual(
values = c(
"Europe & Central Asia" = "#E64B35FF",
"East Asia & Pacific" = "#4DBBD5FF",
"North America" = "#3C5488FF",
"South Asia" = "#B09C85FF",
"Latin America & Caribbean" = "#00A087FF",
"Sub-Saharan Africa" = "#F39B7FFF",
"Middle East & North Africa" = "#8491B4FF"
)+
)labs(x = "Gross Domestic Product - GDP",
y = "Number of NBT") +
theme_classic()+
theme(
axis.title = element_text(face = "bold"),
axis.text = element_text(color = "black"),
axis.line = element_blank(),
axis.ticks.x = element_blank(),
panel.grid.major = element_line(linetype = "dashed"),
legend.position = "none"
+
)coord_cartesian(clip = "off",
xlim = c(0,40000))
# plotting figure 4b
<- tibble(
b x= df_country_complete6$native_richness,
y = df_country_complete6$total_country_museum,
country = df_country_complete6$country,
region = df_country_complete6$region
|>
) ggplot(aes(x=x,y=y))+
geom_ribbon(data = pred_mod_counting_richness2,
aes(x = x_original,
y = predicted,
ymin = conf.low,
ymax = ifelse(conf.high >= 20000, 20000, conf.high)),
alpha = 0.2)+
geom_line(data = pred_mod_counting_richness2,
aes(x = x_original,
y = predicted))+
annotate(
"segment",
x = 2317.744, xend = Inf,
y = Inf, yend = Inf,
linetype = "dotted"
+
)geom_point(aes(fill = region),
shape = 21)+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,3000,1000),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
+
)scale_y_continuous(trans = sqrt_trans,
expand = expansion(mult = c(0,0)),
breaks = c(0,1000,10000,100000,300000),
labels = scales::label_number(scale_cut = scales::cut_short_scale()))+
guides(
y = guide_axis_logticks()
+
)scale_fill_manual(
values = c(
"Europe & Central Asia" = "#E64B35FF",
"East Asia & Pacific" = "#4DBBD5FF",
"North America" = "#3C5488FF",
"South Asia" = "#B09C85FF",
"Latin America & Caribbean" = "#00A087FF",
"Sub-Saharan Africa" = "#F39B7FFF",
"Middle East & North Africa" = "#8491B4FF"
)+
)labs(x = "Native richness",
y = "Number of NBT") +
theme_classic()+
theme(
axis.title = element_text(face = "bold"),
axis.text = element_text(color = "black"),
axis.line = element_blank(),
axis.ticks.x = element_blank(),
panel.grid.major = element_line(linetype = "dashed"),
legend.position = "none"
+
)coord_cartesian(clip = "off",
xlim = c(-100,NA),
ylim = c(NA, 20000))
Domestic Contribution
# predicted values
<-
pred_mod_dc ::ggpredict(mod_DC, "gdp.pad[-0.88:4 by=.05]")
ggeffects
<- scale_back(data = pred_mod_dc, scaled = scale_gdp)
pred_mod_dc_gdp2
# plotting figure 4c
<- tibble(
c x = df_country_complete6$e_migdppc,
y = df_country_complete6$prop_DC,
country = df_country_complete6$country,
region = df_country_complete6$region
|>
) ggplot(aes(x=x,y=y))+
geom_ribbon(data = pred_mod_dc_gdp2,
aes(x = x_original,
y = predicted,
ymin = conf.low,
ymax = conf.high),
alpha = 0.2)+
geom_line(data = pred_mod_dc_gdp2,
aes(x = x_original,
y = predicted))+
geom_point(aes(fill = region),
shape = 21)+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,40000,10000),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
+
) scale_y_continuous(expand = expansion(mult = c(0)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()))+
scale_fill_manual(
values = c(
"Europe & Central Asia" = "#E64B35FF",
"East Asia & Pacific" = "#4DBBD5FF",
"North America" = "#3C5488FF",
"South Asia" = "#B09C85FF",
"Latin America & Caribbean" = "#00A087FF",
"Sub-Saharan Africa" = "#F39B7FFF",
"Middle East & North Africa" = "#8491B4FF"
)+
)labs(x = "Gross Domestic Product - GDP",
y = "Domestic Contribution") +
theme_classic()+
theme(
axis.title = element_text(face = "bold"),
axis.text = element_text(color = "black"),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_line(linetype = "dashed",
lineend = "round"),
legend.position = "none"
+
)coord_cartesian(clip = "off",
xlim = c(0,40000),
ylim = c(0,1))
Domestic Retention
# predicted values
<-
pred_mod_dr ::ggpredict(mod_DR, "gdp.pad[-0.88:4 by=.05]")
ggeffects
<- scale_back(data = pred_mod_dr, scaled = scale_gdp)
pred_mod_dr_gdp2
# plotting figure 4d
<- tibble(
d x = df_country_complete6$e_migdppc,
y = df_country_complete6$prop_DR,
country = df_country_complete6$country,
region = df_country_complete6$region
|>
) ggplot(aes(x=x,y=y))+
geom_ribbon(data = pred_mod_dr_gdp2,
aes(x = x_original,
y = predicted,
ymin = conf.low,
ymax = conf.high),
alpha = 0.2)+
geom_line(data = pred_mod_dr_gdp2,
aes(x = x_original,
y = predicted))+
geom_point(aes(fill = region),
shape = 21)+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,40000,10000),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
+
) scale_y_continuous(expand = expansion(mult = c(0)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()))+
scale_fill_manual(
values = c(
"Europe & Central Asia" = "#E64B35FF",
"East Asia & Pacific" = "#4DBBD5FF",
"North America" = "#3C5488FF",
"South Asia" = "#B09C85FF",
"Latin America & Caribbean" = "#00A087FF",
"Sub-Saharan Africa" = "#F39B7FFF",
"Middle East & North Africa" = "#8491B4FF"
)+
)labs(x = "Gross Domestic Product - GDP",
y = "Domestic Retention") +
theme_classic()+
theme(
axis.title = element_text(face = "bold"),
axis.text = element_text(color = "black"),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_line(linetype = "dashed",
lineend = "round"),
legend.position = "none"
+
)coord_cartesian(clip = "off",
xlim = c(0,40000),
ylim = c(0,1))
Native turnover
<-
pred_mod_beta_native ::ggpredict(mod_turnover_native, "gdp.pad[-0.88:4 by=.05]")
ggeffects
<- scale_back(data = pred_mod_beta_native, scaled = scale_gdp)
pred_mod_beta_native2
# plotting figure 4e
<- tibble(
e x = df_country_complete6$e_migdppc,
y = df_country_complete6$native.beta.model,
country = df_country_complete6$country,
region = df_country_complete6$region
|>
) ggplot(aes(x=x,y=y))+
geom_ribbon(data = pred_mod_beta_native2,
aes(x = x_original,
y = predicted,
ymin = conf.low,
ymax = conf.high),
alpha = 0.2)+
geom_line(data = pred_mod_beta_native2,
aes(x = x_original,
y = predicted))+
geom_point(aes(fill = region),
shape = 21)+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,40000,10000),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
+
)scale_y_continuous(expand = expansion(mult = c(0,0)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()))+
scale_fill_manual(
values = c(
"Europe & Central Asia" = "#E64B35FF",
"East Asia & Pacific" = "#4DBBD5FF",
"North America" = "#3C5488FF",
"South Asia" = "#B09C85FF",
"Latin America & Caribbean" = "#00A087FF",
"Sub-Saharan Africa" = "#F39B7FFF",
"Middle East & North Africa" = "#8491B4FF"
)+
)labs(x = "Gross Domestic Product - GDP",
y = "Native turnover") +
theme_classic()+
theme(
axis.title = element_text(face = "bold"),
axis.text = element_text(color = "black"),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_line(linetype = "dashed",
lineend = "round"),
legend.position = "none"
+
)coord_cartesian(clip = "off",
xlim = c(0,40000),
ylim = c(0,1))
NBT turnover
# predictions
<-
pred_mod_beta_type ::ggpredict(mod_turnover_nbt, "gdp.pad[-0.88:4 by=.05]")
ggeffects
<- scale_back(data = pred_mod_beta_type, scaled = scale_gdp)
pred_mod_beta_type2
# plotting figure 4f
<- tibble(
f x = df_country_complete6$e_migdppc,
y = df_country_complete6$type.beta.model,
country = df_country_complete6$country,
region = df_country_complete6$region
|>
) ggplot(aes(x=x,y=y))+
geom_ribbon(data = pred_mod_beta_type2,
aes(x = x_original,
y = predicted,
ymin = conf.low,
ymax = conf.high),
alpha = 0.2)+
geom_line(data = pred_mod_beta_type2,
aes(x = x_original,
y = predicted))+
geom_point(aes(fill = region),
shape = 21)+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,40000,10000),
labels = scales::label_number(scale_cut = scales::cut_short_scale())) +
scale_y_continuous(expand = expansion(mult = c(0)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()))+
scale_fill_manual(
values = c(
"Europe & Central Asia" = "#E64B35FF",
"East Asia & Pacific" = "#4DBBD5FF",
"North America" = "#3C5488FF",
"South Asia" = "#B09C85FF",
"Latin America & Caribbean" = "#00A087FF",
"Sub-Saharan Africa" = "#F39B7FFF",
"Middle East & North Africa" = "#8491B4FF"
)+
)labs(x = "Gross Domestic Product - GDP",
y = "NBT turnover") +
theme_classic()+
theme(
axis.title = element_text(face = "bold"),
axis.text = element_text(color = "black"),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_line(linetype = "dashed",
lineend = "round"),
legend.position = "none"
+
)coord_cartesian(clip = "off",
xlim = c(0,40000),
ylim = c(0,1))
Joining all plots
<-
fig_model +b+c+d+e+f+
a::plot_annotation(tag_levels = "a")+
patchwork::plot_layout(nrow = 3)&
patchworktheme(
plot.margin = margin(5.5,8,5.5,5.5,"pt")
) fig_model
# saving model figure
ggsave(here::here("output", "figures", "Fig4_models.png"),
width = 6, height = 8,
dpi = 600, plot = fig_model)