Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's no need to split the data into data-frames by year; you can use the <code>ggplot2</code> package quite easily to differentiate the plots by year. First I'll make up some data:</p> <pre><code>dts &lt;- as.Date("20050101", '%Y%m%d') + seq(0,1000,15) A &lt;- data.frame( Dates = dts, Gas = 4000 + cumsum(abs( rnorm(length(dts), 100, 30)))) </code></pre> <p>Next I'll add two columns to <code>A</code>: <code>DayOfYear</code> which is the "day-number" within the year, and the <code>GasDiff</code> column (same as yours but generated more easily, <em>with no loops</em>!):</p> <pre><code>A &lt;- transform( A, Year = format(Dates, '%Y'), DayOfYear = as.numeric( format(Dates, '%j')), GasDiff = c(diff( Gas ),NA)) </code></pre> <p>Next we use <code>ggplot2</code> to first plot all the years one after the other, but with different colors:</p> <pre><code>require(ggplot2) ggplot(A, aes(Dates, GasDiff)) + geom_line( aes(colour = Year)) </code></pre> <p>which gives you this: <img src="https://i.stack.imgur.com/k6F3P.png" alt="alt text"></p> <p>Alternatively you can plot the different years in a vertical grid:</p> <pre><code>ggplot(A, aes(DayOfYear, GasDiff)) + geom_line( ) + facet_grid(Year ~ .) </code></pre> <p>and you get this: <img src="https://i.stack.imgur.com/DGWRb.png" alt="alt text"></p> <p><strong><em>UPDATE:</em></strong> A third way is to plot all the years on the same plot with different colors/points, which may be useful if you are looking for seasonal patterns (but looks bad in my case because I made up random data).</p> <pre><code>ggplot(A, aes(DayOfYear, GasDiff)) + geom_line( aes(colour = Year) ) + geom_point( aes(shape = Year)) </code></pre> <p><img src="https://i.stack.imgur.com/qXJfy.png" alt="alt text"></p>
 

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