0

I'm having trouble getting ggplot to plot a rectangle covering a length of time on my plot, say from 6 AM to 12 PM.

enter image description here

I've been studying this question:

Plotting rectangles in ggplot2 - Invalid input: time_trans works with objects of class POSIXct only

But, for the life of me, I can't figure out why my example doesn't work and theirs does! I think I'm missing something very trivial, but this code:

library(ggplot2)

Example <- structure(list(Activity = c(0, 4, 7), LDT = structure(c(1456293600,
1456301700, 1456353300), class = c("POSIXct", "POSIXt"), tzone =
"Canada/Eastern")), .Names = c("Activity", "LDT"), class = "data.frame",
row.names = c(1L, 10L, 67L))

Time <- data.frame(xmin = as.POSIXct("20160224060000", 
                          format = "%Y%m%d%H%M%S", tz = "Canada/Eastern"),
                   xmax = as.POSIXct("20160224120000", 
                          format = "%Y%m%d%H%M%S", tz = "Canada/Eastern"))

ggplot(Example, aes(LDT, Activity)) +
  geom_point() +
  geom_rect(data = Time, aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf))

Results in the error, Error in eval(expr, envir, enclos) : object 'LDT' not found

When I check the structure of both data.frames, everything is nicely formatted as POSIXct. What am I doing wrong?

str(Example)
'data.frame':   3 obs. of  2 variables:
 $ Activity: num  0 4 7
 $ LDT     : POSIXct, format: "2016-02-24 01:00:00" "2016-02-24 03:15:00" ...
str(Time)
'data.frame':   1 obs. of  2 variables:
 $ xmin: POSIXct, format: "2016-02-24 06:00:00"
 $ xmax: POSIXct, format: "2016-02-24 12:00:00"
Community
  • 1
  • 1
Nova
  • 5,423
  • 2
  • 42
  • 62
  • You're right, geom_rect(data = Time, aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf), inherit.aes = FALSE) works. I didn't see their question in my search. I can delete this or leave it here for others... THANK YOU – Nova Nov 16 '16 at 16:58
  • No need to delete. The two will be linked making the topic more searchable. – Pierre L Nov 16 '16 at 16:59

0 Answers0