Aesthetics control color, fill, size, shape, and linetype—often driven by factors. Use consistent palettes and accessible contrast in production reports.
Color by category (local)
ggplot(df, aes(x = week, y = score, color = group)) +
geom_line()
Factor-driven legends
df <- data.frame(
week = rep(1:3, 2),
score = c(80, 85, 90, 78, 82, 88),
group = factor(rep(c("A", "B"), each = 3))
)
print(df)
Important interview questions and answers
- Q: Color vs fill?
A: Color affects lines/points; fill affects bar/box interior—geom-dependent. - Q: Accessibility?
A: Do not rely on color alone—add labels, patterns, or direct annotations.
Self-check
- Why map group to color in aes?
- What R type helps control legend order?
Tip: Map discrete variables to color and continuous to size thoughtfully—check colorblind-safe palettes.
Interview prep
- Map color to factor?
Discrete groups get distinct colors with a legend automatically.