📖 Scenario: You are a data analyst working with a small dataset of car speeds and stopping distances. You want to visualize the relationship between speed and distance using a scatter plot.
🎯 Goal: Create a scatter plot using geom_point in ggplot2 to show how car speed relates to stopping distance.
📋 What You'll Learn
Create a data frame called cars_data with columns speed and dist using the exact values provided.
Create a variable called plot_title with the exact string 'Speed vs Stopping Distance'.
Use ggplot with geom_point to create a scatter plot of speed on the x-axis and dist on the y-axis.
Add the title stored in plot_title to the plot using ggtitle().
Print the plot object to display the scatter plot.
💡 Why This Matters
🌍 Real World
Scatter plots help visualize relationships between two numeric variables, such as speed and stopping distance in cars.
💼 Career
Data analysts and scientists use scatter plots to explore data patterns and communicate findings clearly.
Progress0 / 4 steps
1
Create the data frame
Create a data frame called cars_data with two columns: speed containing the values 4, 7, 8, 9, 10 and dist containing the values 2, 10, 15, 20, 25.
R Programming
Hint
Use data.frame() with speed = c(...) and dist = c(...) to create the data frame.
2
Add a plot title variable
Create a variable called plot_title and assign it the string 'Speed vs Stopping Distance'.
R Programming
Hint
Assign the exact string to plot_title using the assignment operator <-.
3
Create the scatter plot
Use ggplot with the data frame cars_data. Map speed to the x-axis and dist to the y-axis. Add geom_point() to create the scatter plot points.
R Programming
Hint
Use ggplot(data, aes(x = ..., y = ...)) + geom_point() to create the scatter plot.
4
Add title and display the plot
Add the title stored in plot_title to the plot using ggtitle(plot_title). Then print the plot object to display the scatter plot.
R Programming
Hint
Use plot + ggtitle(plot_title) to add the title, then print(plot) to show the plot.
Practice
(1/5)
1. What does the geom_point() function do in ggplot2?
easy
A. It adds a line connecting data points.
B. It creates a bar chart with grouped bars.
C. It creates a scatter plot showing points for data pairs.
D. It summarizes data with a boxplot.
Solution
Step 1: Understand the purpose of geom_point()
The function geom_point() is used in ggplot2 to add points to a plot, which is the basis of a scatter plot.
Step 2: Identify the plot type created
Scatter plots show the relationship between two numeric variables using dots, which matches what geom_point() does.
Final Answer:
It creates a scatter plot showing points for data pairs. -> Option C
Quick Check:
geom_point() = scatter plot points [OK]
Hint: Remember: geom_point() means dots on a scatter plot [OK]
Common Mistakes:
Confusing geom_point() with geom_line()
Thinking geom_point() creates bar charts
Mixing up scatter plots with boxplots
2. Which of the following is the correct syntax to create a scatter plot with height on the x-axis and weight on the y-axis using ggplot2?
easy
A. ggplot(data) + geom_point(height, weight)
B. ggplot(data) + geom_point(aes(x = weight, y = height))
C. ggplot(data) + geom_point(x = height, y = weight)
D. ggplot(data) + geom_point(aes(x = height, y = weight))
Solution
Step 1: Check the correct use of aes() inside geom_point()
The aesthetics mapping aes() must specify which variables go on x and y axes inside geom_point().
Step 2: Verify the order of variables
The question asks for height on x-axis and weight on y-axis, so aes(x = height, y = weight) is correct.
Final Answer:
ggplot(data) + geom_point(aes(x = height, y = weight)) -> Option D
Hint: Always put aes(x=, y=) inside geom_point() [OK]
Common Mistakes:
Swapping x and y variables
Passing variables directly to geom_point() without aes()
Omitting aes() entirely
3. What will be the output of this R code?
library(ggplot2)
data <- data.frame(x = 1:3, y = c(4, 5, 6))
ggplot(data) + geom_point(aes(x = x, y = y))
medium
A. A scatter plot with points at (1,4), (2,5), and (3,6).
B. A line plot connecting points (1,4), (2,5), and (3,6).
C. An error because aes() is missing.
D. A bar chart showing counts of x.
Solution
Step 1: Understand the data and mapping
The data frame has x values 1, 2, 3 and y values 4, 5, 6. The aes() maps x to x-axis and y to y-axis.
Step 2: Identify the plot type from geom_point()
geom_point() creates a scatter plot with points at the specified coordinates.
Final Answer:
A scatter plot with points at (1,4), (2,5), and (3,6). -> Option A
Quick Check:
geom_point() plots points at (x,y) pairs [OK]
Hint: geom_point() plots points exactly at aes(x, y) coordinates [OK]
Common Mistakes:
Expecting a line plot instead of points
Thinking aes() is missing causes error here
Confusing bar chart with scatter plot
4. Identify the error in this code snippet:
ggplot(data) + geom_point(x = height, y = weight)
medium
A. Using ggplot() without data argument.
B. Missing aes() around x and y inside geom_point().
C. geom_point() cannot plot numeric data.
D. x and y should be swapped.
Solution
Step 1: Check how aesthetics are passed to geom_point()
In ggplot2, x and y mappings must be inside aes() to link variables to axes.
Step 2: Identify the missing aes() call
The code passes x and y directly to geom_point(), which is incorrect syntax.
Final Answer:
Missing aes() around x and y inside geom_point(). -> Option B
Quick Check:
Always use aes(x=, y=) inside geom_point() [OK]
Hint: Always wrap x and y in aes() inside geom_point() [OK]
Common Mistakes:
Passing x and y directly without aes()
Forgetting to provide data to ggplot()
Swapping x and y unnecessarily
5. You want to create a scatter plot of mpg vs hp from the built-in mtcars dataset, but only for cars with cyl equal to 6. Which code correctly does this?
hard
A. ggplot(subset(mtcars, cyl == 6)) + geom_point(aes(x = hp, y = mpg))
B. ggplot(mtcars) + geom_point(aes(x = hp, y = mpg), subset = cyl == 6)
C. ggplot(mtcars) + geom_point(aes(x = hp, y = mpg)) + filter(cyl == 6)
D. ggplot(mtcars) + geom_point(aes(x = hp, y = mpg)) + subset(cyl == 6)
Solution
Step 1: Filter data before plotting
To plot only cars with 6 cylinders, filter the data frame using subset(mtcars, cyl == 6).
Step 2: Use filtered data in ggplot()
Pass the filtered data to ggplot(), then add geom_point() with correct aesthetics.
Final Answer:
ggplot(subset(mtcars, cyl == 6)) + geom_point(aes(x = hp, y = mpg)) -> Option A
Quick Check:
Filter data first, then plot with geom_point() [OK]
Hint: Filter data before ggplot() to plot subsets [OK]
Common Mistakes:
Trying to filter inside geom_point() which is invalid
Using filter() without dplyr or outside ggplot()
Passing subset as argument to geom_point()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.