Advanced ggplot2
In this lesson, you will explore advanced features of ggplot2 that help you create more informative and professional-quality visualizations.
You will learn how to customize plot appearance, use themes, split data into multiple panels, and improve readability for real-world data analysis.
Adding Multiple Layers
One of the biggest strengths of ggplot2 is its layered structure.
You can combine multiple geometries in a single plot to show different aspects of the data.
data <- data.frame(
month = c("Jan", "Feb", "Mar", "Apr", "May"),
sales = c(200, 250, 300, 280, 350)
)
ggplot(data, aes(x = month, y = sales)) +
geom_col(fill = "skyblue") +
geom_point(color = "darkblue", size = 3)
Using Themes
Themes control the overall look and feel of a plot.
ggplot2 provides built-in themes that can be applied easily.
ggplot(data, aes(x = month, y = sales)) +
geom_col(fill = "steelblue") +
theme_minimal()
Themes help create clean and consistent visual styles across reports.
Commonly Used Themes
theme_minimal()– clean and moderntheme_classic()– simple with axis linestheme_bw()– black and white style
Customizing Axes
Axis labels and text can be customized for better readability.
This is especially useful when working with long labels.
ggplot(data, aes(x = month, y = sales)) +
geom_col(fill = "orange") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1)
)
Faceting (Multiple Panels)
Faceting allows you to split a dataset into multiple panels based on a variable.
This helps compare patterns across categories.
data2 <- data.frame(
month = rep(c("Jan", "Feb", "Mar"), 2),
sales = c(200, 250, 300, 180, 220, 260),
region = c("East", "East", "East", "West", "West", "West")
)
ggplot(data2, aes(x = month, y = sales)) +
geom_col(fill = "lightgreen") +
facet_wrap(~ region)
Adding Labels to Bars
Data labels make plots easier to interpret by showing exact values.
This is commonly used in dashboards and reports.
ggplot(data, aes(x = month, y = sales)) +
geom_col(fill = "purple") +
geom_text(aes(label = sales), vjust = -0.5)
Why Advanced ggplot2 Matters
- Creates publication-ready visuals
- Improves clarity and storytelling
- Supports complex data comparisons
- Widely used in analytics and reporting
📝 Practice Exercises
Exercise 1
Create a bar chart with a minimal theme.
Exercise 2
Add rotated x-axis labels to a plot.
Exercise 3
Use faceting to compare data across groups.
Exercise 4
Add value labels to bars in a bar chart.
✅ Practice Answers
Answer 1
ggplot(data, aes(x = month, y = sales)) +
geom_col() +
theme_minimal()
Answer 2
ggplot(data, aes(x = month, y = sales)) +
geom_col() +
theme(axis.text.x = element_text(angle = 45))
Answer 3
ggplot(data2, aes(x = month, y = sales)) +
geom_col() +
facet_wrap(~ region)
Answer 4
ggplot(data, aes(x = month, y = sales)) +
geom_col() +
geom_text(aes(label = sales), vjust = -0.5)
What’s Next?
In the next lesson, you will move into Statistics Basics, where you will start applying R to statistical analysis.