June 7th, 2024

ggplot2

An R package for graphics

  • part of the tidyverse

  • developped by Hadley Wickham

Grammar of graphics

2 principles

  • a graph is made of several layers

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

ggplot2 layers

[source: ggplot2 website]

Essential layers

Layer Description
Data data to be displayed
Aesthetics the variables that will be displayed
Geometries the way the data will be displayed

How to construct a ggplot

  1. initialize the data

  2. add (at least) one geometry — with +

  3. specify required Aesthetics — with aes()

Aesthetics

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

Geometries

37 different geometries…

  • geom_point
  • geom_line
  • geom_tile
  • geom_bar

Colour palettes

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

Be mindful of:

  • black and white

  • colorblinds (~10% of white men)

➡️ viridis 🌐

➡️ MetBrewer 🌐

stop wasting time

ggplot2 practical

Open BADAS4_practical.html and follow along…

Additional resources