Advanced Visualizations with ggplot2
ggplot2 is R's most celebrated visualization package, based on the "Grammar of Graphics" — a layered approach where you build charts piece by piece: data, aesthetics, geometries, and themes.
Production Reality: ggplot2 is part of the tidyverse and expects tidy (long-format) data — revisit the tidyr lesson if your data is still in wide format.
1. The Grammar of Graphics — Core Structure
library(ggplot2)
ggplot(data = df, aes(x = age, y = salary)) +
geom_point()
Every ggplot2 chart follows this pattern: ggplot(data, aes(...)) + geom_*() — data, aesthetic mappings, and one or more geometry layers, joined with +.
2. Aesthetics (aes) — Mapping Data to Visuals
ggplot(df, aes(x = age, y = salary, color = department, size = experience)) +
geom_point(alpha = 0.7)
Aesthetics map data columns to visual properties: x/y position, color, size, shape, and transparency (alpha).
3. Common Geometry Layers
| Geom | Chart Type |
|---|---|
| geom_point() | Scatter plot |
| geom_line() | Line chart |
| geom_bar() / geom_col() | Bar chart |
| geom_histogram() | Histogram |
| geom_boxplot() | Boxplot |
| geom_smooth() | Trend line / regression fit |
ggplot(df, aes(x = department, y = salary)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Salary Distribution by Department")
4. Adding Layers
ggplot(df, aes(x = age, y = salary)) +
geom_point(color = "steelblue") +
geom_smooth(method = "lm", se = TRUE, color = "red") +
labs(title = "Age vs Salary with Trend Line",
x = "Age", y = "Salary ($)")
5. Themes — Controlling the Overall Look
ggplot(df, aes(x = department, fill = department)) +
geom_bar() +
theme_minimal() +
theme(legend.position = "none",
plot.title = element_text(face = "bold", size = 14)) +
labs(title = "Employee Count by Department")
| Built-in Theme | Style |
|---|---|
| theme_minimal() | Clean, minimal gridlines |
| theme_classic() | Classic axis lines, no gridlines |
| theme_bw() | White background, black-and-white |
| theme_void() | No axes or gridlines at all |
6. Faceting — Small Multiples
facet_wrap() and facet_grid() split a single chart into a grid of subplots by category — extremely powerful for comparisons:
ggplot(df, aes(x = age, y = salary)) +
geom_point() +
facet_wrap(~ department) +
labs(title = "Age vs Salary by Department")
7. Saving ggplot2 Charts
ggsave("output/salary_by_dept.png", width = 8, height = 5, dpi = 300)
Pro Tip: ggsave() automatically saves the last plot you rendered, and infers the file format from the extension (.png, .pdf, .svg, etc.).
8. Production-Ready Checklist
- ✅ Understand the layered grammar — data + aes + geoms + theme.
- ✅ Choose the right geom — for the story your data tells.
- ✅ Use facet_wrap() for comparisons — across categories in one figure.
- ✅ Apply a consistent theme — for publication-ready output.
Pro Tip: ggplot2's learning curve pays off fast — once you know the grammar, building a completely new chart type is just swapping one geom for another.
Ready to master R Programming?
Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.
.png)