Skip to content

Setting the grid of a graph of timeseries

2 messages · v.demart@libero.it, Gabor Grothendieck

#
I have the following code

####################################################################

library(zoo)

miedate <- yearmon((2006)+seq(0,23)/12)
tab <- zoo(cbind(A = c(79.47, 89.13, 84.86, 75.68, 72.82, 78.87, 93.46,
78.18, 82.46, 77.25, 80.95, 84.39, 81.7, 74.76, 65.29, 60.3,
66.59, 73.19, 92.39, 65.76, 77.45, 74.22, 101.36, 100.01), B = c(77.95,
76.73, 51.2, 51.86, 51.29, 49.45, 53.88, 47.96, 55.07, 45.34,
37.07, 37.53, 47.79, 37.5, 30.35, 37.78, 34.13, 39.14, 39.89,
35.46, 36.54, 38.39, 47.33, 45.34)),miedate)

par(mai=c(0.75, 0.75, 0.1, 0.1))
my.panel <- function(...) {
   fmt <- "%b-%Y" # format for axis labels
   lines(...)
   panel.number <- parent.frame()$panel.number
   # if bottom panel
   if (!length(panel.number) || panel.number == NCOL(tab)) { 
      # next line only if non-labelled ticks wanted for each point
      axis(1, at = time(tab), lab = FALSE)
      ix <- seq(1, length(tab), 3)
      labs <- format(time(tab), fmt)
      axis(1, at = time(tab)[ix], lab = labs[ix], tcl = -0.7, cex.axis = 0.9)
   }
}

plot.zoo(tab, plot.type="single", panel=my.panel, lty=c(1,2,3,4,5,1), 
lwd=c(8,2,2,2,2,8),           
col=c("darkorange", "red2","darkblue","darkblue","green","midnightblue"), 
            xlab=" ",ylab = "euro/MWh",xaxt="n")

####################################################################

This code produces a graph with a non-decimal x-axis but by far more (human) 
readable because ticks corresponds to months exactly (and not to a weird 
decimal ticking!).
I would like to add a vertical grid with the same criterium.

How can I obtain this?

Ciao
Vittorio
#
- You don't need a panel function since there is only one.
- There should not be more than 2 components on lty and lwd
since tab has two columns and that is the cause of the
warning message you are seeing.
- with the recent version of zoo plot.type uses screen so
you can write the shorter screen = 1 instead of the lengthier
plot.type = "single"

Redone with an abline at the end to give the grid lines gives:

plot(tab, screen = 1, lty = c(1,2), lwd = c(8,2),
	col = c("darkorange", "red2"),
	xlab = " ", ylab = "euro/MWh", xaxt = "n")

fmt <- "%b-%Y" # format for axis labels
axis(1, at = time(tab), lab = FALSE)
ix <- seq(1, length(tab), 3)
labs <- format(time(tab), fmt)
axis(1, at = time(tab)[ix], lab = labs[ix], tcl = -0.7, cex.axis = 0.9)
abline(v = time(tab)[ix], lty = 2, col = "grey")
On Dec 9, 2007 12:33 PM, vittorio <vdemart1 at tin.it> wrote: