I have this plot:
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")
I have 2 questions:
(1) How can I get the legend to show 3 legends: a legend for the blue line that shows a blue dashed line with title "Blue line", a legend for the green line that shows a green dashed line with title "Green line" and a 3rd legend that shows a red dotted line with title "Times"
(2) I would like to annotate or put text on the plot by the t1_numeric, t2_numeric and t3_numeric positions so I tried this:
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")
I would like to put the text "t1" at x = t1_numeric y = 20 I would like to put the text "t2" at x = t2_numeric y = 20 I would like to put the text "t3" at x = t3_numeric y = 20
But I get this error:
Error: Invalid input: time_trans works with objects of class POSIXct only
##################### UPDATE: the proposed solution almost works but when there is a "group" I get an error:
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)