使用自定义面板在数据后面使用abline()绘制网格线

时间:2014-02-26 14:06:21

标签: r plot zoo gridlines

我的目标是创建一个自定义面板,将网格线放在数据后面。我已经设法使用示例创建自定义实验室和刻度,我可以在zoo.pdf中找到它们。我还在abline()的帮助下为我的情节添加了一个自定义网格,包括4个时间序列。不幸的是,网格位于数据前面。

我可以创建一个带有功能的第二个面板,其中只包含网格代码,所以我可以在最终绘制数据之前调用此面板并使用自定义实验室和刻度调用面板吗?

如果没有,还有其他建议吗?提前谢谢。

样本:

             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

我使用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")

所有数据的图表,如果我使用以下代码:

我的原始代码:

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"))

数据如下所示: 时间| TS1 | TS2 | TS3 | TS4 |

数据类是动物园。

每5个步骤创建一个刻度,然后标记每一个刻度: 例如,当我使用示例windspeed时 - >标记为10m / s 20m / s,5m / s和15m / s不是

0 个答案:

没有答案