2
votes

Multiple Boxplot in R from a matrix, grouped by the values of a particular column.

eg.

M= matrix(c(1,2,1,3,2,3,1,4,2,5,3,5,2,6),ncol=2)

ie.

   [,1] [,2]
[1,]    1    4
[2,]    2    2
[3,]    1    5
[4,]    3    3
[5,]    2    5
[6,]    3    2
[7,]    1    6

Now I want a graph with boxplots for each distinct value of column one. i.e. there should be three boxplots viz. for 1,2,3(distinct col. 1 values)

Thanks.

2

2 Answers

3
votes
M <- as.data.frame(M)
boxplot(M$V2~M$V1)
1
votes

You can do this using ggplot2:, e.g.:

library(ggplot2)
df = as.data.frame(M)
ggplot = ggplot(df, aes(as.factor(V1), V2)) + geom_boxplot()