0
0
R Programmingprogramming~10 mins

Bar plots (geom_bar, geom_col) in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic bar plot using ggplot2 with geom_bar.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), count = c(10, 20, 15))
ggplot(data, aes(x = category)) + geom_bar(stat = [1])
Drag options to blanks, or click blank then click option'
A"identity"
B"mean"
C"sum"
D"count"
Attempts:
3 left
💡 Hint
Common Mistakes
Using stat = "identity" when data does not have y values.
2fill in blank
medium

Complete the code to create a bar plot using geom_col with the height of bars from the count column.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), count = c(10, 20, 15))
ggplot(data, aes(x = category, y = [1])) + geom_col()
Drag options to blanks, or click blank then click option'
Acount
Bcategory
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Mapping x to y aesthetic instead of count.
3fill in blank
hard

Fix the error in the code to correctly plot counts of categories using geom_bar.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'A', 'C', 'B', 'B'))
ggplot(data, aes(x = [1])) + geom_bar()
Drag options to blanks, or click blank then click option'
Acount
Bcategory
Cvalue
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using stat = "identity" without y aesthetic.
4fill in blank
hard

Fill both blanks to create a bar plot with bars colored by category and width set to 0.5.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), count = c(5, 10, 7))
ggplot(data, aes(x = category, y = count, fill = [1])) + geom_col(width = [2])
Drag options to blanks, or click blank then click option'
Acategory
Bcount
C0.5
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using count for fill aesthetic.
Setting width to 1 when asked for 0.5.
5fill in blank
hard

Fill all three blanks to create a stacked bar plot showing counts by category and subgroup.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'A', 'B', 'B'), subgroup = c('X', 'Y', 'X', 'Y'), count = c(3, 2, 5, 4))
ggplot(data, aes(x = [1], y = [2], fill = [3])) + geom_col()
Drag options to blanks, or click blank then click option'
Acategory
Bcount
Csubgroup
Dgroup
Attempts:
3 left
💡 Hint
Common Mistakes
Using group instead of subgroup for fill.
Mapping y to subgroup instead of count.