Multiple boxplots for multiple conditions in R

大城市里の小女人 提交于 2019-12-06 12:40:15

问题


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 ?


回答1:


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')



来源:https://stackoverflow.com/questions/19705039/multiple-boxplots-for-multiple-conditions-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!