Question

I want to create a boxplot from a matrix with several variables grouped by two levels of a factor.

Some sample data:

mymatrix = structure(list(Treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 2L), .Label = c("con", "treat"), class = "factor"), 
c1 = c(13L, 93L, 6L, 3L, 45L, 1L, 69L, 38L, 23L, 48L, 82L
), c5 = c(33L, 79L, 3L, 5L, 17L, 22L, 94L, 99L, 85L, 74L, 
9L), c3 = c(96L, 52L, 0L, 6L, 60L, 14L, 69L, 96L, 57L, 99L, 
39L), c8 = c(40L, 27L, 94L, 68L, 76L, 73L, 88L, 45L, 67L, 
95L, 85L), c12 = c(20L, 14L, 53L, 9L, 93L, 1L, 12L, 45L, 
59L, 38L, 25L)), .Names = c("Treatment", "c1", "c5", "c3", 
"c8", "c12"), class = "data.frame", row.names = c("1a", "1b", 
"2a", "2b", "3a", "3b", "4a", "4b", "5a", "5b", "5c"))

I was able to get a boxplot for each variable, but I cannot manage to group them at the same time:

boxplot(as.matrix(mymatrix[,2:6]))
boxplot(as.matrix(mymatrix[,2:6])~Treatment, data=mymatrix)

Thanks in advance for any help.

Was it helpful?

Solution

v <- stack(mymatrix[-1])
v$Treatment <- mymatrix$Treatment
boxplot(values~Treatment+ind, v)

The first part will give us a data.frame like this:

   values ind
1      13  c1
2      93  c1
...
11     82  c1
12     33  c5
...
22      9  c5
23     96  c3
...
55     25 c12

Then we append the Treatment column, and just plot as usual.

update: using the reshape package as suggested by Drew.

v <- melt(mymatrix, id.vars="Treatment")
boxplot(value~Treatment+variable, v)

OTHER TIPS

Personally I like to use the ggplot2/reshape2 approach - it is maybe a little tougher to learn at first, but once you get good at it, I think it makes things much easier.

Note that your 'matrix' is not actually a matrix, it is a data frame. This is convenient, because the approach I suggest only works with data frames.

str(mymatrix)
'data.frame':   11 obs. of  6 variables:
...

First, 'reshape' it to 'long' format, where each row represents a different observation

dfm <- melt(mymatrix, id.vars="Treatment")

(My convention is to append any melted data frame with the letter m).

Next, make the plot using ggplot2. I've mapped the Treatment column to the x axis, and the c1-c12 columns (named variable after reshaping) to the fill color, but the syntax of ggplot2 allows you to easily change that up:

ggplot(dfm, aes(x=Treatment, y=value, fill=variable)) + 
  geom_boxplot()

enter image description here

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