Skip to content

a DSB-styled ggplot2 visual identity for Epinion staff

License

Notifications You must be signed in to change notification settings

jvieroe/epinionDSB

Repository files navigation

epinionDSB

CodeFactor codecov

License: GPL (>= 3)

Master branch status: R-CMD-check

Introduction

The goal of epinionDSB is to provide easy-to-use templates for data visualizations using the ggplot2 package and in line with the Corporate Visual Identity (CVI) of DSB.

For this purpose, epinionDSB functionality includes:

  1. a set of tailor-made ggplot2 themes
  2. custom-made color palettes for both discrete and continuous mapping variables

Quick example

library(tidyverse)
library(epinionDSB)

ggplot(mtcars, aes(x = wt,
                   y = mpg)) +
  geom_point(aes(color = factor(cyl)),
             alpha = .85,
             size = 4) +
  facet_wrap(~ vs) +
  dsb_theme_classic(gridlines = "both") +
  color_dsb_d(reverse = T)

Installation

You can install the development version from GitHub with:

if(!require("devtools")) install.packages("devtools")
library(devtools)
devtools::install_github("jvieroe/epinionDSB")

Readymade ggplot2 themes

epinionDSB::dsb_theme_*() adds a tailormade theme to your ggplot2 objects. This makes them compliant with the DSB CVI and serves as a time-saver when producing several plots.

Choose between three themes:

  • dsb_theme_classic(), designed for most visualization purposes
    • arguments: legend, gridlines, textcolor
  • dsb_theme_map(), a very minimalist theme designed with geospatial maps in mind
    • arguments: legend, textcolor
  • dsb_theme_dust(), to a large degree similar to dsb_theme_classic() but with a warmer, dusty feel
    • arguments: legend, gridlines, textcolor, background

Note that all ggplot2::theme() settings inherent in dsb_theme*() can be overwritten by adding theme(...) elements afterwards.

ggplot(mtcars, aes(x = wt,
                   y = mpg,
                   color = factor(am))) +
  geom_point(size = 4) +
  dsb_theme_classic(gridlines = "none") +
  theme(legend.position = "right")

The dsb_theme_*() functions do not impact the aesthetics of your plot (e.g. color scales), only the ggplot2::theme() elements.

DSB color palettes

You can apply DSB themed colors with epinionDSB. Choose between the following approaches:

Extract HEX codes using dsb_colvec() or dsb_grabcol()

The epinionDSB package provides access to the color palette in the DSB design manual. Specifically, the dsb_colvec() function extracts HEX codes by the color names.

# ... all DSB colors
dsb_colvec()
#>        DSB Red   DSB DarkBlue  DSB LightBlue     DSB Orange  DSB LightGrey 
#>      "#B41730"      "#00233C"      "#5382B6"      "#DF652C"      "#A5A5A5" 
#>     DSB Purple       DSB Teal   DSB Turqoise   DSB DarkGrey DSB LightGreen 
#>      "#6E3C6E"      "#28767E"      "#41BDBF"      "#747474"      "#88C988" 
#>      DSB Green  DSB DarkGreen 
#>      "#1CA645"      "#144E36"

# a selection of colors
dsb_colvec("DSB Red", "DSB DarkBlue")
#>      DSB Red DSB DarkBlue 
#>    "#B41730"    "#00233C"

We can use these to manually change our colors by either (1) using the HEX codes provided by epinion::dsb_colvec() directly or (2) by pasting the names into the epinionDSB::dsb_grabcol() function. Both functions only accept colors in the Epinion color palette as inputs but the latter is not sensitive to the inclusion of the Epinion prefix:

ggplot(mtcars, aes(x = wt,
                   y = mpg,
                   color = factor(am))) +
  geom_point(size = 4) +
  dsb_theme_dust(legend = FALSE) +
  scale_colour_manual(values = c(dsb_grabcol("DSB Red"), 
                                 dsb_grabcol("DarkBlue")))

Apply ready-made color palettes

To provide a more verbose alternative, epinionDSB contains out-of-the-box functions to provide our ggplot2 figures with the DSB color palette. These functions apply to both discrete and continuous variable mapping and for both aes(color = ) and aes(fill = ):

