Challenge - 5 Problems
Faceting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of basic facet_wrap usage
What will be the output of this R code using ggplot2's
facet_wrap?R Programming
library(ggplot2) data <- data.frame(x = 1:6, y = c(2,3,5,7,11,13), group = rep(c('A','B'), each=3)) ggplot(data, aes(x, y)) + geom_point() + facet_wrap(~group)
Attempts:
2 left
💡 Hint
facet_wrap splits data into multiple plots based on a factor variable.
✗ Incorrect
facet_wrap(~group) creates separate plots for each unique value in 'group'. Since 'group' has 'A' and 'B', two plots appear side by side.
❓ Predict Output
intermediate2:00remaining
Effect of facet_grid on plot layout
What is the layout of plots produced by this code?
R Programming
library(ggplot2) data <- data.frame(x = 1:4, y = c(10,20,30,40), type = c('X','X','Y','Y'), category = c('M','N','M','N')) ggplot(data, aes(x, y)) + geom_point() + facet_grid(type ~ category)
Attempts:
2 left
💡 Hint
facet_grid uses two variables to create a grid of plots.
✗ Incorrect
facet_grid(type ~ category) creates a grid with rows for each 'type' and columns for each 'category', resulting in 4 plots arranged 2 by 2.
🔧 Debug
advanced2:00remaining
Identify the error in facet_wrap usage
This code produces an error. What is the cause?
R Programming
library(ggplot2) data <- data.frame(x = 1:5, y = c(5,4,3,2,1), group = c('A','B','C','D','E')) ggplot(data, aes(x, y)) + geom_point() + facet_wrap(group)
Attempts:
2 left
💡 Hint
Check how facet_wrap expects its argument.
✗ Incorrect
facet_wrap expects a formula like ~group, not just the variable name.
❓ Predict Output
advanced2:00remaining
Number of plots created by facet_grid
How many individual plots will this code create?
R Programming
library(ggplot2) data <- data.frame(x = 1:8, y = rnorm(8), type = rep(c('A','B'), 4), category = rep(c('X','Y'), each=4)) ggplot(data, aes(x, y)) + geom_point() + facet_grid(type ~ category)
Attempts:
2 left
💡 Hint
Count unique values in 'type' and 'category'.
✗ Incorrect
There are 2 unique 'type' values and 2 unique 'category' values, so 2x2=4 plots are created.
🧠 Conceptual
expert2:00remaining
Understanding free scales in faceting
What is the effect of setting
scales = 'free' in facet_wrap or facet_grid?Attempts:
2 left
💡 Hint
Think about axis scaling flexibility across subplots.
✗ Incorrect
Setting scales = 'free' allows each subplot to adjust its axis scales independently, useful when data ranges differ.