r - How to add layers with new data to a ggplot with a POSIX axis? -
i'm used if want add point ggplot
, works fine:
ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point() + geom_point(x = 200, y = 20, size = 5, color = "blue")
but, problems if there's posix dates involved:
dat_1 <- data.frame(time = as.posixct(c("2010-01-01", "2010-02-01", "2010-03-01")), y_1 = c(-1, 0, 1))
the basic plot works, of course
(my_plot <- ggplot(dat_1, aes(x = time, y = y_1)) + geom_point())
but adding layer
my_plot + geom_point(x = as.posixct("2010-01-01"), y = 0, size = 5, color = "blue")
returns error
error in ops.posixt((x - from[1]), diff(from)) : '/' not defined "posixt" objects
converting numeric solves issue:
my_plot + geom_point(x = as.numeric(as.posixct("2010-01-01")), y = 0, size = 5, color = "blue")
but not necessary if mapping in aes
wrapper
point_data <- data.frame(x = as.posixct("2010-01-01"), y = 0) my_plot + geom_point(aes(x = x, y = y), data = point_data, size = 5, color = "blue"
Comments
Post a Comment