Challenge - 5 Problems
Grammar of Graphics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this ggplot2 code?
Consider the following R code using ggplot2. What will be the output when this code runs?
R Programming
library(ggplot2) data <- data.frame(x = 1:3, y = c(3, 2, 1)) ggplot(data, aes(x = x, y = y)) + geom_point() + geom_line()
Attempts:
2 left
💡 Hint
Think about what geom_point() and geom_line() do in ggplot2.
✗ Incorrect
The code creates a scatter plot with points at (1,3), (2,2), and (3,1). The geom_line() connects these points in order of x, forming a line.
🧠 Conceptual
intermediate1:30remaining
Which component of Grammar of Graphics defines how data variables map to visual properties?
In the Grammar of Graphics, which component describes the mapping of data variables to visual aesthetics like color, size, or position?
Attempts:
2 left
💡 Hint
Think about what controls the look of points or lines based on data.
✗ Incorrect
Aesthetics define how data variables are mapped to visual properties such as color, size, shape, or position.
🔧 Debug
advanced2:00remaining
What error does this ggplot2 code produce?
Examine the code below. What error will it raise when run?
R Programming
library(ggplot2) data <- data.frame(x = 1:5, y = c(2, 4, 6, 8, 10)) ggplot(data, aes(x = x, y = y)) + geom_point() + geom_line(aes(color = z))
Attempts:
2 left
💡 Hint
Check if all variables used in aes() exist in the data frame.
✗ Incorrect
The variable 'z' is not defined in the data frame, so ggplot2 throws an error that object 'z' is not found.
📝 Syntax
advanced1:30remaining
Which option correctly adds a title to a ggplot?
Which of the following R code snippets correctly adds the title 'My Plot' to a ggplot object named p?
Attempts:
2 left
💡 Hint
ggplot2 uses specific functions to add titles and labels.
✗ Incorrect
The function ggtitle() adds a title to a ggplot. labs() can also add titles but requires the argument name 'title'.
🚀 Application
expert2:30remaining
How many layers does this ggplot have?
Given the code below, how many layers are added to the ggplot object?
R Programming
library(ggplot2) data <- data.frame(x = 1:4, y = c(4, 3, 2, 1)) p <- ggplot(data, aes(x, y)) + geom_point() + geom_smooth(method = 'lm') + facet_wrap(~x)
Attempts:
2 left
💡 Hint
Count only the layers that add visual elements, not facets.
✗ Incorrect
geom_point() and geom_smooth() each add one layer. facet_wrap() adds facets but does not add a layer. So total layers = 2.