Reputation: 33508
Using scale_x_discrete(drop = FALSE)
I manage to keep empty groups on its place on the x-axis:
library(ggplot2)
iris_filtered <- subset(iris, Sepal.Length > 7)
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE)
Except when all the groups are empty, I get:
iris_filtered <- subset(iris, Sepal.Length > 8)
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE)
My hoped-for-output is:
Upvotes: 2
Views: 299
Reputation: 3876
You could just specify the x-axis limits:
iris_filtered <- subset(iris, Sepal.Length > 8)
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE, limits = unique((iris$Species))
Similar approach to show the scale of the y-axis:
ggplot(data = iris_filtered, mapping = aes(x = Species, y = Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(drop = FALSE, limits = c("a","b","c")) +
ylim(min(iris$Sepal.Length), max(iris$Sepal.Length))
Upvotes: 5