[R] Time line plot in R?

Sander Oom slist at oomvanlieshout.net
Mon Jan 17 14:30:05 CET 2005


Dear R users,

In order to illustrate the possible effects of events on variables 
plotted against time, I would like plot a time line of events along side 
the plot of the variables.

The x-axis should be some time unit; the y-axis should be the variable 
of interest; the time line should be plotted below the graph along the 
same x-axis scale.

As I have many variables and different events lists, I would like to 
write a script to read the events from a file and plot them together 
with the other plot.

The time line should look something like this:
http://www.oslis.k12.or.us/secondary/howto/organize/images/timeline.gif

The rest of the graph can be generated as in the example below.

Any suggestions on how to plot the events along a time line?

Thanks in advance,

Sander Oom.

PS: Would it be an idea to start a R gallery of graphs with sample code? 
I came across the 'R graph gallery' compilation by Lepoutre, but I'm 
sure the R community can come up with many more graph examples.



******************************
library(chron)

date<-c("2002/08/01","2002/08/02","2002/08/03","2002/08/04","2002/08/05","2002/08/06","2002/08/07",
"2002/08/08","2002/08/09","2002/08/10")
time<-c("6:15:00","7:15:00","8:15:00","9:15:00","10:15:00","11:15:00","12:15:00","13:15:00",
"14:15:00","15:15:00")
temp<-rnorm(10)+seq(1,1.9,by=0.1)
co2<-rnorm(10)+seq(1,1.9,by=0.1)
df <-data.frame(date, time, temp, co2)

dateEvents<-c("2002/08/01","2002/08/02","2002/08/03","2002/08/07","2002/08/08","2002/08/09")
descEvents<-c("disaster1", "change1", "disaster2", "disaster3", 
"change2", "change3")
dfEvents<-data.frame(dateEvents, descEvents)
dfEvents

df$datetime <- chron(dates=as.character(df$date), 
times=df$time,format=c('y/m/d', 'h:m:s'))

plot(temp ~ datetime, data=df, type="l")




More information about the R-help mailing list