Challenge - 5 Problems
Bar Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of geom_bar with default stat
What is the output of this R code using ggplot2?
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'A', 'C', 'B', 'A'))
ggplot(data, aes(x = category)) + geom_bar()R Programming
library(ggplot2) data <- data.frame(category = c('A', 'B', 'A', 'C', 'B', 'A')) ggplot(data, aes(x = category)) + geom_bar()
Attempts:
2 left
💡 Hint
geom_bar by default counts the number of occurrences of each x value.
✗ Incorrect
geom_bar() without specifying y uses stat='count' which counts how many times each x value appears. Here, 'A' appears 3 times, 'B' 2 times, and 'C' once.
❓ Predict Output
intermediate2:00remaining
Difference between geom_bar and geom_col
Given this data and code, what does the plot show?
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), value = c(5, 3, 8))
ggplot(data, aes(x = category, y = value)) + geom_col()R Programming
library(ggplot2) data <- data.frame(category = c('A', 'B', 'C'), value = c(5, 3, 8)) ggplot(data, aes(x = category, y = value)) + geom_col()
Attempts:
2 left
💡 Hint
geom_col uses the y aesthetic as the bar height directly.
✗ Incorrect
geom_col() uses the y values directly for bar heights, unlike geom_bar() which counts occurrences by default.
🔧 Debug
advanced2:00remaining
Error in geom_bar with y aesthetic
What error does this code produce and why?
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), value = c(2, 4, 6))
ggplot(data, aes(x = category, y = value)) + geom_bar()R Programming
library(ggplot2) data <- data.frame(category = c('A', 'B', 'C'), value = c(2, 4, 6)) ggplot(data, aes(x = category, y = value)) + geom_bar()
Attempts:
2 left
💡 Hint
geom_bar by default uses stat='count' which does not accept y values.
✗ Incorrect
geom_bar() with default stat='count' cannot use y aesthetic. To use y, you must use geom_col() or specify stat='identity'.
🧠 Conceptual
advanced2:00remaining
When to use geom_col instead of geom_bar
Which situation best describes when geom_col() should be used instead of geom_bar()?
Attempts:
2 left
💡 Hint
Think about whether your data already has the values to plot or if you want to count.
✗ Incorrect
geom_col() uses the y values directly as bar heights, so it is used when data already contains summarized values. geom_bar() counts occurrences by default.
❓ Predict Output
expert3:00remaining
Bar plot with grouped data and position dodge
What does this code produce?
library(ggplot2)
data <- data.frame(
group = rep(c('G1', 'G2'), each = 3),
category = rep(c('A', 'B', 'C'), 2),
value = c(2, 5, 3, 4, 1, 6)
)
ggplot(data, aes(x = category, y = value, fill = group)) + geom_col(position = 'dodge')R Programming
library(ggplot2) data <- data.frame( group = rep(c('G1', 'G2'), each = 3), category = rep(c('A', 'B', 'C'), 2), value = c(2, 5, 3, 4, 1, 6) ) ggplot(data, aes(x = category, y = value, fill = group)) + geom_col(position = 'dodge')
Attempts:
2 left
💡 Hint
position='dodge' places bars side by side instead of stacking.
✗ Incorrect
geom_col() with position='dodge' creates grouped bars side by side for each fill group within each x category.