Skip to contents

Following code extracts demographic history from population table, and draws the right panel of Fig. 5.

library(ggplot2)
library(tumopp)

.alt = list(k = c(1, 8, 2 ** 30))
.const = list(D = 3L, C = "hex", N = 16384L, L = "const")
args_table = tumopp::make_args(alt = .alt, const = .const, each = 1L)
results = tumopp::tumopp(args_table)

df = results |>
  dplyr::mutate(demography = purrr::map(population, \(x) {
    tumopp::extract_history(x) |> tumopp::summarize_demography()
  })) |>
  dplyr::select(shape, demography) |>
  tidyr::unnest("demography")

.breaks = .alt[["k"]]
.labels = ifelse(.breaks < 1e6, .breaks, expression(infinity))
.values = setNames(grey(c(0.666, 0.4, 0)), .breaks)

labels_log10 = function(breaks) {
  parse(text = paste0("10^", log10(breaks)))
}

fig5b = ggplot(df) +
  aes(time, size, group = shape) +
  geom_line(aes(color = as.factor(shape)), linewidth = 1, alpha = 0.8) +
  scale_y_log10(breaks = 10 ** seq_len(4L), labels = labels_log10) +
  scale_color_manual(
    name = expression(italic(k)),
    labels = .labels, values = .values,
    guide = guide_legend(reverse = TRUE, override.aes = list(size = 3))
  ) +
  labs(x = expression(Time ~ (italic(t))), y = "# of tumor cells") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.ticks = element_blank(),
    legend.key.height = unit(0.7, "lines"),
    legend.position = c(0.98, 0.02), legend.justification = c(1, 0)
  )

# ggsave("fig5b.png", fig5b, width = 3, height = 3)