How to add layers with new data to a ggplot with a POSIX axis?

I'm used to this if I want to add a point to a ggplot, it works just fine:

ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point() +
    geom_point(x = 200, y = 20, size = 5, color = "blue")

But, I get 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 another layer

my_plot + geom_point(x = as.POSIXct("2010-01-01"),
    y = 0, size = 5, color = "blue")

returns an error

Error in Ops.POSIXt((x - from[1]), diff(from)) : 
  '/' not defined for "POSIXt" objects

Answers


Converting to numeric solves the issue:

my_plot + geom_point(x = as.numeric(as.POSIXct("2010-01-01")),
    y = 0, size = 5, color = "blue")

But it is not necessary if the mapping is in a 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"

Need Your Help

Retrieve all DOM elements in javascript

javascript html

My application needs to retrieve all DOM elements in javascript and

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.