我想用虹膜数据框做一个盒子图.田间树种由刚毛、杂色、处女草3个等级组成.当我过滤掉setosa时,它会从图表中删除框图,但我希望setosa仍然在x轴上.我怎么能这样做呢?
library(tidyverse)
iris%>%
filter(Species!="setosa")%>%
ggplot(aes(x=Species, y=Sepal.Length,fill=Species)) +
geom_boxplot()
我想用虹膜数据框做一个盒子图.田间树种由刚毛、杂色、处女草3个等级组成.当我过滤掉setosa时,它会从图表中删除框图,但我希望setosa仍然在x轴上.我怎么能这样做呢?
library(tidyverse)
iris%>%
filter(Species!="setosa")%>%
ggplot(aes(x=Species, y=Sepal.Length,fill=Species)) +
geom_boxplot()
将drop=FALSE
与x
和/或fill
相加,如果使用ggplot2&>=3.5.0,则将show.legend=TRUE
与geom_boxplot
相加(否则关键符号将不会显示在fill
图例中):
library(tidyverse)
iris %>%
filter(Species != "setosa") %>%
ggplot(aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot(show.legend = TRUE) +
scale_x_discrete(drop = FALSE) +
scale_fill_discrete(drop = FALSE)