my goal is to create a custom panel, which puts the gridlines behind the data. I already managed to create custom labs and ticks with the examples, which I could find in the zoo.pdf. I also included a custom grid with the help of abline() for my plot, including 4 timeseries. Unfortunately, the grid is in front of the data.
Could I create a second panel with a function, which includes only the code for the grid, so I can call this panel before I plot the data finally and call the panel with the custom labs and ticks?
If not, are there any other suggestions? Thank you in advance.
The sample:
Alpha.4 Beta.4 Gamma.4 Delta.4
1970-01-01 13.568902 11.663459 10.813784 14.416251
1970-01-02 16.589658 20.004628 19.446064 17.020729
1970-01-03 16.126522 18.911596 17.923830 17.185888
1970-01-04 8.777292 18.458504 16.100192 12.279212
1970-01-05 11.653461 15.264809 14.873867 11.879644
1970-01-06 13.761351 15.286759 13.615315 15.145426
1970-01-07 19.919492 9.845561 15.874263 15.722178
1970-01-08 8.652448 6.129592 5.654554 10.171612
1970-01-09 2.178357 8.106553 5.077388 6.527074
1970-01-10 4.619415 6.569528 4.475997 6.758681
1970-01-11 17.972488 13.021732 12.424542 18.774242
1970-01-12 10.245440 12.708814 11.641927 12.295237
1970-01-13 12.777710 17.940947 20.262770 7.350949
1970-01-14 13.315638 13.106116 12.951608 13.251887
1970-01-15 14.584471 15.344104 15.560071 14.408234
1970-01-16 15.705166 14.377921 14.568910 15.972086
1970-01-17 9.573016 17.515051 15.020994 12.686316
1970-01-18 11.026816 8.806327 10.520056 9.890074
1970-01-19 17.429600 17.067885 16.825756 17.730323
1970-01-20 25.481975 28.109871 26.704513 26.926373
The sample if I use dput()
structure(c(13.5689023673093, 16.5896579854384, 16.1265219690641,
8.77729169412415, 11.6534614209868, 13.76135090908, 19.9194916190192,
8.65244777254223, 2.17835724891454, 4.61941466078807, 17.9724877378618,
10.2454395713762, 12.77771034015, 13.3156375187899, 14.5844706123193,
15.7051661763148, 9.5730160161016, 11.0268156223082, 17.4295996215816,
25.4819749502041, 11.6634589828563, 20.0046278734704, 18.9115956118944,
18.4585043576629, 15.2648091421901, 15.2867592352242, 9.84556134022888,
6.12959177923138, 8.10655342345505, 6.56952760180824, 13.0217316029535,
12.7088136306114, 17.9409474542165, 13.1061164993761, 15.3441041090633,
14.3779209816531, 17.5150507518324, 8.80632652751536, 17.0678849394503,
28.1098708042811, 10.8137837795108, 19.4460635677012, 17.9238303766769,
16.1001916697774, 14.8738666533766, 13.6153149017674, 15.8742629843906,
5.65455365538076, 5.07738778354152, 4.4759966790492, 12.4245416945614,
11.6419266230246, 20.2627700130692, 12.9516078881791, 15.560070726941,
14.5689100571341, 15.0209941320211, 10.5200556167547, 16.825756177511,
26.7045130232815, 14.4162506546524, 17.0207291063746, 17.1858877845398,
12.2792115759366, 11.8796437253561, 15.145426248407, 15.7221779083876,
10.1716123171397, 6.52707375868124, 6.75868055303889, 18.7742418803468,
12.2952373490947, 7.35094930067717, 13.2518874864707, 14.4082335634949,
15.9720856725895, 12.6863161554442, 9.89007441167474, 17.7303232403674,
26.9263731388641), .Dim = c(20L, 4L), .Dimnames = list(c("14613",
"14617", "14621", "14625", "14629", "14633", "14637", "14641",
"14645", "14649", "14653", "14657", "14661", "14665", "14669",
"14673", "14677", "14681", "14685", "14689"), c("Alpha.4", "Beta.4",
"Gamma.4", "Delta.4")), index = structure(c(0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19), class = "Date"), class = "zoo")
Plot of all data, if I use the code below:
My original code:
library(zoo)
library(chron)
my.panel <- function(x, y, ..., pf = parent.frame()) {
fmt <- "%Y-%b-%d" # format for axis labels
lines(x, y, ...)
panel.number <- pf$panel.number
# if bottom panel
if (with(pf, length(panel.number) == 0 ||
panel.number %% nr == 0 || panel.number == nser)) {
## create ticks at every 91.25th x value and then label every 91.25th tick
axis(side = 1, at = seq(1,length(x), by = 91.25), labels = FALSE)
ix <- seq(1, length(x), 91.25)
labs <- format(x, fmt)
axis(side = 1, at = x[ix], labels = labs[ix], tcl = -0.8, las=2, cex.axis = 1)
}
## vertical line at every 91.25th x value
ix <- seq(1, length(x), 91.25) # index of positions for vertical lines
vlines <- c(axis.Date(1, x, at = x[ix], labels = FALSE))
abline(v = vlines, col = "red", lty = "solid", lwd = 1)
## creates a horizontal line at every 5th y value
hlines <- c(seq(5, max(y), by = 5))
abline(h = hlines, col = "red", lty = "solid", lwd = 1)
## creates ticks at every 5th step and then label every second tick
axis(side = 2, at = seq(0, max(y), by = 5), labels = FALSE)
iy <- seq(0, max(y), 10)
axis(side = 2, at = iy, cex.axis = 1)
}
plot(data, panel = my.panel,
xaxt = "n", yaxt = "n", lwd = 1, main = "Daily Mean",
xlab = "", ylab = c("TS1", "TS2", "TS3", "TS4"),
col = c("black", "black", "black", "black"))
the data looks like this: time|ts1|ts2|ts3|ts4|
The class of data is zoo.
create ticks at every 5 steps and then label every second tick: means for instance, when I use the example windspeed --> 10m/s 20m/s are labeled, 5m/s and 15m/s are not