-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalise_plot.R
72 lines (61 loc) · 2.99 KB
/
finalise_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
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)
}
create_footer <- function (source_name, logo_image_path) {
#Make the footer
footer <- grid::grobTree(grid::textGrob(source_name,
x = 0.004, hjust = 0, gp = grid::gpar(fontsize=16)),
grid::rasterGrob(png::readPNG(logo_image_path), x = 0.9))
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 finalise_plot
#' @examples
#' finalise_plot(plot_name = myplot,
#' source = "The source for my data",
#' save_filepath = "filename_that_my_plot_should_be_saved_to-nc.png",
#' width_pixels = 640,
#' height_pixels = 450,
#' logo_image_path = "logo_image_filepath.png"
#' )
#'
#' @export
finalise_plot <- function(plot_name,
source_name,
save_filepath=file.path(Sys.getenv("TMPDIR"), "tmp-nc.png"),
width_pixels=840,
height_pixels=450,
logo_image_path = system.file("logo.png", package = "FlowmindeR")) {
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)
}