Building a Simple Plot Using Grammar of Graphics in R
📖 Scenario: You are a data analyst who wants to visualize the relationship between two variables in a small dataset. You will use the Grammar of Graphics concept to build a plot step-by-step in R.
🎯 Goal: Create a scatter plot using the Grammar of Graphics approach with ggplot2 in R. You will start by setting up the data, then configure the plot, add points, and finally display the plot.
📋 What You'll Learn
Create a data frame called data with exact columns height and weight and given values
Create a ggplot object called plot with data and aesthetic mapping for height and weight
Add a geom_point() layer to plot to show points
Print the plot object to display the scatter plot
💡 Why This Matters
🌍 Real World
Data analysts and scientists use the Grammar of Graphics to build clear and customizable visualizations to understand data patterns.
💼 Career
Knowing how to use ggplot2 and the Grammar of Graphics is essential for roles in data analysis, data science, and reporting.
Progress0 / 4 steps
1
DATA SETUP: Create the data frame
Create a data frame called data with two columns: height containing values 150, 160, 170, 180, 190 and weight containing values 65, 70, 75, 80, 85.
R Programming
Hint
Use data.frame() with height = c(...) and weight = c(...) to create the data.
2
CONFIGURATION: Create the ggplot object
Create a ggplot object called plot using the data data frame, and map height to the x-axis and weight to the y-axis using aes().
R Programming
Hint
Use ggplot(data, aes(x = height, y = weight)) to create the plot object.
3
CORE LOGIC: Add points layer
Add a points layer to the plot object by adding geom_point().
R Programming
Hint
Add + geom_point() to the ggplot object to show points.
4
OUTPUT: Display the plot
Print the plot object to display the scatter plot.
R Programming
Hint
Use print(plot) to show the plot in R.
Practice
(1/5)
1. In the Grammar of Graphics, which component defines how data variables are mapped to visual properties like color or size in a plot?
easy
A. Data
B. Aesthetics
C. Layers
D. Facets
Solution
Step 1: Understand the role of aesthetics
Aesthetics specify how data variables correspond to visual elements such as color, size, or shape.
Step 2: Differentiate from other components
Data is the raw information, layers are the graphical elements, and facets split plots; only aesthetics map variables visually.
Final Answer:
Aesthetics -> Option B
Quick Check:
Mapping variables to visuals = Aesthetics [OK]
Hint: Remember: aesthetics = visual mapping of data [OK]
Common Mistakes:
Confusing data with aesthetics
Thinking layers define variable mapping
Mixing facets with aesthetics
2. Which of the following is the correct way to start a ggplot object in R using the Grammar of Graphics?
easy
A. ggplot(aes(x = var1, y = var2), data = mydata)
B. plot(data = mydata, aes(x = var1, y = var2))
C. ggplot(data = mydata, aes(x = var1, y = var2))
D. ggplot(mydata, x = var1, y = var2)
Solution
Step 1: Recall ggplot syntax
The correct syntax is ggplot(data = ..., aes(...)) where data is first, then aesthetics.
Step 2: Check each option
ggplot(data = mydata, aes(x = var1, y = var2)) matches the correct syntax; using plot() refers to base R graphics, not ggplot2; swapping data and aes() arguments assigns them incorrectly due to positional matching; omitting aes() fails to map variables to aesthetics.
Final Answer:
ggplot(data = mydata, aes(x = var1, y = var2)) -> Option C
To map a variable to color, it must be inside aes(), not as a fixed argument.
Step 2: Analyze the code
color = var3 is outside aes(), so ggplot treats it as a fixed color, causing an error or wrong plot.
Final Answer:
color should be inside aes() to map to var3 -> Option A
Quick Check:
Variable color mapping requires aes() [OK]
Hint: Put variable color inside aes(), fixed color outside [OK]
Common Mistakes:
Placing variable color outside aes()
Thinking geom_point() disallows color
Omitting data argument in ggplot()
5. You want to create a layered plot in ggplot2 showing points and a smooth trend line for data frame df with variables x and y. Which code correctly applies the Grammar of Graphics to achieve this?
hard
A. ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth()
B. ggplot(df) + geom_point(aes(x = x, y = y)) + geom_smooth()
C. ggplot() + geom_point(df, aes(x = x, y = y)) + geom_smooth(df, aes(x = x, y = y))
D. ggplot(df, aes(x = x)) + geom_point() + geom_smooth()
Solution
Step 1: Understand layering in ggplot2
Layers add graphical elements; setting data and aesthetics once in ggplot() is best practice.
Step 2: Evaluate each option
ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth() sets data and aesthetics once, then adds point and smooth layers correctly. Specifying aes(x = x) globally without y in layers causes missing y aesthetic error. Starting with bare ggplot(df) and aes only in first geom leaves smooth without mappings. Passing df directly to geoms after empty ggplot() misuses argument positions.
Final Answer:
ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth() -> Option A
Quick Check:
Layered plot with shared aes() = ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth() [OK]
Hint: Set data and aes() once, then add layers [OK]
Common Mistakes:
Incomplete aesthetics when splitting across layers
Omitting aes() in later layers
Passing data inside geom_ functions incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.