ggplot2: Data Visualization
The grammar of graphics: R’s most beautiful tool
Concept Recap
ggplot2 builds charts in layers. Each layer adds to the previous one using +:
- ggplot(data, aes(...)): declare data and aesthetic mappings (x, y, color, fill, size, shape)
- geom_bar / geom_col: bar charts (
geom_barcounts rows,geom_coluses a y value) - geom_line / geom_point: line and scatter plots
- geom_histogram / geom_boxplot: distributions
- labs(): title, x, y labels
- theme_minimal() / theme_classic(): clean built in themes
- facet_wrap(~var): separate panels per group
Charts render directly on the page when you click Run: look for the image below the output box!
Worked Example
A polished horizontal bar chart with labels and a custom theme:
Exercise 1: Scatter Plot: GPA by Year
Run this scatter plot that shows GPA vs. year, colored by major. The code is complete: run it, then try adjusting the point size or color palette.
aes(color = major) maps the major variable to point color. geom_text(aes(label = name)) adds student name labels. vjust = -1 positions labels above the points.Exercise 2: Grouped Bar Chart
Create a grouped bar chart comparing average GPA by major AND year using the students data. Use stat="identity" after computing averages with dplyr, and position="dodge" to place bars side by side.
factor(year) tells ggplot to treat year as a discrete category rather than a continuous number, which gives each year its own bar color. position="dodge" places bars side by side instead of stacked.Exercise 3: Faceted Distribution
Create a 2 panel figure using facet_wrap(~major) showing the GPA distribution (histogram or density) per major, with a vertical dashed line marking the mean GPA for each panel.
geom_vline(data = means, ...), ggplot will correctly draw one line per panel. facet_wrap(~major) splits the plot into separate panels, one per major value.Build 4 Separate ggplot2 Charts
Create all four charts one at a time (run each button separately). Each chart should have a title, clean theme, and proper axis labels.
Chart 1: Bar Chart, Total Revenue by Rep
Chart 2: Line Chart, Revenue by Month per Rep
Chart 3: Boxplot, Revenue Distribution by Region
Chart 4: Scatter Plot, Revenue colored by Region
Lab 6 Complete!
You’ve created bar charts, scatter plots, line charts, boxplots, and faceted figures, the core of professional data visualization in R. ggplot2’s layered grammar makes complex charts surprisingly simple to build.
Continue to Lab 7: Statistical Analysis →