library(data.table) library(ggplot2) library(patchwork) library(colorspace)
files <- list.files( "data/results/morph_data", pattern = "vertical", full.names = TRUE ) data_all <- lapply(files, fread) data_all <- rbindlist(data_all) # make a copy df_strat <- copy(data_all) # popsize popsize <- 500 df_strat[, prop := N / popsize] # get time since pathogen sgen <- 3000 genmax <- 3500 df_strat[, gen_abs := gen - sgen] # save for plotting and upload fwrite( df_strat, file = "data/results/data_vertical_evo.csv" )
# load saved data df_strat <- fread("data/results/data_vertical_evo.csv")
# prepare figure comparing types of outcomes p_compare <- ggplot(df_strat[gen %% 2 == 0 & (social_strat != "non-handler tracking"), ]) + geom_path( aes( gen_abs, prop, colour = social_strat, group = interaction(replicate, social_strat) ), size = 0.2 ) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + facet_grid( cols = vars(p_v_transmit), rows = vars(costInfect), as.table = F, switch = c("y"), labeller = labeller( cost = function(x) sprintf("δE = %s", x), p_v_transmit = function(x) sprintf("p(V. Trans.) = %s", x) ) ) + scale_colour_discrete_sequential( palette = "Viridis", l2 = 80, rev = FALSE, name = NULL, limits = c("agent avoiding", "agent tracking", "handler tracking"), labels = stringr::str_to_sentence, na.value = "lightgrey" ) + scale_x_continuous( # trans = ggallin::pseudolog10_trans, breaks = c(-500, 0, 500), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen, sgen + 500), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + scale_y_continuous( breaks = c(0, 0.5, 1), labels = scales::percent, name = "% Individuals" ) + coord_cartesian( xlim = c(-500, 500), ylim = c(0, 1), expand = TRUE ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "top", legend.key.height = unit(1, "mm"), legend.key.width = unit(2, "mm"), axis.text.x = element_text(hjust = 0.5), axis.text.y = element_text( angle = 90, hjust = 0.5 ) ) # save for supplement ggsave( p_compare, filename = "supplement/figures/fig_evo_change_vertical.png", height = 120, width = 120, units = "mm" )
# read in infections data df_vertical_infections <- fread("data/results/data_infections_gen.csv")[ scenario_tag == "vertical", ] df_vertical_infections[, gen_abs := gen - sgen] df_vertical_infections[, pathogen_eliminated := any( n_infected[(gen > sgen) & (gen < sgen + 500)] == 0 ), by = c("costInfect", "p_v_transmit", "replicate") ] # infection persistence p_infections <- ggplot(df_vertical_infections) + geom_path( aes(gen_abs, n_infected / popsize, group = replicate, col = pathogen_eliminated ), linewidth = 0.2 ) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + facet_grid( cols = vars(p_v_transmit), rows = vars(costInfect), as.table = F, switch = c("y"), labeller = labeller( cost = function(x) sprintf("δE = %s", x), p_v_transmit = function(x) sprintf("p(V. Trans.) = %s", x) ) ) + scale_colour_manual( values = c( "FALSE" = "tomato", "TRUE" = "lightgrey" ), labels = c( "FALSE" = "Pathogen persists", "TRUE" = "Pathogen eliminated" ), name = NULL ) + scale_x_continuous( breaks = c(0, 250, 500), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen, sgen + 250, sgen + 500), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + scale_y_continuous( breaks = c(0, 0.5, 1), labels = scales::percent, name = "% Individuals" ) + coord_cartesian( xlim = c(-30, 500), ylim = c(0, 1) ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "top", legend.key.height = unit(1, "mm"), legend.key.width = unit(2, "mm"), axis.text.x = element_text(hjust = 0.5), axis.text.y = element_text( angle = 90, hjust = 0.5 ) ) # save for supplement ggsave( p_infections, filename = "supplement/figures/fig_infection_vertical.png", height = 120, width = 120, units = "mm" )
# infections in the default combination p_infections_default <- ggplot( df_vertical_infections[costInfect == 0.25 & p_v_transmit == 0.2, ][order(-pathogen_eliminated)] ) + geom_path( aes(gen_abs, n_infected / popsize, group = replicate, col = pathogen_eliminated ), linewidth = 0.2 ) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + scale_colour_manual( values = c( "FALSE" = "firebrick", "TRUE" = "lightblue" ), labels = c( "FALSE" = "Pathogen persists", "TRUE" = "Pathogen eliminated" ), name = NULL ) + scale_x_continuous( breaks = c(0, 250, 500), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen, sgen + 250, sgen + 500), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + scale_y_continuous( breaks = c(0, 0.5, 1), labels = scales::percent, name = "% Individuals" ) + coord_cartesian( xlim = c(-30, 500), ylim = c(0, 1) ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "top", legend.key.height = unit(1, "mm"), legend.key.width = unit(2, "mm"), axis.text.x = element_text(hjust = 0.5), axis.text.y = element_text( angle = 90, hjust = 0.5 ) )
Pick two focal simulations in the cost = 0.25, p(v transmit) = 0.2 scenario.
df_focus <- df_strat[p_v_transmit == 0.2 & costInfect == 0.25, ] # subset for parameters df_vertical_infections <- df_vertical_infections[p_v_transmit == 0.2 & costInfect == 0.25, ] # get sims where pathogen is eliminated df_vertical_infections[scenario_tag == "vertical", pathogen_eliminated := any( n_infected[(gen > sgen) & (gen < (sgen + 500))] == 0 ), by = c("costInfect", "p_v_transmit", "replicate") ] # select replicates to show in plot df_vertical_infections_focus <- df_vertical_infections[ replicate == first(replicate[pathogen_eliminated]) | replicate == first(replicate[!pathogen_eliminated]) ] df_focus <- df_focus[replicate %in% df_vertical_infections_focus$replicate, ]
p_evo <- ggplot() + geom_col( data = df_focus, aes(gen_abs, prop, fill = social_strat), width = 5 ) + geom_line( data = df_vertical_infections_focus, aes(gen_abs, n_infected / popsize, col = "infected") ) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + facet_grid( cols = vars(replicate), labeller = labeller( replicate = c( "1" = "Pathogen persistance", "2" = "Pathogen eliminated" ) ), switch = c("y") ) + scale_fill_discrete_sequential( palette = "Viridis", l2 = 70, alpha = 1, c1 = 20, c2 = 40, rev = FALSE, name = NULL, limits = c("agent avoiding", "agent tracking", "handler tracking"), labels = stringr::str_to_sentence, na.value = "lightgrey" ) + scale_colour_manual( values = c(infected = "firebrick"), labels = "% Infected", name = NULL ) + scale_x_continuous( breaks = c(-500, 0, 500), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen - 500, sgen, sgen + 250, sgen + 500), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + scale_y_continuous( breaks = c(0, 0.5, 1), labels = scales::percent, name = "% Individuals" ) + coord_cartesian( xlim = c(-100, 510), ylim = c(0, 1), expand = FALSE ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "top", legend.key.height = unit(1, "mm"), legend.key.width = unit(2, "mm"), axis.text.x = element_text(hjust = 0.5), axis.text.y = element_text( angle = 90, hjust = 0.5 ) )
# read in infections data df_vertical_infections <- fread("data/results/data_infections_gen.csv")[ scenario_tag == "vertical", ] df_vertical_infections[, gen_abs := gen - sgen] df_vertical_infections[, pathogen_eliminated := any( n_infected[(gen > sgen) & (gen < sgen + 250)] == 0 ), by = c("costInfect", "p_v_transmit", "replicate") ] # count outcomes df_outcomes <- unique( df_vertical_infections[, c( "costInfect", "p_v_transmit", "pathogen_eliminated", "replicate" )] ) df_outcomes <- df_outcomes[, .N, by = c( "pathogen_eliminated", "costInfect", "p_v_transmit" )] # plot count of replicates with elimination plot_summarise_vertical <- ggplot(df_outcomes) + geom_col( aes(x = 1, y = N / 10, fill = pathogen_eliminated), position = "stack" ) + facet_grid( cols = vars(p_v_transmit), rows = vars(costInfect), as.table = F, switch = c("y"), labeller = labeller( costInfect = function(x) sprintf("δE = %s", x), p_v_transmit = function(x) sprintf("p(vert.) = %s", x) ) ) + scale_fill_discrete_diverging( palette = "Blue-Red 3", rev = TRUE, l1 = 50, l2 = 80, labels = c("Pathogen persists", "Pathogen eliminated"), name = NULL ) + scale_y_continuous( breaks = c(0.5, 1), labels = scales::percent ) + labs( x = NULL, y = "% Replicates" ) + coord_cartesian( expand = FALSE ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "bottom", axis.ticks.x = element_blank(), axis.text.x = element_blank(), legend.justification = "left", legend.key.width = unit(2, "mm"), legend.key.height = unit(1, "mm"), axis.text.y = element_text( angle = 90, hjust = 0.5 ) )
fig_eco_evo_vertical <- wrap_plots( p_evo, plot_summarise_vertical, design = "AAB" ) & plot_annotation( tag_levels = c("A", 1) ) & theme( legend.position = "bottom", plot.tag = element_text( face = "bold", size = 10 ) ) ggsave( fig_eco_evo_vertical, filename = "figures/fig_eco_evo_vertical.png", height = 80, width = 180, units = "mm" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.