简体   繁体   English

R 多个箱线图在一个 plot

[英]R multiple boxplots in one plot

I have a question regarding multiple boxplots.我对多个箱线图有疑问。 Assume we have data structures like this:假设我们有这样的数据结构:

a <- rnorm(100, 0, 1)
b <- rnorm(100, 0, 1)
c <- rbinom(100, 1, 0.5)

My task is to create a boxplot of a and b for each group of c.我的任务是为每组 c 创建 a 和 b 的箱线图。 However, it needs to be in the same plot.但是,它需要在同一个 plot 中。 Ideally: Boxplot for a and b side by side for group 0 and next to it boxplot for a and b for group 1 and all together in one graphic.理想情况下:a 和 b 的箱线图并排显示第 0 组,并在其旁边显示第 1 组的 a 和 b 箱线图,所有这些都在一个图形中。

I tried several things, but only the seperate plots are working:我尝试了几件事,但只有单独的情节有效:

boxplot(a~as.factor(c))
boxplot(b~as.factor(c))

But actually, that's not what I'm searching for.但实际上,这不是我要寻找的。 As it has to be one plot.因为它必须是一个 plot。

You can use the tidyverse package for this.您可以为此使用tidyverse package。 You transform your data into long-format that you get three variables: "names", "values" and "group".您将数据转换为长格式,您将获得三个变量:“名称”、“值”和“组”。 After that you can plot your boxplots with ggplot() :之后,您可以使用ggplot() plot 您的箱线图:

value_a <- rnorm(100, 0, 1)
value_b <- rnorm(100, 0, 1)
group <- as.factor(rbinom(100, 1, 0.5))

data <- data.frame(value_a,value_b,group)

library(tidyverse)

data %>%
  pivot_longer(value_a:value_b, names_to = "names", values_to = "values") %>%
  ggplot(aes(y = values, x = group, fill = names))+
  geom_boxplot()

Created on 2022-08-19 with reprex v2.0.2使用reprex v2.0.2创建于 2022-08-19

Noah has already given the ggplot2 answer that would also be my go to option.诺亚已经给出了ggplot2答案,这也将是我的 go 选项。 As you used the boxplot function in the question, this is how to approach it with boxplot .当您在问题中使用boxplot function 时,这是使用boxplot处理它的方法。 You should probably stay consistently within base or within ggplot2 for your publication/presentation.您可能应该始终保持在baseggplot2内以进行发布/演示。

First we transform the data to a long format (here an option without additional packages):首先,我们将数据转换为长格式(这里是一个没有附加包的选项):

a <- rnorm(100, 0, 1)
b <- rnorm(100, 0, 1)
c <- rbinom(100, 1, 0.5)
d <- data.frame(a, b, c)
d <- cbind(stack(d, select = c("a", "b")), c)

giving us给我们

> head(d)
       values ind c
1 -0.66905293   a 0
2 -0.28778381   a 0
3  0.29148347   a 1
4  0.81380406   a 0
5 -0.85681913   a 0
6 -0.02566758   a 0

With which we can then call boxplot :然后我们可以使用它调用boxplot

boxplot(values ~ ind + c, data = d, at = c(1, 2, 4, 5))

带有基本图形的分组箱线图

The at argument controls the grouping and placement of the boxes. at参数控制框的分组和放置。 Contrary to ggplot2 you need to choose placing manually, but you also get very fine control of spacing very easily.ggplot2 ,您需要手动选择放置,但您也可以非常轻松地控制间距。

Slightly refined version of the plot: plot 的略微改进版:

boxplot(values ~ ind + c, data = d, at = c(1, 2, 4, 5),
        col = c(2, 4), show.names = FALSE,
        xlab = "")
axis(1, labels= c("c = 0", "c = 1"), at = c(1.5, 4.5))
legend("topright", fill = c(2, 4), legend = c("a", "b"))

在此处输入图像描述

Another option using lattice package with bwplot function:使用带有bwplot function 的格子package 的另一个选项:

library(tidyr)

a <- rnorm(100, 0, 1)
b <- rnorm(100, 0, 1)
c <- rbinom(100, 1, 0.5)

df <- data.frame(a = a,
                 b = b,
                 c = c)

# make longer dataframe
df_long <- pivot_longer(df, cols = -c)

library(lattice)
bwplot(value ~ name | as.factor(c), df_long)

Created on 2022-08-19 with reprex v2.0.2使用reprex v2.0.2创建于 2022-08-19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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