Customise the look with themes in ggplot2

Data Visualisation with R

👩🏻‍💻 Emi Tanaka @ Monash University

  • emi.tanaka@monash.edu
  • @statsgen
  • github.com/emitanaka
  • emitanaka.org



28th November 2022 Australasian Applied Statistics Conference 2022

Customising plots with theme

  • You can customise almost any aspect of the ggplot object.
  • The customisation follows a certain consistent rule which makes it cognitively easier to remember how to customise the plot.

theme: modify the look of texts

element_text()

element_text()

  • Modify any text in the plot as you like it!
ggplot(diamonds, aes(carat, price)) +
  geom_hex() +
  labs(title = "Diamond") +
  theme(
    axis.title.x = element_text(
      size = 30,
      face = "bold",
      angle = 10,
      family = "Fira Code"
    ),
    legend.title = element_text(
      color = "#ef42eb",
      margin = margin(b = 5)
    ),
    plot.title = element_text(
      size = 35,
      family = "Nunito",
      color = "blue"
    )
  )

theme: modify the look of the lines

element_line()

element_line()

  • If there’s a line in the plot that’s not data related, then it’s generally controlled in the theme with element_line().
ggplot(diamonds, aes(carat, price)) +
  geom_hex() +
  labs(title = "Diamond") +
  theme(
    axis.line.y = element_line(
      color = "black",
      size = 1.2,
      arrow = grid::arrow()
    ),
    axis.line.x = element_line(
      linetype = "dashed",
      color = "brown",
      size = 1.2
    ),
    axis.ticks = element_line(color = "red", size = 1.1),
    axis.ticks.length = unit(3, "mm"),
    panel.grid.major = element_line(color = "blue",
                                    size = 1.2),
    panel.grid.minor = element_line(
      color = "#0080ff",
      size = 1.2,
      linetype = "dotted"
    )
  )

theme: modify the look of the
rectangular regions

element_rect()

element_rect()

ggplot(diamonds, aes(carat, price)) +
  geom_hex() +
  labs(title = "Diamond") +
  theme(
    legend.background = element_rect(
      fill = "#fff6c2",
      color = "black",
      linetype = "dashed"
    ),
    legend.key = element_rect(fill = "grey", color = "brown"),
    panel.background = element_rect(fill = "#005F59",
                                    color = "red", 
                                    size = 3),
    panel.border = element_rect(
      color = "black",
      fill = "transparent",
      linetype = "dashed",
      size = 3
    ),
    plot.background = element_rect(fill = "#a1dce9",
                                   color = "black",
                                   size = 1.3),
    legend.position = "bottom"
  )

The selected colors don’t make it a pretty plot but it’s easier to distinguish between the different rectangular elements.

Professional-looking plots

  • You can make very pretty plots using ggplot.
  • Using code to draw plots has an advantage over making plots and editing the plot manually, e.g. using Adobe Illustrator – your plots are easily reproducible!
  • Imagine that after you manually edit a plot, the data was updated – you’ll have to redo your manual task from scratch again!

Let’s make some polished plots!

45:00

> Go to emitanaka.org/dataviz-workshop/exercises/
> Click Exercise 7
> This is the last practice session – feel free to ask questions!