[R] plotting time series using ggplots

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Sep 16 09:42:14 CEST 2010


Dear Alison,

Creating a dataset in long format instead of wide format makes things
much easier with ggplot2. You also need a variable with Year. Rownames
will not do.

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

library(ggplot2)
molten <- melt(rwl, id.vars = "Year")
ggplot(molten, aes(x = Year, y = value)) + geom_smooth()

moltenCI <- cast(Year ~ ., data = molten, fun = c(mean, sd, length))
moltenCI$Low <- with(moltenCI, mean - sd / sqrt(length - 1))
moltenCI$High <- with(moltenCI, mean + sd / sqrt(length - 1))

ggplot(moltenCI, aes(x = Year, y = mean, ymin = Low, ymax = High)) +
geom_ribbon(alpha = 0.5) + geom_line()


ggplot(molten, aes(x = Year, y = value, colour = variable)) +
geom_line()
ggplot(molten, aes(x = Year, y = value, colour = variable)) +
geom_smooth()


HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
  

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org 
> [mailto:r-help-bounces op r-project.org] Namens Alison Macalady
> Verzonden: donderdag 16 september 2010 8:51
> Aan: r-help op r-project.org
> Onderwerp: [R] plotting time series using ggplots
> 
> 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
> 
> ______________________________________________
> R-help op r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.



More information about the R-help mailing list