Skip to contents

The following plots will be used as examples, to provide customizations features.

library(ggplot2)
p_colour <- 
  ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) + 
  geom_point(size = 4)

p_fill <-
  ggplot(diamonds, aes(x = clarity, fill = color)) + 
  geom_bar(position = position_dodge())

df <- data.frame(
 x = runif(100),
 y = runif(100),
 z1 = rnorm(100),
 z2 = abs(rnorm(100))
)

p_gradient <-
  ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z1), size = 3) +
  theme_bw()

p_fill_gradient <-
  ggplot(faithfuld, aes(waiting, eruptions)) +
  geom_raster(aes(fill = density))

Installation

The moment you load the g6tr.theme package, the fonts we use are copied to your directory.

Please go to the g6tr.theme/fonts/Lat directory, open up the following fonts and press install:

  • Lato-Regular.ttf

On Windows: Be aware that if you want to manually specify the family above elsewhere, you need to use this:

grDevices::windowsFonts(`Raleway` = grDevices::windowsFont("Lato"))

Your font names are then:

  • Lato

In the case you would like to manually replace fonts in the plot, you could do it like geom_text(font_family = "Lato").

And should use them as such geom_text(font_family = "Lato")

czi_pal() - palettes

You can use CZI branded palettes provided in theme_czi:

with the usage of czi_pal() function

# czi_pal('core') is a function, czi_pal('core')(7) is a call to export 7 first colors

scales::show_col(czi_pal('core')(13))

scales::show_col(czi_pal('neutrals')(2))

scales::show_col(czi_pal('vary')(15))

# czi_pal('names') let's you see the values and the initial order of the color palette
czi_pal('names')
       core_pink        core_teal      core_orange        core_blue 
       "#ED4753"        "#54B9C4"        "#ffab40"        "#0F4BCD" 
      core_green      core_yellow      core_maroon  core_pink_light 
       "#39D399"        "#FE963C"        "#983045"        "#fc8b89" 
 core_teal_light  core_blue_light core_green_light    core_teal_med 
       "#8CD0D7"        "#577CDB"        "#70DFB4"        "#70C5CE" 
  core_teal_dark         darkgray        lightgray 
       "#0097a7"        "#545454"        "#a2a2a2" 
# czi_pal('core') is a function, czi_pal('core')(4) is a call to return 4 first colors
# rearrange the order of colors returned
czi_pal('core', order = c(3, 2, 1, 7:4))(4)
[1] "#ffab40" "#54B9C4" "#ED4753" "#983045"

those values can be used in manual settings of scales.

p_colour + scale_color_manual(values = czi_pal('core')(3))

p_colour + scale_color_manual(values = czi_pal('core', order = c(3, 2, 1, 4:7))(3))

  p_fill + scale_fill_manual( values = czi_pal()(7)) # core by default

theme_czi() - fonts and layout

This theme automatically loads all fonts if you have installed them. The basic text geom_* are also changed to work with our fonts. Take a regular text geom for example:

ggplot(mtcars,
  aes(wt, mpg, label = rownames(mtcars), colour = factor(cyl))) +
  geom_point() +
  geom_label() + 
  theme_czi()

title = 'Title in Lato'
subtitle = 'Subtitle in Lato'
xaxis = 'Lato'
yaxis = 'Lato'

However, if for example, you use the ggrepel package and apply the geom_repel, you’ll need to use the following:

library(ggrepel)
update_geom_defaults("text_repel",
                     list(colour = "#8F928E",   
                          family = "Lato",
                          size = 4))
#OR

ggplot(mtcars,
  aes(wt, mpg, label = rownames(mtcars), colour = factor(cyl))) +
  geom_point() +
  geom_label_repel(family = "Lato", size = 4) + 
  theme_czi() 

White Theme

The default uses major gridlines on the X-axis. This goes well together with the coord_flip() function. However. If you decide to remove the coord_flip(), make sure you add this line:

theme(panel.grid.major.y = element_line(colour = "#545454"),
      panel.grid.major.x = element_blank())
 ggplot(diamonds, aes(x = clarity, fill = color)) + 
 geom_bar(position = position_dodge()) +
 theme_czi() +
 labs(title = title,
       subtitle = subtitle,
       x = xaxis,
       y = yaxis) +
 coord_flip() 

Finalizing & saving your plot

You can find the created plots in the directory of the package: g6tr.theme/inst/plots

Transparent Background (Default)

myplot <- 
  ggplot(diamonds, aes(x = clarity, fill = color)) +
  geom_bar(position = position_dodge()) +
  theme_czi(transparent = TRUE, base_size = 16) +
  labs(
    title = title,
    subtitle = subtitle,
    x = xaxis,
    y = yaxis
  ) +
  coord_flip()

Simple blank transparent themed plot.

# Invisibly returns a plot as well so you can still use it

finalise_plot(
  plot = myplot,
  filepath = '',
  width_pixels = 600,
  height_pixels = 800,
  brand = 'populace'
)

White Background

 myplot <- 
  ggplot(diamonds, aes(x = clarity, fill = color)) +
  geom_bar(position = position_dodge()) +
  theme_czi() +
  labs(title = title,
       subtitle = subtitle,
       x = xaxis,
       y = yaxis) +
  coord_flip()

Adding a footer with both a source on the left and a logo on the right.

# Invisibly returns a plot as well so you can still use it

finalise_plot(
  plot = myplot,
  add_footer = T,
  filepath = '',
  source = 'Source: Left hand side',
  width_pixels = 600,
  height_pixels = 800,
  brand = 'populace',
  logo = T, #Whether to include the logo or not
  background_color = 'white'
)

Saving the plot without a footer

# Invisibly returns a plot as well so you can still use it

finalise_plot(
  plot = myplot,
  add_footer = F,
  filepath = '',
  width_pixels = 600,
  height_pixels = 800,
  brand = 'populace',
  logo = F, #Should be False if no footer (also done automatically)
  background_color = 'white'
)

Colored Background

Note that this only works when saving the plot. If you want to have the background changed in the plot inline you’ll need to change that in the plot theme itself. See example:

 myplot <- 
  ggplot(diamonds, aes(x = clarity, fill = color)) +
  geom_bar(position = position_dodge()) +
  theme_czi(transparent = TRUE) +
  labs(title = title,
       subtitle = subtitle,
       x = xaxis,
       y = yaxis) +
  coord_flip()

Adding a footer with both a source on the left and a logo on the right. Inline it doesn’t work:

# Invisibly returns a plot as well so you can still use it

finalise_plot(
  plot = myplot,
  add_footer = T,
  filepath = 'colored_with_footer.png',
  source = 'Source: Left hand side',
  width_pixels = 600,
  height_pixels = 800,
  brand = 'populace',
  logo = T, #Whether to include the logo or not
  background_color = '#FEEC53' #Use type = transparent in the theme to get the full background colored
)

But if you read the image it does:

knitr::include_graphics("colored_with_footer.png")

Saving the plot without a footer

# Invisibly returns a plot as well so you can still use it

finalise_plot(
  plot = myplot,
  add_footer = F,
  filepath = '',
  width_pixels = 600,
  height_pixels = 800,
  brand = 'populace',
  logo = F, #Should be False if no footer (also done automatically)
  background_color = 'white'
)