Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 即使所有组均为空,也显示空组_R_Ggplot2 - Fatal编程技术网

R 即使所有组均为空,也显示空组

R 即使所有组均为空,也显示空组,r,ggplot2,R,Ggplot2,使用scale\u x\u discrete(drop=FALSE)I设法将空组保留在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)

使用
scale\u x\u discrete(drop=FALSE)
I设法将空组保留在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)
库(ggplot2)
iris(7)
ggplot(数据=虹膜过滤,映射=aes(x=种,y=萼片宽度))+
geom_箱线图()+
比例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)
iris_(8)
ggplot(数据=虹膜过滤,映射=aes(x=种,y=萼片宽度))+
geom_箱线图()+
比例x离散(下降=假)

我希望的产出是:


您可以只指定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))

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))