问:在R中的单个图中显示分组和组合的箱图

时间:2022-11-16 23:40:03

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

问:在R中的单个图中显示分组和组合的箱图

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


2  

Yes, you can just create a column for all species as follows:

是的,您可以为所有物种创建一个列,如下所示:

iris = iris %>% mutate(all = "All Species")
p1 <- ggplot(iris) + 
  geom_boxplot(aes(x=Species, y=Sepal.Length)) +
  geom_boxplot(aes(x=all, y=Sepal.Length))
p1

问:在R中的单个图中显示分组和组合的箱图

#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


2  

Yes, you can just create a column for all species as follows:

是的,您可以为所有物种创建一个列,如下所示:

iris = iris %>% mutate(all = "All Species")
p1 <- ggplot(iris) + 
  geom_boxplot(aes(x=Species, y=Sepal.Length)) +
  geom_boxplot(aes(x=all, y=Sepal.Length))
p1

问:在R中的单个图中显示分组和组合的箱图