R ggplot2:使用POSIXct时间戳数据对交替天数进行着色
我可以绘制一行变量与时间戳的对比图(下图p1)。不过,我想每隔几天给这块地遮阴。数据有一个条目,每小时一次,持续两天R ggplot2:使用POSIXct时间戳数据对交替天数进行着色,r,plot,ggplot2,time-series,posixct,R,Plot,Ggplot2,Time Series,Posixct,我可以绘制一行变量与时间戳的对比图(下图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,
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))
dat只需添加一个按天交替的额外变量,并使用该变量填充即可
dat$fill_flag <- round(as.integer(dat$TIMESTAMP) / (24 * 3600)) %% 2
dat$fill\u flag尝试以下方法:
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)
p1这两天效果很好。对于更长的时间序列(即更多天),我如何调整'x[1:2],xmax=x[2:3]'。谢谢。@nofunsallyhead(…,-1)
和tail(…,-1)
可能。这不会创建24小时工作日的组。是否可以对此进行修改?如果是,填充使用什么命令?谢谢。确实如此,但你必须注意时区。例如,我的日期是以CDT为单位的,因此为了获得眼球所看到的“相同”长度,我必须将四舍五入(如.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 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
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)