Data Visualization

Topic 03 · ggplot2 & Grammar of Graphics

Ashwin Kishtagari, MBBS, MSCI

Division of Hematology/Oncology, Vanderbilt University Medical Center

2026-08-18

The Grammar of Graphics

Layered thinking

A plot is built from:

  • Data — what you’re showing
  • Aesthetics — mapping variables to visual properties
  • Geometries — points, lines, bars
  • Scales, facets, themes — refinement

A basic ggplot

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  labs(x = "Weight", y = "MPG", color = "Cylinders") +
  theme_minimal()

Adding a trend

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", se = TRUE) +
  theme_minimal()

Faceting

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl) +
  theme_minimal()

Summary

  • Build plots in layers
  • Map data to aesthetics intentionally
  • Facets reveal structure across groups