create boxplot from matrix with ggplot2

boxplot, ggplot2, matrix, r

Solution

You have to transform your matrix into a data frame in the long format to use `ggplot2`:

dat <- stack(as.data.frame(myMatrix))

Now you can use `ggplot2`:

library(ggplot2)
ggplot(dat) + 
  geom_boxplot(aes(x = ind, y = values))

Problem

I have been looking for this subject but I couldn't find a clear answer to my problem. I have a matrix like this: ``` > myMatrix [,1] [,2] [,3] [1,] 1.0000000 0.8236821 5.174437e-01 [2,] 0.2696589 0.1854324 1.000000e+00 [3,] 0.3266629 0.1982345 1.000000e+00 [4,] 1.0000000 0.7766927 1.807857e-01 [5,] 1.0000000 0.5033586 3.662552e-01 [6,] 1.0000000 0.9419056 4.989487e-02 [7,] 1.0000000 0.6215466 9.446507e-01 [8,] 1.0000000 0.9483751 8.328207e-01 [9,] 1.0000000 0.4597556 1.756653e-01 [10,] 1.0000000 0.4842302 8.154578e-01 [11,] 1.0000000 0.5108767 1.912999e-01 [12,] 1.0000000 0.4315146 2.921003e-01 [13,] 1.0000000 0.8404401 8.960460e-03 [14,] 0.8190788 1.0000000 3.551697e-08 [15,] 1.0000000 0.5707248 1.897027e-03 [16,] 0.7768339 1.0000000 6.486805e-07 [17,] 0.7258489 1.0000000 3.463566e-06 [18,] 0.1839966 0.1848062 1.000000e+00 [19,] 0.3061347 0.1304444 1.000000e+00 [20,] 1.0000000 0.4425222 9.216534e-02 ``` I would like to produce a boxplot using `ggplot2` which display this (3 groups which are the 3 columns): ``` boxplot(myMatrix) ``` I can't find the way with `ggplot2` to produce the same boxplot with my 3 groups. Please, can someone help me?

Original source