简体   繁体   English

即使所有组都是空的,也显示空组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM