I regularly build a data frame with time values (originating from xts or zoo objects) for analysis and plotting. Here’s an example:
library(xts)
x1 <- xts(rnorm(10), as.Date("2012-01-01") + 0:9)
x2 <- xts(rnorm(10), as.Date("2012-01-04") + 0:9)
(df <- data.frame(merge(x1=x1, x2=x2), v1=4:16, v2=rnorm(13)))
x1 x2 v1 v2
2012-01-01 0.1930827 NA 4 1.05972724
2012-01-02 0.4429592 NA 5 -1.89299068
2012-01-03 1.6657630 NA 6 0.70445966
2012-01-04 -0.2765922 -0.26728223 7 0.35336959
2012-01-05 -0.1756590 -2.04888130 8 0.90129924
2012-01-06 -2.4849132 0.49400975 9 1.54486914
2012-01-07 -0.9993353 -1.09308203 10 1.16600015
2012-01-08 -0.7326309 0.55781566 11 0.37178542
2012-01-09 -0.2973543 -0.59872496 12 0.07512468
2012-01-10 -1.5061380 0.08567125 13 1.77494367
2012-01-11 NA 0.81835375 14 -0.38211167
2012-01-12 NA 1.30131894 15 -1.09220795
2012-01-13 NA -1.29505649 16 1.27148069
To plot the time series, I need to use as.Date(row.names(df)) for the horizontal axis. For example:
plot(as.Date(row.names(df)), df$x1, type="l", xlab="", ylab="Test", main="Using row.names()", col="red")
lines(as.Date(row.names(df)), df$x2, col="blue")
lines(as.Date(row.names(df)), df$v2, col="green")
It’s a real pain to use as.Date(row.names(df)) over and over, so in the past, I added a column to the data frame using the following code:
(df <- cbind(df, tim=as.Date(row.names(df))))
I can now plot the time series as:
with(df, {
plot(tim, x1, type="l", xlab="", ylab="Test", main="Using a new tim column", col="red")
lines(tim, x2, col="blue")
lines(tim, v2, col="green")
})
This technique works, but adding that tim column feels sloppy. Is there a better/cleaner way to do this?
Edit 1 (2012-05-13) ====================================
The reason I’m using a data frame is because this data is commonly fed to one or more packages that require a data frame, and/or because I typically need to represent non-numeric data in some of the variables.
I would stick with the zoo facilities as much as possible: