Plot population composition and statistical analysis

library(data.table)
library(ggplot2)
library(colorspace)
library(patchwork)
source("R/fun_wt_vals.R")

Load LT weights

# read weight data and scale
data_wt = fread("data/data_gens_dist_summary.csv")[g >= 200]
data_wt[, wt_abs_sum := apply(
  data_wt[, c("wt_2", "wt_3", "wt_4")], 
  1, 
  FUN = function(x) {
    sum(abs(x))
  })
]

data_wt[, c("wt_2", "wt_3", "wt_4") := lapply(
  .SD, `/`, wt_abs_sum
), .SDcols = c("wt_2", "wt_3", "wt_4")]

# assign foraging strategy
data_wt[, comp_strat := dplyr::case_when(
  type == "obligate" & wt_5 < 0 ~ "klept",
  type == "interf" ~ "mixed",
  T ~ "forager"
)]

# movement strategy
# assign strategy
data_wt[, move_strat := dplyr::case_when(
  wt_4 > 0.6 ~ "prey tracking",
  ((wt_4 > 0) & (wt_3 > 0) & (abs(wt_4 - wt_3) < 0.1))  ~ "prey and handler tracking",
  wt_3 > 0.5 ~ "handler tracking",
  wt_3 < -0.5 ~ "handler avoiding",
  wt_2 > 0.5 ~ "non-handler tracking",
  wt_2 < -0.5 ~ "non-handler avoiding",
  T ~ "mixed"
)]

data_wt[, move_strat := factor(
  move_strat, 
  levels = c(
    "prey tracking",
    "handler tracking",
    "prey and handler tracking",
    "non-handler avoiding",
    "handler avoiding",
    "mixed"
  )
)]

# split data
data_wt = split(data_wt, by = "type")
data_wt = data_wt[c("exploit", "obligate")]

Plot strategy evolution

plot_strat_evol = lapply(data_wt, function(df) {
  ggplot(
  df
) +
  geom_bar(
    aes(factor(growth), fill = move_strat)
  )+
  scale_fill_manual(
    values = c(
      "prey tracking" = "seagreen",
      "prey and handler tracking" = "royalblue",
      "non-handler avoiding" = "red",
      "handler avoiding" = "orange",
      "handler tracking" = "lightblue",
      "mixed" = "grey"
    ),
    breaks = c(
      "prey tracking",
      "handler tracking",
      "prey and handler tracking",
      "non-handler avoiding",
      "handler avoiding",
      "mixed"
    ),
    na.value = "grey"
  )+
    scale_y_continuous(
      labels = scales::comma,
      breaks = c(1e3, 3e3, 6e3)
    )+
  theme_test(
    base_size = 8
  )+
  theme(
    strip.background = element_blank(),
    strip.text = element_text(
      face = "italic"
    ),
    axis.title.x = ggtext::element_markdown(),
    axis.text.y = element_text(
      angle = 90,
      hjust = 0.5,
      size = 6
    ),
    legend.position = "top",
    legend.key.height = unit(2, "mm")
  )+
  coord_cartesian(expand = F, ylim = c(0, 6000))+
  facet_grid(
      ~ comp_strat, 
      # ncol = 2, 
      labeller = labeller(
        .multi_line = F,
        growth = label_both,
        comp_strat = c(
          "forager" = "Foragers",
          "klept" = "Kleptoparasites"
        )
      )
    )+
  labs(
    fill = NULL,
    y = "# Agents",
    x = "Landscape productivity *r<sub>max</sub>*"
  )
})

fig_pop_com =
wrap_plots(
  plot_strat_evol, guides = "collect",
  design = "ABB"
) &
  theme(
    legend.position = "bottom"
  )

Load and plot repeatability data

# load data
load("data/data_rpt.Rds")

Scenario 1

# get data
data_sc1 = copy(data_rpt[[1]])

# make figure across growth rates
fig_sc1_rpt = 
  ggplot(data_sc1)+
  geom_pointrange(
    aes(
      factor(growth), rpt_v,
      ymin = rpt_v - rpt_se,
      ymax = rpt_v + rpt_se,
      fill = factor(growth)
    ),
    show.legend = F,
    shape = 21,
    alpha = 0.8,
    position = position_jitter(width = 0.1)
  )+
  scale_fill_manual(
    values = rev(terrain.colors(3))
  )+
  theme_bw(
    base_size = 8
  )+
  theme(
    axis.title.x = ggtext::element_markdown()
  )+
  coord_cartesian(
    ylim = c(0, 1),
    xlim = c(0.75, 3.25),
    expand = F
  )+
  labs(
    x = "*r<sub>max</sub>*",
    y = "Repeatability"
  )

