R Tutorial

Data Visualization

The Grammar of Graphics: Mastering ggplot2

What is ggplot2?

ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.
r
library(ggplot2)

ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Weight vs. Fuel Efficiency")

The Layered Grammar

Wait! Building a plot in ggplot2 is like building a sandwich. You add layers using the + operator.
  1. Data: What are you plotting?
  2. Aesthetics (aes): Which variables map to the X-axis, Y-axis, Color, or Size?
  3. Geometries (geom): How should the data be represented? (Points, Bars, Lines, etc.)
  4. Facets: Should the plot be split into sub-plots?
  5. Themes: What should the overall "look" be?

Common Geometries

GeomPlot Type
geom_point()Scatter Plot
geom_line()Line Chart
geom_bar()Bar Chart
geom_boxplot()Box Plot
geom_histogram()Histogram
geom_density()Density Plot

Faceting

Wait! You can create multiple small charts for different categories in just one line using facet_wrap().
r
ggplot(df, aes(x, y)) + geom_point() + facet_wrap(~category)

Summary

TermDescription
aesAesthetic mapping (Connecting data to visuals)
geomThe type of plot
labsLabels (Title, Subtitle, Axis names)
themeThe stylistic look (e.g., theme_minimal())
+The operator used to add layers
Best ForCreating high-quality, professional data visualizations
Logic"Declarative plotting: Describe WHAT, not HOW"
SafetyAutomatic scaling and legend generation
ModernThe most popular visualization library in the world
StandardUsed by data journalists and researchers globally
IdentityThe "Masterpiece" of the R ecosystem
ScalabilityHandles millions of points efficiently