【问题标题】:ggplot2: shade alternate days with POSIXct timestamp dataggplot2:使用 POSIXct 时间戳数据遮蔽隔日
【发布时间】:2014-05-03 10:55:56
【问题描述】:

我可以绘制一条变量与时间戳的关系(在下面绘制 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))

但是,当我尝试使用here建议的方法时

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 中。但是,我不确定如何纠正这个问题,因为矩形中没有时间戳。

【问题讨论】:

    标签: r plot ggplot2 time-series posixct


    【解决方案1】:

    只需添加一个按天交替的额外变量并使用它进行填充。

    dat$fill_flag <- round(as.integer(dat$TIMESTAMP) / (24 * 3600)) %% 2
    

    【讨论】:

    • 这不会创建每天 24 小时的组。可以修改它吗?如果是这样,填充使用什么命令?谢谢。
    • 确实如此,但您必须注意时区。例如,我的日期在 CDT 中,所以为了获得眼球所见的“相同”长度,我必须写 round(as.integer(dat$TIMESTAMP + 7*3600) / (24 * 3600) ) %% 2 (CDT = GMT + 7)
    • 在这种情况下,我得到了[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 1,正如预期的那样
    【解决方案2】:

    试试这样的:

    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)
    

    rectangles 中的数据无论如何我都不认为是正确的,因为24 作为 x 轴上下文中的时间戳没有多大意义。

    还要注意如何通过在数据框中指定一个实际变量然后将填充映射到该变量来交替着色,以及使用Inf-Inf 来避免需要实际最大/最小 y 的技巧价值观。 (同样可以在 x 轴的端点做同样的事情,让阴影进一步延伸。)

    您可能需要手动填充比例来自定义颜色。

    【讨论】:

    • 这可以持续 2 天。如何调整 'x[1:2],xmax = x[2:3]' 以获得更长的时间序列(即更多天)。谢谢。
    • @nofunsally head(...,-1)tail(...,-1) 可能。
    猜你喜欢
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多