2024

ggplot2: A Brief History

The Philosophy Behind ggplot2

  • Grammar of Graphics: A system for describing and building graphs.

  • ggplot2 allows for the creation of complex plots from simple components.

  • The emphasis is on layering components to build up to the visualization you need.

Components of a ggplot

  • Aesthetics define how your data is mapped to visual properties (e.g., x, y , color).

  • Geoms represent geometric shapes that represent data (e.g., points, lines, bars).

  • Scales fine-tune how aesthetics relationships arevisually encoded (e.g., color maps).

  • Themes control the non-data parts of the plot (e.g., plot background, grid lines, and text elements.)

Aesthetics

  • Aesthetics define how your data is mapped to visual properties.

  • Common aesthetics include x, y, color, fill, size, and shape.

# `aes` used to map `displ` to x coordinates and `hwy` to y
# coordinates
library(ggplot2)
ggplot(mpg, aes(x=displ, y=hwy)) + 
    geom_point()

Aesthetics

Geoms

  • Geoms represent geometric shapes that represent data (e.g., points, lines, bars, etc.)
# `geom_smooth()` and `geom_point` used to represent x and y
ggplot(mpg, aes(x=displ, y=hwy)) + 
    geom_point()

Geoms

Scales

  • Scales fine-tune how aesthetics relationships arevisually encoded (e.g., color maps, axis transforms, etc).
# `scale_color_manual` used to control how levels of `class`
# are mapped to colors.
ggplot(mpg, aes(x=displ, y=hwy, color=class)) +
  geom_point() +
  scale_color_manual(values=c("compact"="blue", 
                              "suv"="red",
                              "pickup"="green",
                              "midsize"="purple",
                              "minivan"="orange",
                              "2seater"="brown",
                              "subcompact"="pink"))

Scales

Themes

  • Themes control the non-data parts of the plot (e.g., plot background, grid lines, and text elements.)
# `theme_minimal` is used set a minimal appearance to the
# overall plot
ggplot(mpg, aes(x=displ, y=hwy)) +
    geom_point() + 
    theme_minimal()

Themes

Summary

  • Aesthetics define how your data is mapped to visual properties (e.g., x, y , color).

  • Geoms represent geometric shapes that represent data (e.g., points, lines, bars).

  • Scales fine-tune how aesthetics relationships arevisually encoded (e.g., color maps).

  • Themes control the non-data parts of the plot (e.g., plot background, grid lines, and text elements.)

Question

Identify the major components of the following ggplot2 plot.

ggplot(mpg, aes(x=displ, y=hwy, color=class)) +
  geom_point() +
  scale_color_manual(values=c("compact"="blue", 
                              "suv"="red",
                              "pickup"="green",
                              "midsize"="purple",
                              "minivan"="orange",
                              "2seater"="brown",
                              "subcompact"="pink")) +
    theme_minimal()

Answer

Click here for the answer
  • aes(x=displ, y=hwy, color=class) specifies the aesthetic.

  • geom_point() specifies the geometric object.

  • scale_color_manual specifies the color mapping.

  • theme_minimal specifies the theme.