R中多个条件的多个箱形图 [英] Multiple boxplots for multiple conditions in R

查看:912
本文介绍了R中多个条件的多个箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白我是如何从其他几个帖子在一张图中绘制多个箱型图的。但是我有这种情况,我们无法一起绘制多个条件。我应用了与我以前的帖子相同的想法( R中的多个箱型),但不适用于

我有这个数据集

pre $ Control $ b $ Control
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

并且想在同一个图表中将多个条件表示为多个箱形图。



I想要比较治疗中第一张比较从对照的L1到S1和S2的比较,并且第二比较比较治疗中的对照和S3,S4,S5的L2和L3以及与治疗中的S4和S5相比较的第三比较,L4和L5比较。



这种多重条件boxplot可能如何?或者,我应该分别制作这些箱柜图案,然后将它们放在同一个图表中?解析方案

我不确定这是不是你需要什么,但它需要一点数据操作。



如果你想在第四列(即Group2)中手动输入你的分组,对于(L1,S1,S2 | L2,L3,S3,S4,S5 | L4,L5,S4,S5),您需要复制S4& S5行并将它们放置在适当的组中。然后你会改变:

$ $ $ $ $ $ $ $ $ $ c $ facet_wrap(〜Group2,scales ='free')

-

  library(ggplot2)
library(reshape2)

control < - ##读入控制数据
control $ group <-rep('control',nrow(control))
(控制,id.vars ='组')

处理< - ##读入控制数据
处理$ group <-rep('treatment', (治疗))
治疗< - 融合(治疗,id.vars ='组')

allData< - rbind(控制,治疗)

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



- UPDATE -

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

控制< - ##读取控制数据
control $ group < - rep('control',nrow(control))
control < - melt(control,id.vars ='group')

治疗< - ##在治疗数据中读取
治疗$组<-rep('治疗',nrow(治疗))
治疗< - 融化(治疗,id.vars = 'group')

allData< - rbind(control,treatment)

compA< - subset(allData,
variable =='L1'|
变量=='S1'|
variable =='S2')
compB< - subset(allData,
variable =='L2'|
variable =='L3'|
variable =='S3'|
variable =='S4'|
variable =='S5')
compC< - subset(allData,
variable =='L4' |
变量=='L5'|
变量=='S4'|
变量=='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')


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.

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.

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 (i.e., "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. 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')

这篇关于R中多个条件的多个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