Question

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 ?

Was it helpful?

Solution

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

enter image description here

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

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top