R/create_figure_error_alignment_length_median.R

#' Create figure 'figure_error_alignment_length_median'
#' @param parameters parameters, as returned by read_collected_parameters
#' @param nltt_stats nLTT statistics, as returned by read_collected_nltt_stats
#' @param esses ESSes, as returned by read_collected_esses
#' @param filename name of the file the figure will be saved to
#' @export
#' @author Richel Bilderbeek
create_figure_error_alignment_length_median <- function(
  parameters,
  nltt_stats,
  esses,
  filename
) {

  sti <- NULL; rm(sti) # nolint, should fix warning: no visible binding for global variable
  ai <- NULL; rm(ai) # nolint, should fix warning: no visible binding for global variable
  pi <- NULL; rm(pi) # nolint, should fix warning: no visible binding for global variable
  nltt_stat <- NULL; rm(nltt_stat) # nolint, should fix warning: no visible binding for global variable
  scr <- NULL; rm(scr) # nolint, should fix warning: no visible binding for global variable
  sequence_length <- NULL; rm(sequence_length) # nolint, should fix warning: no visible binding for global variable
  median <- NULL; rm(median) # nolint, should fix warning: no visible binding for global variable

  `%>%` <- dplyr::`%>%`

  nltt_stat_medians <- nltt_stats %>% dplyr::group_by(filename, sti, ai, pi) %>%
    dplyr::summarise(median = median(nltt_stat))

  testit::assert(all(names(nltt_stat_medians)
    == c("filename", "sti", "ai", "pi", "median")))

  # Connect the mean nLTT stats and parameters
  testit::assert("filename" %in% names(parameters))
  testit::assert("filename" %in% names(nltt_stat_medians))
  df_medians <- merge(x = parameters, y = nltt_stat_medians, by = "filename", all = TRUE)

  # Merge with the ESSes
  df_medians <- merge(x = df_medians, y = esses, by = c("filename", "sti", "ai", "pi"), all = TRUE)


  ggplot2::ggplot(
    data = stats::na.omit(df_medians),
    ggplot2::aes(x = as.factor(scr), y = median, fill = as.factor(sequence_length))
  ) + ggplot2::geom_boxplot() +
      ggplot2::facet_grid(erg ~ sirg) +
      ggplot2::xlab(latex2exp::TeX("Speciation completion rate $\\lambda$")) +
      ggplot2::ylab(latex2exp::TeX("Median nLTT statistics, $\\widetilde{\\Delta_{nLTT}}$")) +
      ggplot2::labs(
        fill = latex2exp::TeX("Sequence\nlength $l_a$"),
        title = "The effect of alignment length on median nLTT statistic for\ndifferent speciation completion rates (x axis boxplot),\nspeciation initiation rates (columns)\nand extinction rates (rows)",
        caption = filename
      ) +
      ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))

  ggplot2::ggsave(file = filename, width = 7, height = 7)
}
richelbilderbeek/wiritttea documentation built on May 27, 2019, 8:02 a.m.