I am trying to display grouped boxplot and combined boxplot into one plot. Take the iris
data for instance:
我试图将分组的boxplot和组合的boxplot显示在一个图中。以虹膜数据为例:
data(iris)
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()
p1
I am trying to compare overall distribution with distributions within each categories. So is there a way to display a boxplot of all samples on the left of these three grouped boxplots?
我试图将整体分布与每个类别中的分布进行比较。那么有没有办法在这三个分组的箱形图左侧显示所有样本的箱线图?
Thanks in advance.
提前致谢。
2 个解决方案
#1
2
You can rbind
a new version of iris
, where Species
equals "All"
for all rows, to iris
before piping to ggplot
在管道到ggplot之前,您可以将新版本的虹膜(其中Species等于所有行的“全部”)转换为虹膜
p1 <- iris %>%
rbind(iris %>% mutate(Species = 'All')) %>%
ggplot(aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
#2
#1
2
You can rbind
a new version of iris
, where Species
equals "All"
for all rows, to iris
before piping to ggplot
在管道到ggplot之前,您可以将新版本的虹膜(其中Species等于所有行的“全部”)转换为虹膜
p1 <- iris %>%
rbind(iris %>% mutate(Species = 'All')) %>%
ggplot(aes(x = Species, y = Sepal.Length)) +
geom_boxplot()