#' Add sciensano theme to ggplot chart
#'
#' This function allows you to add the sciencsano theme to your ggplotgraphics.
#' @keywords sciensano_style
#' @param font Specify one of the available fonts (Default: Arial)
#'
#' @importFrom ggplot2 %+replace%
#'
#' @export
#' @examples
#' \dontrun{line <- ggplot(line_df, aes(x = year, y = lifeExp)) +
#' geom_line(colour = "#007f7f", size = 1) +
#' geom_hline(yintercept = 75, size = 1, colour="#3AAA35") +
#' sciensano_style()}
sciensano_style <- function(font = "Arial") {
if (!(font %in% extrafont::fonts())) {
warning("Please specify an available font\nThe first available font will be taken:")
message(paste0("Using font: ", extrafont::fonts()[1]))
font <- extrafont::fonts()[1]
}
## build on theme_bw
ggplot2::theme_minimal() %+replace%
ggplot2::theme(
#Text format:
#This sets the font, size, type and colour of text for the chart's title
plot.title = ggplot2::element_text(family=font,
size=28,
face="bold",
color="black"),
#This sets the font, size, type and colour of text for the chart's subtitle, as well as setting a margin between the title and the subtitle
plot.subtitle = ggplot2::element_text(family=font,
size=22,
margin=ggplot2::margin(9,0,9,0)),
plot.caption = ggplot2::element_blank(),
#This leaves the caption text element empty, because it is set elsewhere in the finalise plot function
#Legend format
#This sets the position and alignment of the legend, removes a title and backround for it and sets the requirements for any text within the legend. The legend may often need some more manual tweaking when it comes to its exact position based on the plot coordinates.
legend.position = "bottom",
legend.text.align = 0,
legend.background = ggplot2::element_blank(),
legend.title = ggplot2::element_blank(),
legend.key = ggplot2::element_blank(),
legend.text = ggplot2::element_text(family=font,
size=18,
color="black"),
#Axis format
#This sets the text font, size and colour for the axis test, as well as setting the margins and removes lines and ticks. In some cases, axis lines and axis ticks are things we would want to have in the chart - the cookbook shows examples of how to do so.
axis.title = ggplot2::element_blank(),
axis.text = ggplot2::element_text(family=font,
size=18,
color="black"),
axis.text.x = ggplot2::element_text(margin=ggplot2::margin(5, b = 10)),
axis.ticks = ggplot2::element_blank(),
axis.line = ggplot2::element_blank(),
#Grid lines
#This removes all minor gridlines and adds major y gridlines. In many cases you will want to change this to remove y gridlines and add x gridlines. The cookbook shows you examples for doing so
panel.grid.minor = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_line(color="#cbcbcb"),
panel.grid.major.x = ggplot2::element_blank(),
#Blank background
#This sets the panel background as blank, removing the standard grey ggplot background colour from the plot
panel.background = ggplot2::element_blank(),
#Strip background (#This sets the panel background for facet-wrapped plots to white, removing the standard grey ggplot background colour and sets the title size of the facet-wrap title to font size 18)
strip.background = ggplot2::element_rect(fill="#3AAA35FF"),
strip.text = ggplot2::element_text(size = 18, color = "white")
)
}
#' Arrange alignment and save Sciensano ggplot chart
#'
#' Running this function will save your plot with the correct guidelines for publication for a Sciensano graphic.
#' It will left align your title, subtitle and source, add the Sciensano 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 (DEFAULT: set to \code{FALSE} if you do not want to save plot)
#' @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 Sciensano blocks image that sits within the data folder of your package
#' @return (Invisibly) an updated ggplot object.
#' @keywords finalise_plot
#' @examples
#' \dontrun{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=FALSE,
width_pixels=640,
height_pixels=450,
logo_image_path = file.path(system.file("extdata", package = 'ggsano'),"logo.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.10/(height_pixels/450)))
## saving the plot
if (save_filepath != FALSE) {
message(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)
}
### HELPER FX
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::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.900))
return(footer)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.