[R] plotting time series using ggplots

Alison Macalady ali at kmhome.org
Thu Sep 16 08:51:14 CEST 2010


Hi,
I would like to plot a bunch of tree ring width data (time series)  
using ggplots, but I'm having trouble figuring out how to do it.

My data is in a data.frame, with years as rownames and a distinct tree  
ring series in each column. So, something like this:

rwl<-matrix(rnorm(800), nrow = 100)
colnames(rwl) <- paste('V', 1:8, sep = '')
rownames(rwl)<-c(1900:1999)
rwl<-as.data.frame(rwl))

I have 2 specific things I'd like to do:
1) use stat_summary(), geom_line(), and either geom_smooth() or  
geom_ribbon() to plot the mean of the timeseries  (e.g. V1:V8) and  
error bands or confidence limits around the mean for each year. I  
cannot figure out how to do this except for this really clunky way:

rwl$year<-as.numeric(rownames(rwl))
h<-ggplot(rwl, aes(x=year))
h+ geom_line(aes(y=V1))  #plots one of the timeseries
ymax=as.data.frame( t(apply(rwl[1:8], 1, summary,na.rm=TRUE )) )[,4]
ymin=as.data.frame( t(apply(rwl[1:8], 1, summary,na.rm=TRUE )) )[,2]
h + geom_ribbon(aes(ymin=ymin, ymax=ymax)) 
+geom_line(aes(y=rowMeans(rwl[1:8])))

2) I'd like to be able to plot all of the timeseries together, or plot  
them grouped by another variable (for example site), but I can't  
figure out how to get ggplot to plot all of the columns as Y data,  
without specifying each separate timeseries with a distinct  
+geom_line(aes(y="columnname").

Any suggestions on either problem would be greatly appreciated!

~Ali
--------------------
Alison Macalady
Ph.D. Candidate
University of Arizona
School of Geography and Development
& Laboratory of Tree Ring Research



More information about the R-help mailing list