简体   繁体   English

R中多个条件的多个箱图

[英]Multiple boxplots for multiple conditions in R

I understood how I have to plot multiple boxplots in one graph from the several others posts. 我明白了如何在其他几个帖子的一个图表中绘制多个箱图。 But I have this situation where am unable to plot multiple conditions together. 但我有这种情况,我无法一起绘制多个条件。 I applied the same idea as my former post ( Multiple boxplots in R ) but does not work for this case. 我应用了与我以前的帖子( R中的多个 )相同的想法,但不适用于这种情况。

I have this dataset 我有这个数据集

     Control                      Treatment
      L1    L2   L3  L4   L5        S1   S2    S3  S4  S5    
g1   10.5    12  10  11   12        13   14    10  11  12 
g2    11     13  10  10   11        10.5 12    8   9   10
g3    10     9   9   8    9         11   10    11  9   11
g4    9      8   8   9    8         6     5    5   7   6
g5    16     4   6.5 6.8  5         4     6    6   8   9
g6    11     12  7.8 7.5  6         5     4    9   10  11
g7    10     6   8.9 6.4  7.2       13    12   12  12  10
g8    5      4   9.0 5.6  7.8       12    12   9   8   7 
g9    11     12  11  8.5  7.4       10    11.5 8   7   6   
g10   8.9    7.8 13  5.6  6.8       7.6   5.8  5   4   5 

And would like to represent several conditions as multiple boxplots in the same graph. 并且希望将多个条件表示为同一图表中的多个箱图。

I would like to make the first plot comparing L1 from control to S1 and S2 in treatment and the second plot comparing L2 and L3 from control to S3, S4, S5 in treatment and a third plot with L4 and L5 comparing to S4 and S5 in treatment. 我想制作第一个图,比较L1从对照到S1和S2的治疗,第二个图比较L2和L3从对照到S3,S4,S5治疗和第三个图与L4和L5比较S4和S5治疗。

How is this multiple condition boxplot possible? 这个多重条件箱图如何可能? Or should I make these boxplots separately and then put them together in the same graph ? 或者我应该单独制作这些箱图,然后将它们放在同一个图表中?

I'm not sure if this is what you are looking for, but it requires a little bit of data manipulation. 我不确定这是否是您正在寻找的,但它需要一些数据操作。

If you want to manually enter your groupings in a fourth column (ie, "Group2") for (L1,S1,S2 | L2,L3,S3,S4,S5 | L4,L5,S4,S5), you will be required to duplicate the S4 & S5 rows and place them in the appropriate group. 如果要在(L1,S1,S2 | L2,L3,S3,S4,S5 | L4,L5,S4,S5)的第四列(即“Group2”)中手动输入分组,则需要进行分组复制S4和S5行并将它们放在适当的组中。 Then you will change: 然后你会改变:

facet_wrap( ~ Group2, scales = 'free')

-- -

library(ggplot2)
library(reshape2)

control <- ## read in control data
control$group <- rep('control', nrow(control))
control <- melt(control, id.vars = 'group')

treatment <- ## read in control data
treatment$group <- rep('treatment', nrow(treatment))
treatment <- melt(treatment, id.vars = 'group')

allData <- rbind(control, treatment)

ggplot(allData, aes(x = variable, y = value, group = variable)) +
  geom_boxplot() +
  facet_wrap( ~ group, scales = 'free')

在此输入图像描述

-- UPDATE -- - 更新 -

library(gdata)
library(reshape2)
library(ggplot2)

control <- ## read in control data
control$group <- rep('control', nrow(control))
control <- melt(control, id.vars = 'group')

treatment <- ## read in treatment data
treatment$group <- rep('treatment', nrow(treatment))
treatment <- melt(treatment, id.vars = 'group')

allData <- rbind(control, treatment)

compA <- subset(allData, 
              variable == 'L1' | 
              variable == 'S1' | 
              variable == 'S2')
compB <- subset(allData, 
              variable == 'L2' | 
              variable == 'L3' | 
              variable == 'S3' | 
              variable == 'S4' | 
              variable == 'S5')
compC <- subset(allData, 
              variable == 'L4' | 
              variable == 'L5' | 
              variable == 'S4' | 
              variable == 'S5')

allData <- combine(compA, compB, compC)

ggplot(allData, aes(x = variable, y = value, group = variable, fill = group)) +
  geom_boxplot() +
  facet_wrap( ~ source, scales = 'free_x')

在此输入图像描述

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

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