Complete the code to create a basic bar plot using ggplot2 with geom_bar.
library(ggplot2) data <- data.frame(category = c('A', 'B', 'C'), count = c(10, 20, 15)) ggplot(data, aes(x = category)) + geom_bar(stat = [1])
geom_bar by default uses stat = "count" to count the number of cases for each category.
Complete the code to create a bar plot using geom_col with the height of bars from the count column.
library(ggplot2) data <- data.frame(category = c('A', 'B', 'C'), count = c(10, 20, 15)) ggplot(data, aes(x = category, y = [1])) + geom_col()
geom_col uses the y aesthetic to set the height of bars, so we map y to the count column.
Fix the error in the code to correctly plot counts of categories using geom_bar.
library(ggplot2) data <- data.frame(category = c('A', 'B', 'A', 'C', 'B', 'B')) ggplot(data, aes(x = [1])) + geom_bar()
When using geom_bar with stat = "identity", you must provide y values. Here, we want to count categories, so remove stat = "identity" or map x to category and use default stat.
Fill both blanks to create a bar plot with bars colored by category and width set to 0.5.
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])
We fill bars by category and set width to 0.5 for narrower bars.
Fill all three blanks to create a stacked bar plot showing counts by category and subgroup.
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()
To create a stacked bar plot, map x to category, y to count, and fill to subgroup.