[英]Show empty groups even when all groups are empty
Using scale_x_discrete(drop = FALSE)
I manage to keep empty groups on its place on the x-axis:使用
scale_x_discrete(drop = FALSE)
我设法将空组保持在 x 轴上的位置:
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:我希望的输出是:
You could just specify the x-axis limits:您可以只指定 x 轴范围:
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:显示 y 轴比例的类似方法:
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.