darach
darach

Reputation: 63

A better way to plot lots of lines (in ggplot perhaps)?

Using R 3.0.2, I have a dataframe that looks like

head()
         0     5    10    15    30    60   120   180   240
YKL134C  0.08 -0.03 -0.74 -0.92 -0.80 -0.56 -0.54 -0.42 -0.48
YMR056C -0.33 -0.26 -0.56 -0.58 -0.97 -1.47 -1.31 -1.53 -1.55
YBR085W  0.55  3.33  4.11  3.47  2.16  2.19  2.01  2.09  1.55
YJR155W -0.44 -0.92 -0.27  0.75  0.28  0.45  0.45  0.38  0.51
YNL331C  0.42  0.01 -0.05  0.23  0.19  0.43  0.73  0.95  0.86
YOL165C -0.49 -0.46 -0.25  0.03 -0.26 -0.16 -0.12 -0.37 -0.34

Where row.names() are variable names, names() are measurement times, and the values are measurements. It's several thousand rows deep. Let's call it tmp.

I want to do a sanity check of plotting every variable as time versus value as a line-plot on one plot. What's a better way to do it than naively plotting each line with plot() and lines():

timez <- names(tmp)
plot(x=timez, y=tmp[1,], type="l", ylim=c(-5,5))
for (i in 2:length(tmp[,1])) {
    lines(x=timez,y=tmp[i,])
}

The above crude answer is good enough, but I'm looking for a way to do this right. I had a concusion recently, so sorry if I'm missing something obvious. I've been doing that a lot.

Could it be something with transposing the data.frame so it's each timepoint observed across several thousand variables? Or melt()-ing the data.frame in some meaningful way? Is there someway of handling it in ggplot using aggregate()s of data.frames or something? This isn't the right way to do this, is it?

At a loss.

Upvotes: 3

Views: 1763

Answers (2)

CHP
CHP

Reputation: 17189

You can simply use matplot as follows

DF
##             0     5    10    15    30    60   120   180   240
## YKL134C  0.08 -0.03 -0.74 -0.92 -0.80 -0.56 -0.54 -0.42 -0.48
## YMR056C -0.33 -0.26 -0.56 -0.58 -0.97 -1.47 -1.31 -1.53 -1.55
## YBR085W  0.55  3.33  4.11  3.47  2.16  2.19  2.01  2.09  1.55
## YJR155W -0.44 -0.92 -0.27  0.75  0.28  0.45  0.45  0.38  0.51
## YNL331C  0.42  0.01 -0.05  0.23  0.19  0.43  0.73  0.95  0.86
## YOL165C -0.49 -0.46 -0.25  0.03 -0.26 -0.16 -0.12 -0.37 -0.34

matplot(t(DF), type = "l", xaxt = "n", ylab = "") + axis(side = 1, at = 1:length(names(DF)), labels = names(DF))

xaxt = "n" suppresses ploting x axis annotations. axis function allows you to specify details for any axis, in this case we are using to specify labels of x axis.

It should produce plot as below.

enter image description here

Upvotes: 2

Christie Haskell Marsh
Christie Haskell Marsh

Reputation: 2234

I personally prefer ggplot2 for all of my plotting needs. Assuming I've understood you correctly, you can put the data in long format with reshape2 and then use ggplot2 to plot all of your lines on the same plot:

library(reshape2)
df2<-melt(df,id.var="var")
names(df2)<-c("var","time","value")
df2$time<-as.numeric(substring(df2$time,2))

library(ggplot2)
ggplot(df2,aes(x=time,y=value,colour=var))+geom_line()

enter image description here

Upvotes: 3

Related Questions