Discrete variables

  • color_dsb_d: to use with the aes(color = x), where x is a factor or character variable
  • fill_dsb_d: to use with the aes(fill = x), where x is a factor or character variable

Continuous variables

  • color_dsb_c: to use with the aes(color = x), where x is a numeric or integer variable
  • fill_dsb_c: to use with the aes(fill = x), where x is a numeric or integer variable

The main argument taken by all four functions is reverse which allows you to reverse the order of the color scale (default is FALSE).

Additional arguments: *_dsb_d()

  • When mapping color_dsb_d() or fill_dsb_d() to a variable with only two levels, you can manually choose colors with the primary and secondary arguments. As with epinionDSB::dsb_grabcol() colors can be specified with or without the DSB prefix
p1 <-
  ggplot(mtcars, aes(x = wt,
                     y = mpg,
                     color = factor(am))) +
  geom_point(size = 4) +
  dsb_theme_classic(legend = FALSE) +
  color_dsb_d(primary = "DSB Red",
              secondary = "DSB Orange")

p2 <-
  ggplot(mtcars, aes(x = wt,
                     y = mpg,
                     color = factor(am))) +
  geom_point(size = 4) +
  dsb_theme_classic(legend = FALSE) +
  color_dsb_d(primary = "Red",
              secondary = "Orange")


library(patchwork)
p1 / p2

Additional arguments: *_epi_c()

  • epinionDSB contains four different continuous color palettes: reds, greens, blues, and teals.
  • You choose between these with the palette option in color_dsb_c() and fill_dsb_c()
# continuous variable in aes()
p1 <- 
  ggplot(mtcars, aes(x = wt,
                         y = mpg,
                         color = disp)) +
  geom_point(size = 4) +
  dsb_theme_dust(background = TRUE,
                 legend = FALSE) +
  color_dsb_c(palette = "greens")

p2 <- 
  ggplot(mtcars, aes(x = wt,
                         y = mpg,
                         color = disp)) +
  geom_point(size = 4) +
  dsb_theme_dust(background = TRUE,
                 legend = FALSE) +
  color_dsb_c(palette = "greens",
              reverse = TRUE)

p3 <- 
  ggplot(mtcars, aes(x = wt,
                         y = mpg,
                         color = disp)) +
  geom_point(size = 4) +
  dsb_theme_dust(background = TRUE,
                 legend = FALSE) +
  color_dsb_c(palette = "reds")

p4 <- 
  ggplot(mtcars, aes(x = wt,
                         y = mpg,
                         color = disp)) +
  geom_point(size = 4) +
  dsb_theme_dust(background = TRUE,
                 legend = FALSE) +
  color_dsb_c(palette = "reds",
              reverse = TRUE)


library(patchwork)
library(repinion)

(p1 + p2) /
  (p3 + p4) + 
  plot_annotation(theme = 
                    theme(plot.background = 
                            element_rect(color = NA,
                                         fill = repinion::grabcol("Epinion WarmSand"),
                                         )
                          )
                  )

Additional arguments: *_dsb_d()

  • When mapping color_dsb_d() or fill_dsb_d() to a variable with only two levels, you can manually choose colors with the primary and secondary arguments

Additional arguments

Notice that *_dsb_c() and *_dsb_d() inherently calls ggplot2::scale_*_gradientn() and ggplot2::discrete_scale(), respectively. For that reason, additional arguments, such as guide, also apply. See ggplot2::scale_colour_gradientn() and ggplot2::discrete_scale() for details on additional arguments.

Acknowledgements

  • The R Core Team for developing and maintaining the language
  • Hadley Wickham (hadley) and the rest of the team working on the amazing ggplot2 package (and, frankly, the entire tidyverse ecosystem)
  • Garrick Aden-Buie (gadenbuie) and Dr Simon Jackson (drsimonj) for inspiration
  • Steffen Bank (steffenbank) for convincing me to finally try to write my own R package

About

a DSB-styled ggplot2 visual identity for Epinion staff

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages