Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are the plots corresponding to Prasad's ggplot examples (1) done using <code>xyplot</code> in lattice. (2) After that we show how to do it using <code>xyplot.zoo</code> from the zoo package and then (3) we show how to do each yet again using the <code>plot.zoo</code> which uses the zoo package's classic graphics facilities.</p> <p>In each of these cases we also show a 4th style which is based on <code>xblocks</code>.</p> <p>First lets repeat Prasad's setup:</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)))) A &lt;- transform( A, Year = format(Dates, '%Y'), DayOfYear = as.numeric( format(Dates, '%j')), GasDiff = c(diff( Gas ),NA)) </code></pre> <p>Now lets try using lattice</p> <pre><code>library(lattice) # xyplot library(latticeExtra) # layer_, panel.xblocks library(gridExtra) # grid.arrange library(RColorBrewer) # brewer.pal png("png1.png") p1 &lt;- xyplot(GasDiff ~ Dates, group = Year, A, type = "l", par.settings = list(superpose.line = list(col = 1:nlevels(A$Year))), auto.key = list(lines = TRUE, points = FALSE)) p2 &lt;- xyplot(GasDiff ~ DayOfYear | Year, A, type = "l", layout = c(1, 3)) p3 &lt;- xyplot(GasDiff ~ DayOfYear, A, group = Year, type = "l", auto.key = list(lines = TRUE, points = FALSE)) # and here is another style: myPalette &lt;- brewer.pal(nlevels(A$Year), "Set3") p4 &lt;- xyplot(GasDiff ~ Dates, A, type = "l", col = 1) + layer_(panel.xblocks(A$Dates, myPalette[A$Year])) grid.arrange(nrow = 2, p1, p2, p3, p4) dev.off() </code></pre> <p>This gives these 4 plots:</p> <p><img src="https://i.stack.imgur.com/X9HnG.png" alt="alt text"></p> <p>and now lets repeat this using using zoo in conjunction with the lattice and the other packages:</p> <pre><code>png("png2.png") library(zoo) library(lattice) library(latticeExtra) # layer_, panel.xblocks library(gridExtra) # grid.arrange library(RColorBrewer) # brewer.pal z &lt;- with(A, zoo(GasDiff, Dates)) year &lt;- format(time(z), "%Y") # split years into separate columns and plot P1 &lt;- xyplot(do.call("merge", split(z, year)), screen = 1, col = 1:3) # split years into separate columns and use day.of.year as time day.of.year &lt;- function(x) as.numeric(format(x, "%j")) zz &lt;- read.zoo(A[c(1, 5, 3)], FUN = day.of.year, split = 3) colnames(zz) &lt;- unique(year) P2 &lt;- xyplot(na.approx(zz, na.rm = FALSE)) P3 &lt;- xyplot(na.approx(zz, na.rm = FALSE), screen = 1, col = 1:3, auto.key = TRUE) pal &lt;- brewer.pal(nlevels(factor(year)), "Set3") P4 &lt;- xyplot(z, screen = 1) + layer_(panel.xblocks(time(z), pal[factor(year)])) grid.arrange(nrow = 2, P1, P2, P3, P4) dev.off() </code></pre> <p>Here is the output:</p> <p><img src="https://i.stack.imgur.com/RIGTi.png" alt="alt text"></p> <p>A third set of ways is to use classic graphics with zoo where we use the same <code>z</code>, <code>zz</code> and <code>pal</code> calculated above:</p> <pre><code>library(zoo) library(RColorBrewer) # brewer.pal png("png3a.png") plot(do.call("merge", split(z, year)), screen = 1, col = 1:3) dev.off() png("png3b.png") plot(na.approx(zz, na.rm = FALSE)) dev.off() png("png3c.png") plot(na.approx(zz, na.rm = FALSE), screen = 1, col = 1:3) legend("topleft", colnames(zz), lty = 1, col = 1:3, bty = "n") dev.off() png("png3d.png") plot(z, type = "n") xblocks(time(z), pal[factor(year)]) lines(z) dev.off() </code></pre> <p>and here is the output</p> <p><img src="https://i.stack.imgur.com/pCjvl.png" alt="alt text"></p> <p><img src="https://i.stack.imgur.com/bOHNK.png" alt="alt text"></p> <p><img src="https://i.stack.imgur.com/hcrcU.png" alt="alt text"></p> <p><img src="https://i.stack.imgur.com/yvBRt.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