0
0
R Programmingprogramming~20 mins

Bar plots (geom_bar, geom_col) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bar Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA bar plot with bars showing counts: A=3, B=2, C=1
BA bar plot with bars showing sum of category values
CA scatter plot with points at categories A, B, C
DAn error because y aesthetic is missing
Attempts:
2 left
💡 Hint
geom_bar by default counts the number of occurrences of each x value.
Predict Output
intermediate
2: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()
AA bar plot with bars heights exactly 5, 3, and 8 for A, B, and C
BA bar plot counting occurrences of each category ignoring value
CA line plot connecting points (A,5), (B,3), (C,8)
DAn error because geom_col requires stat='count'
Attempts:
2 left
💡 Hint
geom_col uses the y aesthetic as the bar height directly.
🔧 Debug
advanced
2: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()
APlot shows bars with heights 2, 4, and 6.
BError: stat_count() must not be used with a y aesthetic.
CError: object 'value' not found.
DNo error; bars show counts ignoring y values.
Attempts:
2 left
💡 Hint
geom_bar by default uses stat='count' which does not accept y values.
🧠 Conceptual
advanced
2:00remaining
When to use geom_col instead of geom_bar
Which situation best describes when geom_col() should be used instead of geom_bar()?
AWhen you want to count the number of occurrences of each category.
BWhen you want to create a scatter plot with bars.
CWhen you want to plot pre-summarized values as bar heights.
DWhen you want to plot a line graph connecting categories.
Attempts:
2 left
💡 Hint
Think about whether your data already has the values to plot or if you want to count.
Predict Output
expert
3: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')
AA line plot connecting values for each group across categories
BA stacked bar plot with bars for G1 and G2 stacked in each category
CAn error because position='dodge' is invalid
DA grouped bar plot with side-by-side bars for G1 and G2 in each category with heights from value
Attempts:
2 left
💡 Hint
position='dodge' places bars side by side instead of stacking.