Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 equivalent of matplot() : plot a matrix/array by columns?

matplot() makes it easy to plot a matrix/two dimensional array by columns (also works on data frames):

a <- matrix (rnorm(100), c(10,10))
matplot(a, type='l')

Is there something similar using ggplot2, or does ggplot2 require data to be melted into a dataframe first?

Also, is there a way to arbitrarily color/style subsets of the matrix columns using a separate vector (of length=ncol(a))?

like image 484
naught101 Avatar asked Aug 21 '12 02:08

naught101


1 Answers

Maybe a little easier for this specific example:

library(ggplot2)
a <- matrix (rnorm(100), c(10,10))
sa <- stack(as.data.frame(a))
sa$x <- rep(seq_len(nrow(a)), ncol(a))
qplot(x, values, data = sa, group = ind, colour = ind, geom = "line")
like image 114
adibender Avatar answered Sep 24 '22 05:09

adibender