ggplot2:使用POSIXct时间戳数据替换阴影 [英] ggplot2: shade alternate days with POSIXct timestamp data

查看:256
本文介绍了ggplot2:使用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,46L,47L,
48L,49L,28L,29L,30L,31L,32L,33L,34L,35L,36L,37L,39L,
40L,41L,42L) ,.Label = c(,2013年8月4日0:50,8/4/2013 1:50,
8/4/2013 10:50,8/4 / 2013 11:50,2013年8月4日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,
2013年8月4日18:50,8/4/2013 19: 50,8/4/2013 2:50,8/4/2013 20:50,
8/4/2013 21:50,2013年8月4日22:50 ,8/4/2013 23:50,2013年8月4日3:50,
2013年8月4日4时50分,8/4/2013 5:50, 2013年8月4日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,2013年8月5日13时50分,
8/5/2013 14:50,2013年8月5日15时50分,2013年8月5日16 :50,8/5/2013 17:50,
8/5/2013 18:50,2013年8月5日19:50,2013年8月5日2时50分,8/5/2013 20:50,
8/5/2013 21:50,2013年8月5日22:50,2013年8月5日23:50,2013年8月5日3时50分,
8 / 5/2013 4:50,2013年8月5日5:50,2013年8月5日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.0e-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.77e-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,$ b $ ),.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 ,78,79,80,81,82,
83,84,85,86,87,88 89,90,91,92,93,
94,95,96,97,98,99 ,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 省略))

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)

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



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

出现以下错误:


错误:无效的输入:time_trans与类POSIXct
的对象一起工作


我相信这是因为矩形数据框isn'吨在POSIXct。但是,我不确定如何解决这个问题,因为在矩形中没有时间戳。尝试类似这样的东西



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

c $ c> rectangles 我不认为这是正确的,因为 24 作为时间戳没有太大意义你的x轴。

还要注意阴影如何通过在数据框中指定一个实际变量,然后将填充映射到该变量以及使用 Inf -Inf 以避免需要实际的最大/最小y值。 (在x轴的端点上也可以做到这一点,以使阴影进一步扩展。)



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

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.

解决方案

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.

这篇关于ggplot2:使用POSIXct时间戳数据替换阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