Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I produce a boxplot in ggplot using a matrix

Tags:

r

ggplot2

In R it is easy to turn a matrix into a boxplot

> myMatrix
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   27   32   31   28   20   28   10   29   15    29
 [2,]   31   33   20   28   21    9   14   21   34    33
 [3,]   27   33   28   23   26   33   19   11   26    30
 [4,]   33   17   10   31   10   32   10   29   31    28
 [5,]   25   10   29   34   32   33   28   32   32    32
 [6,]   32   19   13   32   26   20    9   26   32    33
 [7,]   33   32   18   26   27   28   29   32   24    25
 [8,]   33   34   32   30   27   31   22   32   33    30
 [9,]   32   34   31   22   17   31   11   27   18    23
[10,]   26   10   30   30   27    4    4    4    4     4

> boxplot(as.data.frame(myMatrix))

How do I accomplish the same thing in ggplot?

like image 388
Jeremy Leipzig Avatar asked Feb 25 '10 22:02

Jeremy Leipzig


People also ask

How do you make a boxplot matrix in R?

If we want to create boxplot for matrix columns then we need to convert the matrix into data frame and then use the boxplot function. For example, if we have a matrix called M then the boxplot for columns in M can be created by using boxplot(as. data. frame(M)).

How can you create a boxplot using ggplot2?

In ggplot2, geom_boxplot() is used to create a boxplot. Let us first create a regular boxplot, for that we first have to import all the required libraries and dataset in use. Then simply put all the attributes to plot by in ggplot() function along with geom_boxplot.

What is boxplot Ggplot?

The boxplot compactly displays the distribution of a continuous variable. It visualises five summary statistics (the median, two hinges and two whiskers), and all "outlying" points individually.

Does Ggplot boxplot show median or mean?

A boxplot summarizes the distribution of a continuous variable and notably displays the median of each group.


1 Answers

You could use the melt() function and then geom_boxplot(). First reproduce the data (thanks to Excel):

vec <- c(c(27, 32, 31, 28, 20, 28, 10, 29, 15, 29), 
 + c(31, 33, 20, 28, 21,  9, 14, 21, 34, 33), 
 + c(27, 33, 28, 23, 26, 33, 19, 11, 26, 30), 
 + c(33, 17, 10, 31, 10, 32, 10, 29, 31, 28), 
 + c(25, 10, 29, 34, 32, 33, 28, 32, 32, 32), 
 + c(32, 19, 13, 32, 26, 20,  9, 26, 32, 33), 
 + c(33, 32, 18, 26, 27, 28, 29, 32, 24, 25), 
 + c(33, 34, 32, 30, 27, 31, 22, 32, 33, 30), 
 + c(32, 34, 31, 22, 17, 31, 11, 27, 18, 23), 
 + c(26, 10, 30, 30, 27,  4,  4,  4,  4,  4))
myMatrix <- matrix(data=vec, ncol=10, byrow=TRUE)

Then melt and plot:

library(reshape)
library(ggplot2)
ggplot(data=melt(as.data.frame(myMatrix)), aes(variable, value)) + geom_boxplot()

Also, as Hadley suggested (long ago) in the comments, there is no need to cast the matrix to a data frame:

ggplot(data=melt(myMatrix), aes(as.factor(X2), value)) + geom_boxplot()
like image 76
momobo Avatar answered Oct 10 '22 21:10

momobo