knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
library(ggsano)

GGSANO

R-CMD-check

The goal of ggsano is to make the Sciensano color and housestyle available in R. The creation of this package was based on the bbplot package, a package with the same intetions createb by the BBC. So kudos to the BBC team.

Installation

You can install the latest version of ggsano from GitHub by running the following code:

# install.packages("devtools")
devtools::install_github("sciensanogit/ggsano")

The package can be loaded in R by:

library(ggsano)

Using the functions

Colors

The package has two palettes-functions available: pal_sciensano() for discrete colors, and pal_sciensano_c() for continuous colors.

Available palettes

For the color palette pal_sciensano, there are four options available: default, contrast, GnRd, and PuBl. The colors and HEX-codes are as follows:

DEFAULT

scales::show_col(pal_sciensano(palette = "default")(14), labels = TRUE)

CONTRAST

scales::show_col(pal_sciensano(palette = "contrast")(12),  labels = TRUE)

GnRd

scales::show_col(pal_sciensano(palette = "GnRd")(6),  labels = TRUE)

PuBl

scales::show_col(pal_sciensano(palette = "PuBl")(6),  labels = TRUE)

Examples

Using the palette for a discrete scale:

library("ggplot2")
library("reshape2")
library("cowplot")

data("mtcars")
data("diamonds")

ggplot(
  subset(diamonds, carat >= 2.2),
  aes(x = table, y = price, colour = cut)
) +
  geom_point(alpha = 0.7) +
  geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
  theme_bw() + scale_color_sciensano(palette = "GnRd", reverse = TRUE)

ggplot(
  subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),
  aes(x = depth, fill = cut)
) +
  geom_histogram(colour = "black", binwidth = 1, position = "dodge") +
  theme_bw() + scale_fill_sciensano(palette = "GnRd", reverse = TRUE)

Using the palette for a continuous scale:

cor <- abs(cor(mtcars))
cor_melt <- melt(cor)

p1 <- ggplot(
  cor_melt,
  aes(x = Var1, y = Var2, fill = value)
) +
  geom_tile(colour = "black", size = 0.3) +
  theme_bw() + scale_fill_sciensano_c(palette = "red-yellow-green")

p2 <- ggplot(
  cor_melt,
  aes(x = Var1, y = Var2, fill = value)
) +
  geom_tile(colour = "black", size = 0.3) +
  theme_bw() + scale_fill_sciensano_c(palette = "green")

p3 <- ggplot(
  cor_melt,
  aes(x = Var1, y = Var2, fill = value)
) +
  geom_tile(colour = "black", size = 0.3) +
  theme_bw() + scale_fill_sciensano_c(palette = "red")

cowplot::plot_grid(p1, p2, p3)

Sciensano style

sciensano_style(): has no arguments and is added to the ggplot chain after you have created a plot. It will change the text-size, font, and lay-out of the graph into a dedicated Sciensano theme.

p1 +
  sciensano_style() + ## Default font is Arial.
  theme(legend.key.width = ggplot2::unit(2.5, "cm")) ## extend the legend
ggplot(
  subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),
  aes(x = depth, fill = cut)) +
  geom_histogram(colour = "black", binwidth = 1, position = "dodge") +
  theme_bw() + scale_fill_sciensano(palette = "GnRd", reverse = TRUE) +
  sciensano_style()

Finalizing plots

finalise_plot: will save out your plot with the correct guidelines for publication for a Sciensano graphic. It is made up of functions that will left align your title, subtitle and source, add the Sciensano blocks at the bottom right and save it to your specified location. The function has six arguments, three of which need to be explicitly set and three that are defaults unless you overwrite them.

Here are the function arguments: finalise_plot(plot_name, source_name, save_filepath, width_pixels, height_pixels, logo_image_path)

final_plot <- finalise_plot(
  plot_name = p1 + 
    sciensano_style() +
    ggtitle("Example plot of mtcars", subtitle = "correlations") +
    theme(legend.key.width = ggplot2::unit(2.5, "cm")),
  source = "Source: mtcars",
  logo_image_path = "inst/extdata/logo.png",
  width_pixels = 640,
  height_pixels = 550
)

final_plot


sciensanogit/ggsano documentation built on April 14, 2022, 8:57 p.m.