#' Title
#'
#' @param plot_grid
#' @param width
#' @param height
#' @param save_filepath
#'
#' @return
#' @export
#'
#' @examples
save_plot <- function (plot_grid, width, height, save_filepath) {
grid::grid.draw(plot_grid)
#save it
ggplot2::ggsave(filename = save_filepath,
plot=plot_grid, width=(width/72), height=(height/72), bg="white")
}
#Left align text
#' Title
#'
#' @param plot_name
#' @param pieces
#'
#' @return
#' @export
#'
#' @examples
left_align <- function(plot_name, pieces){
grob <- ggplot2::ggplotGrob(plot_name)
n <- length(pieces)
grob$layout$l[grob$layout$name %in% pieces] <- 2
return(grob)
}
#' Title
#'
#' @param source_name
#' @param logo_image_path
#'
#' @return
#' @export
#'
#' @examples
create_footer <- function (source_name, logo_image_path) {
#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.004, hjust = 0, gp = grid::gpar(fontsize=16)),
grid::rasterGrob(png::readPNG(logo_image_path), x = 0.8))
return(footer)
}
#' Arrange alignment and save BBC ggplot chart
#'
#' Running this function will save your plot with the correct guidelines for publication for a BBC News graphic.
#' It will left align your title, subtitle and source, add the BBC blocks at the bottom right and save it to your specified location.
#' @param plot_name The variable name of the plot you have created that you want to format and save
#' @param source_name The text you want to come after the text 'Source:' in the bottom left hand side of your side
#' @param save_filepath Exact filepath that you want the plot to be saved to
#' @param width_pixels Width in pixels that you want to save your chart to - defaults to 640
#' @param height_pixels Height in pixels that you want to save your chart to - defaults to 450
#' @param logo_image_path File path for the logo image you want to use in the right hand side of your chart,
#' which needs to be a PNG file - defaults to BBC blocks image that sits within the data folder of your package
#' @return (Invisibly) an updated ggplot object.
#' @keywords finalize_plot
#' @examples
#' @export
finalize_plot <- function(plot_name,
source_name = '',
save_filepath=file.path(Sys.getenv("TMPDIR"), "tmp-nc.png"),
width_pixels=50,
height_pixels=100,
logo_image_path = file.path(system.file("logos", package = 'gghutchins'),
"hutchins.png")) {
footer <- create_footer(source_name, logo_image_path)
#Draw your left-aligned grid
plot_left_aligned <- left_align(plot_name, c("subtitle", "title", "caption"))
plot_grid <- ggpubr::ggarrange(plot_left_aligned, footer,
ncol = 1, nrow = 2,
heights = c(1, 0.045/(height_pixels/450)))
## print(paste("Saving to", save_filepath))
save_plot(plot_grid, width_pixels, height_pixels, save_filepath)
## Return (invisibly) a copy of the graph. Can be assigned to a
## variable or silently ignored.
invisible(plot_grid)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.