0
0
R Programmingprogramming~30 mins

Faceting for subplots in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Faceting for Subplots in R with ggplot2
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(p) to display the plot.