Keep Unused Factor Levels in ggplot2 Barplot in R (Example) | Barchart with Empty Values in Category

preview_player
Показать описание
R code of this video:

y = c(2, 0, 4, 3, 0))
data <- data[data$y > 0, ]
data$x

library("ggplot2") # Load ggplot2 package

ggp <- ggplot(data, aes(x, y, fill = x)) + # Create barchart without 0-levels
geom_bar(stat = "identity")
ggp # Draw barplot

ggp + # Create barchart with empty factors
scale_x_discrete(drop = FALSE)

Follow me on Social Media:
Комментарии
Автор

Hi, thank you! What if I need the opposite? I have a range from 2014 to 2021 in my x-axis, but I don't have values in 2016 and in 2020. However, it keeps ploting empty spaces between the other bars, I'm trying to erase them...

larissacury
welcome to shbcf.ru