Scenario 2 not accounting for strategy

# get data
data_sc2 = copy(data_rpt[[2]])

# make figure across growth rates
fig_sc2_rpt =
  ggplot(data_sc2)+
  geom_pointrange(
    aes(
      factor(growth), rpt_v,
      ymin = rpt_v - rpt_se,
      ymax = rpt_v + rpt_se,
      fill = factor(growth)
    ),
    show.legend = F,
    shape = 21,
    alpha = 0.8,
    position = position_jitter(width = 0.1)
  )+
  scale_fill_manual(
    values = rev(terrain.colors(3))
  )+
  theme_bw(
    base_size = 8
  )+
  theme(
    axis.title.x = ggtext::element_markdown()
  )+
  coord_cartesian(
    ylim = c(0, 1),
    xlim = c(0.75, 2.25),
    expand = F
  )+
  labs(
    x = "*r<sub>max</sub>*",
    y = "Repeatability"
  )

Scenario 2 accounting for strategy

# get data
data_sc2_strat = copy(data_rpt[[3]])

# make figure across growth rates
fig_sc2_strat =
  ggplot(data_sc2_strat)+
  geom_pointrange(
    aes(
      factor(growth), rpt_v,
      ymin = rpt_v - rpt_se,
      ymax = rpt_v + rpt_se,
      fill = factor(growth)
    ),
    show.legend = F,
    shape = 21,
    alpha = 0.8,
    position = position_jitter(width = 0.1)
  )+
  scale_fill_manual(
    values = rev(terrain.colors(3))
  )+
  theme_bw(
    base_size = 8
  )+
  theme(
    axis.title.x = ggtext::element_markdown()
  )+
  coord_cartesian(
    ylim = c(0, 1),
    xlim = c(0.75, 2.25),
    expand = F
  )+
  labs(
    x = "*r<sub>max</sub>*",
    y = "Repeatability"
  )

Scenario 2 splitting by strategy

# get data
data_sc2_split = copy(data_rpt[[4]])

# make figure across growth rates
fig_sc2_split =
  ggplot(data_sc2_split)+
  geom_pointrange(
    aes(
      factor(growth), rpt_v,
      ymin = rpt_v - rpt_se,
      ymax = rpt_v + rpt_se,
      fill = factor(growth)
    ),
    show.legend = F,
    shape = 21,
    alpha = 0.8,
    position = position_jitter(width = 0.1)
  )+
  scale_fill_manual(
    values = rev(terrain.colors(3))
  )+
  theme_bw(
    base_size = 8
  )+
  theme(
    strip.background = element_blank(),
      strip.text = element_text(
        face = "italic"
      ),
    axis.title.x = ggtext::element_markdown()
  )+
  facet_grid(
      ~ comp_strat, 
      # ncol = 2, 
      labeller = labeller(
        .multi_line = F,
        comp_strat = c(
          "forager" = "Foragers",
          "klept" = "Kleptoparasites"
        )
      )
    )+
  coord_cartesian(
    ylim = c(0, 1),
    xlim = c(0.75, 2.25),
    expand = F
  )+
  labs(
    x = "*r<sub>max</sub>*",
    y = "Repeatability"
  )
fig_rpt =
  wrap_plots(
    fig_pop_com,
    tag_level = "new"
  ) /
  wrap_plots(
    fig_sc1_rpt,
    wrap_plots(
      fig_sc2_rpt,
      fig_sc2_strat,
      fig_sc2_split,
      tag_level = "new",
      design = "ABCC"
    ),
    design = "ABBBB",
    tag_level = "new"
  ) / 
  plot_layout(
    design = "AA\nBB"
  ) &
  plot_annotation(
    tag_levels = c("A", "1", "a")
  ) &
  theme(
    plot.tag = element_text(
      face = "bold",
      size = 8
    )
  )

ggsave(
  fig_rpt,
  filename = "figures/fig_03.png",
  width = 170,
  height = 130,
  units = "mm"
)


pratikunterwegs/process-pattern documentation built on Dec. 22, 2021, 9:50 a.m.