Basics of ggplot2

Robert Schlegel

Problem

  • Default R plotting (Base R) hasn’t advanced much since the 90s
  • Non-intuitive syntax, functions, and arguments
  • Published figures do not look very professional

Solution

  • ggplot2 uses the grammar of graphics
  • Integrated into tidyverse with intuitive functions and arguments
  • Well developed support and extensions for professional figures

Setup

We will need the following two packages for the examples in these slides.

library(tidyverse) # Contains ggplot2

library(palmerpenguins) # Contains the dataset

Basics

  • One figure is made of one chunk of code
  • Starts with ggplot()
  • Each line is connected with a +
  • Add shapes with geom functions
    • e.g. geom_point() adds points
  • Plot skeleton created within aes()
  • Arguments assigned like normal functions (e.g. ggplot(data = penguins))

Basic plot

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm)) +
  geom_point(aes(colour = species))

Focus on aes()

  • Understand when to use aes()
  • Columns from the data go inside aes()
    • e.g. geom_point(aes(colour = species))
  • Static values go outside aes()
  • Mistakes with aes() are common when learning ggplot2
  • Good starting point when looking for errors

Inside aes()

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm)) +
  # 'island' is a column from 'penguins'
  geom_point(aes(colour = island))

Outside aes()

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm)) +
  # Make all points red
  geom_point(colour = "red")

Outside inside?

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm)) +
  # What is happening here?
  geom_point(aes(colour = "red"))

Inside outside?

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm)) +
  # Why does this cause an error?
  geom_point(colour = species)
Error in list2(na.rm = na.rm, ...): object 'species' not found

More than just colour

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm)) +
  # What else can we add?
  geom_point(aes(size = flipper_length_mm, shape = island))

Add a geom

ggplot(data = penguins,
       # NB: Arguments passed to first aes() apply to all geoms
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth(method = "lm")

Change labels

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  # Change label text
  labs(x = "Body mass (g)", y = "Bill length (mm)", colour = "Species") +
  # Change legend position
  theme(legend.position = "bottom")