Note that there are some explanatory texts on larger screens.

plurals
  1. POPlotting from a data frame that contains one or more time series
    primarykey
    data
    text
    <p>I regularly build a data frame with time values (originating from xts or zoo objects) for analysis and plotting. Here's an example:</p> <pre><code>library(xts) x1 &lt;- xts(rnorm(10), as.Date("2012-01-01") + 0:9) x2 &lt;- xts(rnorm(10), as.Date("2012-01-04") + 0:9) (df &lt;- 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 </code></pre> <p>To plot the time series, I need to use <code>as.Date(row.names(df))</code> for the horizontal axis. For example:</p> <pre><code>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") </code></pre> <p>It's a real pain to use <code>as.Date(row.names(df))</code> over and over, so in the past, I added a column to the data frame using the following code:</p> <pre><code>(df &lt;- cbind(df, tim=as.Date(row.names(df)))) </code></pre> <p>I can now plot the time series as:</p> <pre><code>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") }) </code></pre> <p>This technique works, but adding that <code>tim</code> column feels sloppy. Is there a better/cleaner way to do this?</p> <p>Edit 1 (2012-05-13) ====================================</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload