0
0
R Programmingprogramming~20 mins

ggplot() and aes() basics in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ggplot2 Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this ggplot code?

Consider the following R code using ggplot2. What will be the main visual element shown?

R Programming
library(ggplot2)
data <- data.frame(x = 1:5, y = c(3, 5, 2, 8, 7))
ggplot(data, aes(x = x, y = y)) + geom_point()
AA bar chart with bars at x positions 1 to 5 with heights 3 to 7
BA line plot connecting points (1,3), (2,5), (3,2), (4,8), (5,7)
CA scatter plot with points at coordinates (1,3), (2,5), (3,2), (4,8), (5,7)
DAn empty plot with no points or lines
Attempts:
2 left
💡 Hint

Look at the geom_point() function. It adds points to the plot.

Predict Output
intermediate
2:00remaining
What happens if you swap x and y in aes()?

Given the same data, what will this code produce?

R Programming
library(ggplot2)
data <- data.frame(x = 1:5, y = c(3, 5, 2, 8, 7))
ggplot(data, aes(x = y, y = x)) + geom_point()
AA scatter plot with points at coordinates (3,1), (5,2), (2,3), (8,4), (7,5)
BAn error because x and y are swapped
CA line plot connecting points (3,1), (5,2), (2,3), (8,4), (7,5)
DA scatter plot with points at coordinates (1,3), (2,5), (3,2), (4,8), (5,7)
Attempts:
2 left
💡 Hint

Swapping x and y in aes() changes the axes.

🧠 Conceptual
advanced
2:00remaining
Which option correctly maps color to a variable?

In ggplot2, how do you map the color of points to a variable group in the data?

Aggplot(data, aes(x = x, y = y)) + geom_point(aes(color = group))
Bggplot(data, aes(x = x, y = y)) + geom_point(color = group)
Cggplot(data, aes(x = x, y = y, color = group)) + geom_point()
Dggplot(data, aes(x = x, y = y)) + geom_point(color = "group")
Attempts:
2 left
💡 Hint

Mapping aesthetics inside aes() links them to data variables.

🔧 Debug
advanced
2:00remaining
Why does this ggplot code produce an error?

Look at this code snippet. Why does it cause an error?

R Programming
library(ggplot2)
data <- data.frame(x = 1:3, y = c(4, 5, 6))
ggplot(data, aes(x = x, y = y)) + geom_point(aes(color = z))
ABecause variable 'z' does not exist in the data frame
BBecause color cannot be mapped inside geom_point()
CBecause aes() must be outside ggplot()
DBecause geom_point() requires a size aesthetic
Attempts:
2 left
💡 Hint

Check if all variables used in aes() exist in the data.

Predict Output
expert
2:00remaining
How many points will be plotted here?

Given this code, how many points will appear in the plot?

R Programming
library(ggplot2)
data <- data.frame(x = c(1, 2, 2, 3), y = c(4, 5, 5, 6))
ggplot(data, aes(x = x, y = y)) + geom_point()
A3 points, duplicates are automatically removed
BAn error due to duplicate points
C5 points, because ggplot adds one extra point
D4 points, including duplicates at (2,5)
Attempts:
2 left
💡 Hint

ggplot2 plots all rows; duplicates are not removed automatically.