R/finalise_plot.R

Defines functions pretty_plot

Documented in pretty_plot

create_footer <- function (source_name, logo_path, fontsize = 16) {
  #Make the footer
  footer <- grid::grobTree(grid::linesGrob(x = grid::unit(c(0, 1), "npc"), y = grid::unit(1.1, "npc")),
                           grid::textGrob(source_name,
                                          x = 0.01, hjust = 0, gp = grid::gpar(fontsize=fontsize)),
                           get_img(logo_path))
  return(footer)
}

#' Add a logo and text underneath a plot
#'
#' \code{pretty_plot} is a wrapper to place a logo at the bottom right and an optional text at the bottom left.
#' It is intended to give a consistent, 'corporate' feel to figures.
#' It adds an extra margin to the plot at the bottom where the logos and text are placed.
#'
#' @param plot A \code{ggplot} to be amended.
#' @param source_name A character string of text to be added bottom left.
#' @param logo_path Filepath to png or jpg file of logo to be added bottom right.
#' @param text_size Font size for the text. Default is 16
#' @param theme_apply Logical. If \code{TRUE} applies the HSRC theme to the plot.
#' @param facet_apply Logical. If \code{TRUE} applies colour and text size to a facet plot.
#' @return A plot with text (left) and logo (right)
#'
#' @examples
#' library(ggplot2)
#' pl_1 <- ggplot(mpg, aes(displ, hwy, colour = class)) + geom_point()
#' pl_1_no_legend <- ggplot(mpg, aes(displ, hwy, colour = class)) +
#' geom_point(show.legend = FALSE)
#'
#' pl_1
#' pl_1_no_legend
#'
#' pp_pl_1 <- pretty_plot(pl_1, source = "Text 14 point",
#' logo_path = system.file("extdata", "SNAP3_2019.jpg", package = "testpackage"),
#' text_size = 14, theme_apply = FALSE)
#'
#' pp_pl_1
#'
#' pp_pl_1_no_legend <- pretty_plot(pl_1, source = "Text 16 point, HSRC theme",
#' logo_path = system.file("extdata", "SNAP3_2019.jpg", package = "testpackage"),
#' text_size = 16, theme_apply = TRUE)
#'
#' pp_pl_1_no_legend
#'
#' pl_2 <- ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~class, ncol = 3)
#'
#' pp_pl_2 <- pretty_plot(pl_2, source_name = "Facet wrap",logo_path = system.file("extdata", "SNAP3_2019.jpg", package = "testpackage"),
#' text_size = 16, theme_apply = TRUE,
#' facet_apply = TRUE)
#'
#' pp_pl_2

#' @export
pretty_plot <- function(plot, source_name = "", logo_path,
                        text_size = 16,
                        theme_apply = TRUE,
                        facet_apply = FALSE) {
  new_footer <- create_footer(source_name, logo_path = logo_path, fontsize = text_size)
  if (theme_apply) {plot <- plot +
    theme_HSRC()} else
    {plot <- plot}
  if (facet_apply) {
    plot <- plot +
      theme(strip.text = element_text(
        size = text_size*0.75, color = grDevices::rgb(105, 105, 105, maxColorValue = 255)))
  } else
  {plot <- plot}
  plot_grid <- ggpubr::ggarrange(plot, new_footer,
                                 ncol = 1, nrow = 2,
                                 heights = c(1, 0.07))

}
IainMoppett/testpackage documentation built on Feb. 4, 2023, 11 p.m.