library(data.table) library(ggplot2) library(patchwork) library(colorspace)
df_strat <- fread("data/results/data_default_strategy_evo.csv")
df_strat <- df_strat[, c( "gen", "gen_abs", "social_strat", "mean_move", "mean_assoc", "mean_intake", "mean_energy" )] # cast long df_strat <- melt(df_strat, id.vars = c("gen", "gen_abs", "social_strat")) # remove non-handler tracking df_strat <- df_strat[social_strat != "non-handler tracking"] # log strategy data sgen <- 3000 genmax <- 5000
# plot strategy differences in movement distance plot_diff_move <- ggplot(df_strat[variable %in% c("mean_move") & gen %% 100 == 0]) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + stat_summary( aes(gen_abs, value, col = social_strat), geom = "line", size = 0.2, show.legend = FALSE ) + stat_summary( aes(gen_abs, value, col = social_strat), size = 0.2, shape = 1, show.legend = FALSE ) + scale_x_continuous( breaks = c(-500, 0, 500, 2000), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen, genmax), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + 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" ) + coord_cartesian( xlim = c(-500, 2000) ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "bottom", legend.key = element_blank(), legend.background = element_blank(), strip.background = element_blank(), strip.text = element_text( face = "italic" ), plot.background = element_blank(), strip.placement = "outside" ) + labs( x = NULL, y = "Distance moved", colour = NULL, shape = NULL ) + guides( shape = NULL, colour = guide_legend( override.aes = list( shape = 15 ) ) )
# plot strategy differences in intake plot_diff_intake <- ggplot(df_strat[variable %in% c("mean_intake") & gen %% 100 == 0]) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + stat_summary( aes(gen_abs, value, col = social_strat), geom = "line", size = 0.2, show.legend = FALSE ) + stat_summary( aes(gen_abs, value, col = social_strat), size = 0.2, shape = 2, show.legend = FALSE ) + scale_x_continuous( breaks = c(-500, 0, 500, 2000), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen, genmax), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + 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" ) + coord_cartesian( xlim = c(-500, 2000) ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "bottom", legend.key = element_blank(), legend.background = element_blank(), strip.background = element_blank(), strip.text = element_text( face = "italic" ), plot.background = element_blank(), strip.placement = "outside" ) + labs( x = NULL, y = "Per-capita intake", colour = NULL, shape = NULL ) + guides( shape = NULL, colour = guide_legend( override.aes = list( shape = 15 ) ) )
# plot strategy differences in energy plot_diff_energy <- ggplot(df_strat[variable %in% c("mean_energy") & gen %% 100 == 0]) + geom_vline( xintercept = c(0), lty = 2, size = 0.3, col = c("red") ) + stat_summary( aes(gen_abs, value, col = social_strat), geom = "line", size = 0.2, show.legend = FALSE ) + stat_summary( aes(gen_abs, value, col = social_strat), size = 0.2, shape = 5, show.legend = FALSE ) + scale_x_continuous( breaks = c(-500, 0, 500, 2000), labels = scales::comma_format( accuracy = 1 ), name = "Gens. after pathogen intro.", sec.axis = sec_axis( trans = function(x) x + sgen, breaks = c(sgen, genmax), labels = scales::comma_format( accuracy = 1 ), name = "Generations" ) ) + 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" ) + coord_cartesian( xlim = c(-500, 2000), ylim = c(-6, NA) ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "bottom", legend.key = element_blank(), legend.background = element_blank(), strip.background = element_blank(), strip.text = element_text( face = "italic" ), plot.background = element_blank(), strip.placement = "outside" ) + labs( x = NULL, y = "Per-capita energy", colour = NULL, shape = NULL ) + guides( shape = NULL, colour = guide_legend( override.aes = list( shape = 15 ) ) )
plot_strat_diff <- wrap_plots( plot_diff_move, plot_diff_intake, plot_diff_energy, ncol = 3, guides = "collect" ) & theme( legend.position = "none" )
files <- list.files( path = "data/results/move_assoc_data", pattern = "default", full.names = TRUE ) # read data and filter for default paramaeter combination data_move_assoc <- lapply(files, fread) |> rbindlist() data_move_assoc <- data_move_assoc[between(gen, 3000, 3500), ] data_move_assoc <- data_move_assoc[costInfect == 0.25 & regen_time == 50, ] # round moved data_move_assoc[, moved_bin := plyr::round_any(moved, 5)] data_move_assoc <- data_move_assoc[social_strat != "non-handler tracking", ]
# plot associations per movement distance plot_move_assoc <- ggplot() + geom_jitter( data = data_move_assoc[gen %% 10 == 0, ], aes(moved, mean_assoc, col = social_strat), size = 0.1, alpha = 0.5 ) + geom_vline( xintercept = 50, linewidth = 0.3, lty = "dashed", colour = "indianred" ) + stat_summary( data = data_move_assoc, aes(moved_bin, mean_assoc, fill = social_strat ), shape = 21 ) + annotate( geom = "text", x = 100, y = 750, label = "Post-pathogen\n3000 ≤ G ≤ 3500", hjust = "inward", fontface = "italic", colour = "red", size = 3, family = "Arial" ) + scale_fill_discrete_sequential( palette = "Viridis", l1 = 15, l2 = 80, rev = F, breaks = c("agent avoiding", "handler tracking", "agent tracking"), name = NULL, labels = stringr::str_to_sentence ) + scale_colour_discrete_sequential( palette = "Viridis", c1 = 30, c2 = 40, l1 = 50, l2 = 80, rev = F, name = NULL, labels = stringr::str_to_sentence ) + scale_y_continuous( labels = scales::comma_format() ) + coord_cartesian( ylim = c(30, 1000), expand = T ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( axis.text.y = element_text( angle = 90, hjust = 0.5 ), legend.key.height = unit(1, "mm"), legend.position = "bottom" ) + labs( x = "Distance moved", y = "Encounters w/ individuals" ) + guides( alpha = "none", shape = "none", colour = "none", fill = guide_legend( override.aes = list( alpha = 1 ) ) )
# load data data <- fread("data/results/data_default_strategy_evo.csv") data <- data[between(gen, 3000, 3500), ]
# assign broad strategy data[, social_strat := fifelse( social_strat %in% c("agent avoiding", "handler tracking"), social_strat, "other" )] # assign levels data[, social_strat := factor(social_strat, levels = c( "other", "handler tracking", "agent avoiding" ) )]
# plot infection rate histogram per strategy plot_infected <- ggplot(data) + ggdist::stat_histinterval( aes( social_strat, prop_infec, fill = social_strat, group = social_strat ), size = 0.5, n = 31, show.legend = F ) + scale_fill_discrete_sequential( palette = "Viridis", l2 = 80, order = c(2, 1, 3), name = NULL ) + scale_y_continuous( labels = scales::percent ) + scale_x_discrete( breaks = c( "agent avoiding", "handler tracking", "other" ), limits = c( "agent avoiding", "handler tracking", "other" ) ) + theme_test( base_family = "Arial", base_size = 8 ) + theme( legend.position = "top", legend.key.height = unit(0.5, "mm"), legend.key.width = unit(2, "mm"), axis.text.y = element_text( angle = 90, hjust = 0.5 ), plot.background = element_blank(), axis.text.x = element_blank() ) + labs( x = "Social movement\nstrategy", y = "% Infected" )
# prepare and plot figure for strategy-wise social differences plot_soc_out <- wrap_plots( plot_strat_diff, plot_move_assoc, plot_infected, guides = "collect", design = "AAA\nBBC\nBBC" ) & plot_annotation( tag_levels = c("A", 1) ) & theme( legend.position = "bottom", plot.tag = element_text( face = "bold" ) ) ggsave( plot = plot_soc_out, file = "figures/fig_social_outcomes.png", height = 120, width = 120, units = "mm" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.