ggplot2:使用POSIXct时间戳数据对交替日期进行阴影处理

3

我可以绘制一个变量与时间戳的折线图(下面是p1的绘图)。然而,我想为交替的日子填充阴影。该数据每小时记录一次,持续两天。

dat <-structure(list(TIMESTAMP = structure(c(2L, 3L, 14L, 19L, 20L, 
21L, 22L, 23L, 24L, 25L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 15L, 16L, 17L, 18L, 26L, 27L, 38L, 43L, 44L, 45L, 46L, 47L, 
48L, 49L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 39L, 
40L, 41L, 42L), .Label = c("", "8/4/2013 0:50", "8/4/2013 1:50", 
"8/4/2013 10:50", "8/4/2013 11:50", "8/4/2013 12:50", "8/4/2013 13:50", 
"8/4/2013 14:50", "8/4/2013 15:50", "8/4/2013 16:50", "8/4/2013 17:50", 
"8/4/2013 18:50", "8/4/2013 19:50", "8/4/2013 2:50", "8/4/2013 20:50", 
"8/4/2013 21:50", "8/4/2013 22:50", "8/4/2013 23:50", "8/4/2013 3:50", 
"8/4/2013 4:50", "8/4/2013 5:50", "8/4/2013 6:50", "8/4/2013 7:50", 
"8/4/2013 8:50", "8/4/2013 9:50", "8/5/2013 0:50", "8/5/2013 1:50", 
"8/5/2013 10:50", "8/5/2013 11:50", "8/5/2013 12:50", "8/5/2013 13:50", 
"8/5/2013 14:50", "8/5/2013 15:50", "8/5/2013 16:50", "8/5/2013 17:50", 
"8/5/2013 18:50", "8/5/2013 19:50", "8/5/2013 2:50", "8/5/2013 20:50", 
"8/5/2013 21:50", "8/5/2013 22:50", "8/5/2013 23:50", "8/5/2013 3:50", 
"8/5/2013 4:50", "8/5/2013 5:50", "8/5/2013 6:50", "8/5/2013 7:50", 
"8/5/2013 8:50", "8/5/2013 9:50"), class = "factor"), VAR = c(1.79e-06, 
2.15e-06, 1.83e-06, 1.64e-06, 2.01e-06, 2.4e-06, 2.17e-06, 5.2e-07, 
-8.29e-07, -8.05e-07, -3.28e-07, -2.48e-07, -6.45e-10, -6.49e-08, 
-1.14e-07, 9.04e-09, -1.97e-08, 1.27e-08, 3.14e-08, 2.71e-07, 
8.92e-07, 1.34e-06, 1.36e-06, 1.81e-06, 2.12e-06, 1.98e-06, 1.57e-06, 
1.53e-06, 1.88e-06, 1.08e-06, 2.15e-06, 5.36e-07, -5.73e-07, 
-7.76e-07, -4.74e-07, -2.07e-07, -1.01e-08, -8.63e-08, 1.14e-08, 
2.19e-08, -2.29e-08, 8.32e-08, 8.54e-08, 1.25e-07, 4.74e-07, 
5.15e-07, 5.52e-07, 3.34e-07)), .Names = c("TIMESTAMP", "VAR"
), row.names = c(NA, 48L), class = "data.frame", na.action = structure(49:144, .Names = c("49", 
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", 
"61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", 
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", 
"83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", 
"94", "95", "96", "97", "98", "99", "100", "101", "102", "103", 
"104", "105", "106", "107", "108", "109", "110", "111", "112", 
"113", "114", "115", "116", "117", "118", "119", "120", "121", 
"122", "123", "124", "125", "126", "127", "128", "129", "130", 
"131", "132", "133", "134", "135", "136", "137", "138", "139", 
"140", "141", "142", "143", "144"), class = "omit"))

dat$TIMESTAMP <- strptime(dat$TIMESTAMP, "%m/%d/%Y %H:%M")
dat$TIMESTAMP <- as.POSIXct(dat$TIMESTAMP)

p1 <- ggplot() + geom_line(data = dat, aes(TIMESTAMP,VAR))

然而,当我尝试使用这里建议的方法时

rect_left <- c(24)

rectangles <- data.frame(
  xmin = rect_left,
  xmax = rect_left + 24,
  ymin = -1.44454e-06,
  ymax = 4.84275e-06
)


p1 + geom_rect(data=rectangles, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill='gray80', alpha=0.8)

我收到了以下错误信息:

错误:无效输入:time_trans仅适用于POSIXct类的对象

我认为这是因为矩形数据框不是POSIXct格式。然而,由于矩形中没有时间戳,我不确定该如何纠正此错误。
2个回答

4

可以尝试像这样:

p1 <- ggplot() + geom_line(data = dat, aes(TIMESTAMP,VAR))

x <- unique(round(dat$TIMESTAMP,"days"))
y <- data.frame(xmin = x[1:2],xmax = x[2:3])
y$grp <- seq_len(nrow(y))

p1 + geom_rect(data=y, aes(xmin=xmin, xmax=xmax,fill = factor(grp)),
               ymin = -Inf,ymax = Inf, alpha=0.2)
< p > rectangles 中的数据似乎并不正确,因为在您的 x 轴上下文中,24 并没有太多意义作为时间戳。

请注意,通过在数据框中指定实际变量然后将填充映射到该变量,以及使用 Inf-Inf 来避免实际最大/最小 y 值的需要来交替指定阴影。 (同样的操作可以在 x 轴的端点处进行,以使阴影延伸更远。)

您可能需要手动设置填充比例尺以自定义颜色。


这个方法在两天内运行得很好。如何调整“x [1:2],xmax = x [2:3]”以适应更长的时间序列(即更多天)。谢谢。 - nofunsally
@nofunsally 可能是 head(...,-1)tail(...,-1) - joran

1
只需添加一个额外的变量,按天交替使用并填充即可。
dat$fill_flag <- round(as.integer(dat$TIMESTAMP) / (24 * 3600)) %% 2

这不会创建24小时天数的组。它可以修改以实现此目的吗?如果可以,使用哪个命令进行填充?谢谢。 - nofunsally
是的,但你必须小心时区。例如,我的日期是在CDT中,所以为了获得与肉眼看到的“相同”时间段,我不得不编写 round(as.integer(dat$TIMESTAMP + 7*3600) / (24 * 3600) ) %% 2 (CDT = GMT + 7) - Robert Krzyzanowski
在这种情况下,我得到了[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1,与预期相符。 - Robert Krzyzanowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接