📖 Scenario: You are a data analyst working with a dataset of car models and their fuel efficiency. You want to create separate plots for each car manufacturer to compare their fuel efficiency visually.
🎯 Goal: Build a faceted plot using ggplot2 in R that shows fuel efficiency for different car models, separated by manufacturer in subplots.
📋 What You'll Learn
Create a data frame called cars with columns manufacturer, model, and mpg with exact values
Create a ggplot object called p with model on x-axis and mpg on y-axis
Add points to the plot using geom_point()
Use facet_wrap() to create subplots by manufacturer
Print the plot object p
💡 Why This Matters
🌍 Real World
Faceting helps compare groups side-by-side in separate plots, useful in data analysis and reporting.
💼 Career
Data analysts and scientists use faceting to explore and present data clearly across categories.
Progress0 / 4 steps
1
Create the data frame
Create a data frame called cars with these exact columns and values: manufacturer: 'Ford', 'Ford', 'Toyota', 'Toyota', 'Honda', 'Honda' model: 'F-150', 'Mustang', 'Corolla', 'Camry', 'Civic', 'Accord' mpg: 20, 25, 30, 28, 32, 31
R Programming
Hint
Use data.frame() with vectors for each column named exactly manufacturer, model, and mpg.
2
Load ggplot2 library
Load the ggplot2 library by writing library(ggplot2)
R Programming
Hint
Type library(ggplot2) to load the plotting package.
3
Create the faceted plot
Create a ggplot object called p using cars data, mapping model to x-axis and mpg to y-axis, add points with geom_point(), and use facet_wrap(~manufacturer) to create subplots by manufacturer
R Programming
Hint
Use ggplot() with aes(x = model, y = mpg), add geom_point(), then add facet_wrap(~manufacturer).
4
Display the faceted plot
Print the plot object p using print(p)
R Programming
Hint
Use print(p) to display the plot.
Practice
(1/5)
1. What is the main purpose of faceting in R plotting with ggplot2?
easy
A. To save the plot as an image file
B. To change the color of points in a plot
C. To add titles to the plot
D. To split one plot into multiple smaller plots based on categories
Solution
Step 1: Understand faceting concept
Faceting divides a single plot into multiple smaller plots based on categories in the data.
Step 2: Identify purpose in ggplot2
In ggplot2, faceting helps compare groups by creating subplots for each category.
Final Answer:
To split one plot into multiple smaller plots based on categories -> Option D
Quick Check:
Faceting = split plot by category [OK]
Hint: Faceting means splitting plots by groups [OK]
Common Mistakes:
Confusing faceting with coloring points
Thinking faceting adds titles
Believing faceting saves files
2. Which of the following is the correct syntax to create subplots by one variable using facet_wrap in ggplot2?
easy
A. ggplot(data) + geom_point() + facet_wrap(~ variable)
B. ggplot(data) + geom_point() + facet_grid(variable)
C. ggplot(data) + geom_point() + facet_wrap(variable)
D. ggplot(data) + geom_point() + facet_grid(~ variable)
Solution
Step 1: Recall syntax for facet_wrap
facet_wrap requires a formula with tilde (~) and the variable name, like ~ variable.
Step 2: Check options
ggplot(data) + geom_point() + facet_wrap(~ variable) uses facet_wrap(~ variable), which is correct syntax for faceting by one variable.
Final Answer:
ggplot(data) + geom_point() + facet_wrap(~ variable) -> Option A