在POSIXct x轴和Legend构成多个vlines

时间:2017-03-30 19:41:15

标签: r ggplot2

我有这个情节:

library(ggplot2)
library(scales)
date_brake = "1 min"

t1 = as.POSIXct("2015-01-01 12:30:34",tz="GMT")
t1 <- as.POSIXct(format(t1),tz="UTC")
#attr(t1,"tzone") <- "America/New_York"
t1_numeric = as.numeric(t1)

t2 = as.POSIXct("2015-01-01 12:33:10",tz="GMT")
t2 <- as.POSIXct(format(t2),tz="UTC")
#attr(t1,"tzone") <- "America/New_York"
t2_numeric = as.numeric(t2)

t3 = as.POSIXct("2015-01-01 12:34:34",tz="GMT")
t3 <- as.POSIXct(format(t3),tz="UTC")
#attr(t1,"tzone") <- "America/New_York"
t3_numeric = as.numeric(t3)


dat = data.frame(x = as.POSIXct(c("2015-01-01 12:30:31","2015-01-01 12:35:30"),tz="GMT"), y = c(30,10)  )
ggplot(dat, aes(x=x, y = y))+geom_line()+
  scale_x_datetime(date_labels = "%H:%M:%S", breaks= date_breaks(date_brake)) +
  geom_hline(aes(yintercept = 15,linetype ="dashed"),color = "green") +
  geom_hline(aes(yintercept = 25,linetype ="dashed"),color = "blue") +
  geom_vline(aes(xintercept =  t1_numeric, linetype = "dotted"   ), color = "red"   ) +
  geom_vline(aes(xintercept =  t2_numeric, linetype = "dotted"   ), color = "red"   ) +
  geom_vline(aes(xintercept =  t3_numeric, linetype = "dotted"   ), color = "red")

enter image description here

我有两个问题:

(1)如何让图例显示3个图例:蓝色线条的图例,显示带标题的蓝色虚线&#34;蓝线&#34;绿色线条的图例,显示绿色虚线标题&#34;绿线&#34;和第三个图例,显示带有标题&#34;时间&#34;

的红色虚线

(2)我想通过t1_numeric,t2_numeric和t3_numeric位置在图上注释或放置文本,所以我尝试了这个:

geom_text() + 
  annotate("text", label = "t1", x = t1_numeric, y = 20, size = 8, colour = "red")+
  annotate("text", label = "t2", x = t2_numeric, y = 20, size = 8, colour = "red")+
  annotate("text", label = "t3", x = t3_numeric, y = 20, size = 8, colour = "red")

我想把文字&#34; t1&#34;在x = t1_numeric y = 20 我想把文字&#34; t2&#34;在x = t2_numeric y = 20 我想把文字&#34; t3&#34;在x = t3_numeric y = 20

但是我收到了这个错误:

Error: Invalid input: time_trans works with objects of class POSIXct only
#####################更新:建议的解决方案几乎可以工作,但是当有一个&#34;组&#34;我收到一个错误:
Error: Insufficient values in manual scale. 5 needed but only 3 provided.


dat = data.frame(x = as.POSIXct(c("2015-01-01 12:30:31","2015-01-01 12:35:30"),tz="GMT"), y = c(30,10), group = c("A","B") )

dat.hlines = data.frame(y = c(15,25, NA, NA, NA), 
                        label = c("Green Line", "Blue Line", "Times", "Times", "Times"), 
                        x = c(NA, NA, t1, t2, t3), 
                        label_2 = c(NA, NA, "t1", "t2", "t3"), 
                        label_xpos = c(t1, t1, t1, t2, t3)) # whatever the value linked to "Green" and "Blue" lines, label_2 (NA) wont be shown

ggplot(dat, aes(x, y, group = group)) +
   geom_point(aes (color = as.factor(group))) +
  scale_x_datetime(date_labels = "%H:%M:%S", breaks= date_breaks(date_brake)) +
  geom_hline(data = dat.hlines, aes(yintercept = y, color = label), linetype = "dashed") +
  geom_vline(data = dat.hlines, aes(xintercept = x, color = label), linetype = "dotted") + 
  scale_color_manual("Your title", values = c("Green Line" = "green", "Blue Line" = "blue", "Times" = "red")) +
  geom_text(data = dat.hlines, aes(label_xpos, label = label_2), y = 20, color = "red", hjust = -0.5)

1 个答案:

答案 0 :(得分:0)

可能的解决方案,创建第二个数据帧(dat.hlines):

enter image description here

dat = data.frame(x = as.POSIXct(c("2015-01-01 12:30:31","2015-01-01 12:35:30"),tz="GMT"), y = c(30,10)  )

dat.hlines = data.frame(y = c(15,25, NA, NA, NA), 
label = c("Green Line", "Blue Line", "Times", "Times", "Times"), 
x = c(NA, NA, t1, t2, t3), 
label_2 = c(NA, NA, "t1", "t2", "t3"), 
label_xpos = c(t1, t1, t1, t2, t3)) # whatever the value linked to "Green" and "Blue" lines, label_2 (NA) wont be shown

ggplot(dat, aes(x, y)) +
  geom_line() +
  scale_x_datetime(date_labels = "%H:%M:%S", breaks= date_breaks(date_brake)) +
  geom_hline(data = dat.hlines, aes(yintercept = y, color = label), linetype = "dashed") +
  geom_vline(data = dat.hlines, aes(xintercept = x, color = label), linetype = "dotted") + 
  scale_color_manual("Your title", values = c("Green Line" = "green", "Blue Line" = "blue", "Times" = "red")) +
  geom_text(data = dat.hlines, aes(label_xpos, label = label_2), y = 20, color = "red", hjust = -0.5)