Intermediate
Data Visualization with ggplot2
Build publication-quality graphics using the grammar of graphics - the most powerful visualization system in any data science language.
The Grammar of Graphics
ggplot2 is built on a layered grammar where every plot is composed of:
- Data: The dataset to visualize
- Aesthetics (aes): Mappings from data variables to visual properties (x, y, color, size)
- Geometries (geom): The visual marks (points, lines, bars)
- Facets: Subplots for different subsets
- Statistics: Statistical transformations
- Coordinates: The coordinate system
- Themes: Visual styling
R
library(ggplot2) # Basic structure: data + aesthetics + geometry ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()
Common Geoms
Scatter Plot - geom_point
R
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point(size = 3, alpha = 0.7) + labs(title = "MPG vs Weight", color = "Cylinders")
Line Chart - geom_line
R
ggplot(economics, aes(x = date, y = unemploy)) + geom_line(color = "steelblue", linewidth = 1) + labs(title = "US Unemployment Over Time")
Bar Chart - geom_bar / geom_col
R
# geom_bar counts occurrences ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) + geom_bar() # geom_col uses pre-computed values df <- mtcars |> group_by(cyl) |> summarise(avg = mean(mpg)) ggplot(df, aes(x = factor(cyl), y = avg, fill = factor(cyl))) + geom_col()
Histogram & Boxplot
R
# Histogram ggplot(mtcars, aes(x = mpg)) + geom_histogram(bins = 15, fill = "steelblue", color = "white") # Boxplot ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) + geom_boxplot() # Violin plot ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) + geom_violin() + geom_jitter(width = 0.1, alpha = 0.5)
Heatmap - geom_tile
R
cor_data <- as.data.frame(as.table(cor(mtcars))) ggplot(cor_data, aes(x = Var1, y = Var2, fill = Freq)) + geom_tile() + scale_fill_gradient2(low = "blue", high = "red", mid = "white")
Faceting
R
# facet_wrap - wrap into rows/columns ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + facet_wrap(~ cyl) # facet_grid - 2D grid of panels ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + facet_grid(am ~ cyl)
Themes, Colors, Labels
R
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point(size = 3) + scale_color_brewer(palette = "Set1") + labs( title = "Fuel Efficiency by Weight", subtitle = "Colored by number of cylinders", x = "Weight (1000 lbs)", y = "Miles per Gallon", color = "Cylinders", caption = "Source: mtcars dataset" ) + theme_minimal() + theme( plot.title = element_text(face = "bold", size = 16), legend.position = "bottom" )
Saving Plots
R
# Save the last plot ggsave("my_plot.png", width = 10, height = 6, dpi = 300) ggsave("my_plot.pdf", width = 10, height = 6) # Save a specific plot object p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() ggsave("scatter.png", plot = p)
Interactive with plotly
R
library(plotly) p <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point(size = 3) # Convert any ggplot to interactive ggplotly(p)
Tip: ggplot2 has over 50 geom_* functions. Start with the basics (point, line, bar, histogram, boxplot) and explore more as needed. The ggplot2 reference is excellent.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX