1

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

grg
  • 5,023
  • 3
  • 34
  • 50
Nucore
  • 107
  • 1
  • 13
  • 1
    Does this SO answer give you what you need: http://stackoverflow.com/questions/7263743/how-do-i-draw-gridlines-using-abline-that-are-behind-the-data?rq=1 – eipi10 Feb 26 '14 at 14:11
  • Thank you for the link. When I try to create a new function called my.grid, which only includes the creating vertical lines and horizontal lines-part of the function above, using panel.first, I only get a warning message: "panel.first" is not a graphical parameter. Could it be, that panel.first can not handle functions? – Nucore Feb 26 '14 at 15:14
  • Please provide something reproducible, i.e. provide the data. – G. Grothendieck Feb 26 '14 at 15:29
  • I'm sorry. I will do this. Could you suggest me a way, to upload the sample on this site? Is there a common filehoster, which is used here? – Nucore Feb 26 '14 at 16:14
  • 1
    It would probably be better if you provide a small sample of your data and post it directly within your question. You can do this by selecting maybe 10 or 20 rows of your data (and include only the columns necessary for the plot). Let's say this subset of your data is called "df". Paste the output of `dput(df)` in your question. – eipi10 Feb 26 '14 at 16:31
  • sample of data is now there and the plot of data. – Nucore Feb 26 '14 at 19:15
  • panel.first takes an expression. If you just add, say `panel.first=my.grid`, then R thinks my.grid is a graphical parameter and throws an error. Instead it has to be something like `panel.first=my.grid(...)` where the "..." are the arguments to the function. – eipi10 Feb 27 '14 at 01:24
  • This works for a single plot: `plot(1:25,1:25, panel.first=c(abline(h=seq(5,25,5), lty=3, col='grey20'), abline(v=seq(5,25,5), lty=3, col='grey20')))`, but panel.first doesn't seem to draw the lines if you include more than two columns of data (creating a multipanel scatterplot like your code above does). Perhaps someone else can jump in with a solution that gets panel.first to work for the multi-panel case. – eipi10 Feb 27 '14 at 01:25
  • One more thing: You've posted your data in a form that's difficult to use. Instead you should assign the data to an object, `dput()` that object, and then paste the output of `dput()` into your question. – eipi10 Feb 27 '14 at 01:28
  • Thank you for your help. Really appreciate it. Now I have a better insight about what panel.first expects. I've added the output of dput(). – Nucore Feb 27 '14 at 12:23
  • Creating an own function for panel.first does not seem to be useful, when I have to put the arguments in by myself. Seems more like I have to solve the problem with giving panel.first a vector of functions like you did. – Nucore Feb 27 '14 at 13:00

0 Answers0