Learn Without Walls
← Back to R Practice Labs
Lab 6 of 10

ggplot2: Data Visualization

The grammar of graphics — R’s most beautiful tool

← Lab 5: Strings Lab 6 of 10 Lab 7: Statistics →
⏳ Loading R... (first load takes ~15 seconds)

📖 Concept Recap

ggplot2 builds charts in layers. Each layer adds to the previous one using +:

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:

library(ggplot2) sales <- data.frame( category = c("Electronics","Clothing","Food","Sports","Books"), revenue = c(45000, 32000, 28000, 19000, 12000) ) ggplot(sales, aes(x = reorder(category, revenue), y = revenue, fill = category)) + geom_col(show.legend = FALSE) + geom_text(aes(label = paste0("$", format(revenue, big.mark=","))), hjust = -0.1, size = 3.5) + coord_flip() + scale_fill_brewer(palette = "Set2") + labs(title = "Revenue by Category", x = NULL, y = "Revenue ($)") + theme_minimal() + expand_limits(y = 55000)
✏️ Guided

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.

Output will appear here...
💡 Hint: 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.
💪 Independent

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.

Output will appear here...
💡 Hint: 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.
🔥 Challenge

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.

Output will appear here...
💡 Hint: When using 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.
🏆 Mini Project — Sales Dashboard (4 Charts)

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

Output will appear here...

Chart 2: Line Chart — Revenue by Month per Rep

Output will appear here...

Chart 3: Boxplot — Revenue Distribution by Region

Output will appear here...

Chart 4: Scatter Plot — Revenue colored by Region

Output will appear here...

✅ 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 →

← Lab 5: Strings Lab 6 of 10 Lab 7: Statistics →