library(data.table) library(ggplot2) library(patchwork) library(colorspace)
data_pref_evol = fread("data/results/data_relative_preference_evolution.csv") data_pref_evol = data_pref_evol[wt %in% glue::glue("wt_{seq(2,4)}")] data_pref_evol = data_pref_evol[sim_type != "random"] # set weight levels data_pref_evol$wt = factor( data_pref_evol$wt, levels = c("wt_4", "wt_3", "wt_2") ) labels = c( "wt_4" = "sP", "wt_3" = "sH", "wt_2" = "sN" )
plot_wt_evo <- function(df) { l = split(df, by = "wt") subplots = Map( l, names(l), f = function(le, wt) { this_pal = fcase( wt == "wt_4", "Mako", wt == "wt_3", "Viridis", wt == "wt_2", "Rocket" ) ggplot(le)+ geom_hline( yintercept = 0, size = 0.05, col = "grey" )+ geom_vline( xintercept = c(10, 30, 300, 950), size = 0.05, lty = 2, col = "grey" )+ geom_tile( aes( gen, wt_value, fill = prop ) )+ scale_fill_continuous_sequential( palette = this_pal, limits = c(NA, 0.2), breaks = c(0.01, 0.1, 0.2), labels = scales::percent, na.value = "black" )+ facet_grid( rows = vars(rep), cols = vars(wt), labeller = labeller( rep = function(x) glue::glue("Rep: {x}"), wt = labels ) )+ theme_test( base_size = 6, base_family = "Arial" )+ theme( legend.position = "bottom", legend.key.height = unit(1, "mm"), legend.key.width = unit(3, "mm"), legend.title = element_text( vjust = 1.5 ), strip.background = element_blank() )+ coord_cartesian( ylim = c(-1, 1) )+ labs( x = "Generation", y = "Relative preference", fill = "% Ind." ) } ) wrap_plots(subplots[c("wt_4", "wt_3", "wt_2")]) }
data_pref_evo = split(data_pref_evol, by = "sim_type") invisible( Map( data_pref_evo, names(data_pref_evo), f = function(df, n) { p = plot_wt_evo(df) ggsave( p, height = 3, width = 4, filename = glue::glue( "supplement/figures/fig_rel_pref_evo_sc_{n}.png" ) ) } ) )
{width=99%}
{width=99%}
{width=99%}
\newpage
# load relative preferences data_pref = fread("data/results/data_rel_pref.csv") # data_pref = data_pref[rep == 3,] # split by sim type data_pref = split(data_pref, by = "sim_type")
plot_pref = Map( data_pref, names(data_pref), f = function(df, n) { g = c(10, 30, 300, 950) if(n == "facultative") g = c(10, 30, 950) p = ggplot(df[gen %in% g])+ geom_abline( intercept = c(0, 1, -1), slope = c(0, -1, 1), lty = 2, size = 0.2, col = "grey" )+ geom_jitter( aes(sP, sH, fill = sN), colour = "grey50", shape = 21, # stroke = 0.2, alpha = 0.5, size = 2 )+ scale_fill_continuous_diverging( palette = "Blue-Red 2", rev = T, limits = c(-1, 1), breaks = c(-1, 1), labels = c("Avoid", "Prefer") )+ scale_x_continuous( breaks = c(0, 1), labels = c("Neutral", "Prefer") )+ scale_y_continuous( breaks = c(-1, 1), labels = c("Avoid", "Prefer") )+ coord_cartesian( xlim = c(0, 1), ylim = c(-1, 1) )+ theme_test( base_size = 9 )+ theme( axis.text.y = element_text( hjust = c(0, 1), angle = 90 ), axis.text.x = element_text( hjust = c(0, 1) ), strip.text = element_text( face = "italic" ), strip.background = element_blank(), legend.position = "top", legend.title = element_text(vjust = 1.5), legend.key.height = unit(1, "mm"), legend.key.width = unit(5, "mm") )+ labs( x = "sP: Prey-item preference", y = "sH: Handler preference", fill = "sN: Non-handler preference" ) if (n == "obligate") { p = p + facet_grid( rows = vars(rep, comp_strat), cols = vars(gen), labeller = labeller( compt_strat = c( "forager" = "Foragers", "klept" = "Klept." ), rep = function(x) glue::glue("Rep: {x}"), gen = function(x) sprintf("Gen = %s", x), .multi_line = FALSE ) ) } else { p = p + facet_grid( rows = vars(rep), cols = vars(gen), labeller = labeller( gen = function(x) sprintf("Gen = %s", x), rep = function(x) glue::glue("Rep: {x}") ) ) } p } ) invisible( Map( plot_pref, names(plot_pref), f = function(p, n) { h = 5 w = 7 if(n == "obligate") h = 8.5 ggsave( p, filename = glue::glue( "supplement/figures/fig_rel_pref_corr_{n}.png" ), height = h ) } ) )
{width=70%}
{width=70%}
{width=70%}
\newpage
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.