ggplot2 builds charts with layers: data, aesthetics, geoms, facets, and themes. Install with install.packages("ggplot2")—lessons explain locally; playground prints chart-ready data.
Grammar of graphics
library(ggplot2)
ggplot(df, aes(x = week, y = score)) +
geom_point() +
labs(title = "Scores over time")
Playground: prepare data
chart <- data.frame(week = 1:4, score = c(82, 85, 88, 90))
print(chart)
Understand the table ggplot would consume—compare layering with matplotlib in Python.
Important interview questions and answers
- Q: What is aes()?
A: Maps variables to visual properties—x, y, color, fill—ggplot2 handles scales and legends. - Q: Why ggplot2 locally?
A: Package not in base R sandbox; install locally for publication charts.
Self-check
- What ggplot2 function adds points?
- What does aes(x = week, y = score) map?
Tip: Install ggplot2 locally—prepare tidy data frames in the playground first.
Interview prep
- Grammar of graphics?
Layer data, aesthetics, geoms, scales, and themes declaratively.