SAVOR: Part IV

Graphics with ggplot2

Boris Hejblum

June 11, 2025

ggplot2

An package for graphics

  • part of the tidyverse

  • originally developped by Hadley Wickham 🫶

Grammar of graphics

2 principles

  • a graph is made of several layers

  • grammar (i.e. mathematical & esthetical rules) between layers

A ggplot2 components

7 components:

source: ggplot2 website

Essential components

ggplot2 needs at least one of each of the three components below to make a plot:

Component Description
Data data to be displayed
Mapping aesthetics the variables that will be displayed
Geometry layers the way the data will be displayed

How to construct a ggplot

  1. Initialize the data — through ggplot(data=...)

  2. Add (at least) one geometry layer — through +geom_xxx()

  3. specify required aesthetics mapping — through aes()

Most common aesthetics mapping

  • x
  • y
  • colour
  • fill
  • size
  • alpha
  • linetype
  • labels
  • shape

Common layers

More than 37 different geometries available…

  • geom_point
  • geom_line
  • geom_tile
  • geom_bar

Colour palettes

Warning

Chosing colors is hard ! (as hard as naming things ?)

Be mindful of:

  • black & white (printing)

  • colorblinds (~10% of white men)

viridis 👉🌐

MetBrewer 👉🌐

Tip

stop wasting time

Additional resources

ggplot2 practical

👉 Your turn !

Open SAVOR_practical4.html and follow along…