Implementing the Grammar of Graphics with {ggplot2}

Introduction to Data Science

PDF version of slides

{ggplot2}

A package that implements a layered grammar of graphics

{tidyverse}

  • An opinionated collection of R packages designed for data science

  • All packages share an underlying philosophy and a common grammar

library(tidyverse)

Minimalist plot template

ggplot(data = <DATA>) +
  <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

Example

ggplot(data = penguins) +
  geom_histogram(mapping = aes(x = flipper_length_mm, fill = species))

RStudio cheatsheet lays out all geom options

Aesthetics options

Visual characteristics of plotting characters that can be mapped to a specific variable in the data are

  • color

  • size

  • shape

  • alpha (transparency)

Facets

Small multiples are created through facets

ggplot(data = penguins) +
  geom_histogram(mapping = aes(x = flipper_length_mm, fill = species)) +
  facet_wrap(~ species)

Theme elements

Non-data components of the plot that can be modified with theme elements

ggplot(data = penguins) +
  geom_histogram(mapping = aes(x = flipper_length_mm, fill = species)) +
  facet_wrap(~ species) +
  labs(x = "Flipper length (mm)", 
       title = "Penguins near Palmer Station")

Updated template

ggplot(data = <DATA>) +
  <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>)) +
  <FACET_FUNCTION> +
  <THEME_FUNCTION>