combine_cluster_plots_and_save.R 1.35 KiB
#' Plot list of three plots in three columns and save to 'path' with filename
#' 'Cluster_overview_'cluster'.pdf'
#'
#' This function combines a list of three plots into a single plot with three columns
#' and saves it as a PDF file with a specified filename. The cluster number is
#' included as a title on the top of the plot.
#'
#' @param plot_list A list of three plots to combine.
#' @param cluster The cluster number to include in the plot title.
#' @param out_path The path where the plot should be saved. Default is 'path'.
#' @param w The width of the plot. Default is 'cm_width'.
#' @param h The height of the plot. Default is 'cm_height'.
#' @return combined plot
#' @import ggplot2
#' @export
combine_cluster_plots_and_save <- function(plot_list, cluster, out_path = path,
w = cm_width, h = cm_height) {
dir.create(out_path, showWarnings = FALSE)
combo_pl <- ggpubr::ggarrange(plotlist = plot_list,
ncol = 3, align = "hv", common.legend = TRUE,
legend = "bottom")
combo_pl <- ggpubr::annotate_figure(combo_pl, top = ggpubr::text_grob(
paste("Cluster:", cluster), vjust = 0.5, x = 0.01, hjust = 0))
ggsave(file.path(out_path, paste0("Cluster_overview_", cluster, ".pdf")),
combo_pl, width = w, height = h, units = "cm")
return(combo_pl)
}