ggplot2: shade alternate days with POSIXct timestamp data

天大地大妈咪最大 提交于 2019-12-23 19:07:51

问题


I can plot a line of a variable vs timestamp (plot p1 below). However, I'd like to shade the plot for alternate days. The data has an entry once an hour for two days.

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

However, when I try using the method suggested 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)

I get the following error:

Error: Invalid input: time_trans works with objects of class POSIXct only

Which I believe is because the rectangle data frame isn't in POSIXct. However, I am unsure on how to rectify this, as there isn't a timestamp in rectangles.


回答1:


Try something like this:

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)

The data in rectangles I don't think was correct anyway, since 24 doesn't have much meaning as a time stamp in the context of your x axis.

Note also how the shading is alternated by specifying an actual variable in the data frame and then mapping fill to that variable, and the trick of using Inf and -Inf to avoid the need for the actual max/min y values. (The same can be done at the end points on the x axis to get the shading to extend further.)

You might want a manual fill scale to customize the colors.




回答2:


Just add an extra variable that alternates by day and fill using that.

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


来源:https://stackoverflow.com/questions/22637284/ggplot2-shade-alternate-days-with-posixct-timestamp-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!