Search code examples
rdateggplot2gganimate

view_zoom_manual xmin and xmax using date r ggplot


How can I use date in view_zoom_manual, the below works but if i change step to date i get an error below

  Step  <- c(1:50)
  Value <- runif(50,0,10)
  Final <- data.frame(Step, Value)
                    ggplot(Final, aes(x = Step, y = Value)) + 
                    geom_line(size = 1) + 
                    geom_point() +
                    view_zoom_manual(xmin = 0:20, xmax = 30:50, ymin = min(Final$Value), ymax = max(Final$Value))

Date =      c( "2018-10-18 00:00:00 BST", "2018-10-18 00:10:00 BST", "2018-10-18 00:20:00 BST", "2018-10-18 00:30:00 BST", "2018-10-18 00:40:00 BST", "2018-10-18 00:50:00 BST",
 "2018-10-18 01:00:00 BST", "2018-10-18 01:10:00 BST", "2018-10-18 01:20:00 BST", "2018-10-18 01:30:00 BST", "2018-10-18 01:40:00 BST", "2018-10-18 01:50:00 BST",
 "2018-10-18 02:00:00 BST", "2018-10-18 02:10:00 BST", "2018-10-18 02:20:00 BST", "2018-10-18 02:30:00 BST", "2018-10-18 02:40:00 BST", "2018-10-18 02:50:00 BST",
 "2018-10-18 03:00:00 BST", "2018-10-18 03:10:00 BST", "2018-10-18 03:20:00 BST", "2018-10-18 03:30:00 BST", "2018-10-18 03:40:00 BST", "2018-10-18 03:50:00 BST",
 "2018-10-18 04:00:00 BST", "2018-10-18 04:10:00 BST", "2018-10-18 04:20:00 BST", "2018-10-18 04:30:00 BST", "2018-10-18 04:40:00 BST", "2018-10-18 04:50:00 BST",
 "2018-10-18 05:00:00 BST", "2018-10-18 05:10:00 BST", "2018-10-18 05:20:00 BST", "2018-10-18 05:30:00 BST", "2018-10-18 05:40:00 BST", "2018-10-18 05:50:00 BST",
 "2018-10-18 06:00:00 BST", "2018-10-18 06:10:00 BST", "2018-10-18 06:20:00 BST", "2018-10-18 06:30:00 BST", "2018-10-18 06:40:00 BST", "2018-10-18 06:50:00 BST",
 "2018-10-18 07:00:00 BST", "2018-10-18 07:10:00 BST", "2018-10-18 07:20:00 BST", "2018-10-18 07:30:00 BST", "2018-10-18 07:40:00 BST", "2018-10-18 07:50:00 BST",
 "2018-10-18 08:00:00 BST", "2018-10-18 08:10:00 BST")   

Final <- data.frame(Date, Value)

   ggplot(Final, aes(x = Date, y = Value)) + 
   geom_line(size = 1) + 
   geom_point() +
   view_zoom_manual(xmin = Date[1]:Date[21], xmax = Date[30]:Date[50], ymin = min(Final$Value), ymax = max(Final$Value))#

So if I define using date[] i get the error. Is there a way of using date and time here?

Error in Date[1]:Date[21] : NA/NaN argument
In addition: Warning messages:
1: In data.frame(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax) :
  NAs introduced by coercion
2: In data.frame(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax) :
  NAs introduced by coercion

Solution

  • view_zoom_manual cannot handle dates. Instead we use the date as numeric and convert it back to be shown correctly on the x scale, see scale_x_continuous.

    library(gganimate)
    
    Date = as.POSIXct(c( "2018-10-18 00:00:00 BST", "2018-10-18 00:10:00 BST", "2018-10-18 00:20:00 BST", "2018-10-18 00:30:00 BST", "2018-10-18 00:40:00 BST", "2018-10-18 00:50:00 BST",
                         "2018-10-18 01:00:00 BST", "2018-10-18 01:10:00 BST", "2018-10-18 01:20:00 BST", "2018-10-18 01:30:00 BST", "2018-10-18 01:40:00 BST", "2018-10-18 01:50:00 BST",
                         "2018-10-18 02:00:00 BST", "2018-10-18 02:10:00 BST", "2018-10-18 02:20:00 BST", "2018-10-18 02:30:00 BST", "2018-10-18 02:40:00 BST", "2018-10-18 02:50:00 BST",
                         "2018-10-18 03:00:00 BST", "2018-10-18 03:10:00 BST", "2018-10-18 03:20:00 BST", "2018-10-18 03:30:00 BST", "2018-10-18 03:40:00 BST", "2018-10-18 03:50:00 BST",
                         "2018-10-18 04:00:00 BST", "2018-10-18 04:10:00 BST", "2018-10-18 04:20:00 BST", "2018-10-18 04:30:00 BST", "2018-10-18 04:40:00 BST", "2018-10-18 04:50:00 BST",
                         "2018-10-18 05:00:00 BST", "2018-10-18 05:10:00 BST", "2018-10-18 05:20:00 BST", "2018-10-18 05:30:00 BST", "2018-10-18 05:40:00 BST", "2018-10-18 05:50:00 BST",
                         "2018-10-18 06:00:00 BST", "2018-10-18 06:10:00 BST", "2018-10-18 06:20:00 BST", "2018-10-18 06:30:00 BST", "2018-10-18 06:40:00 BST", "2018-10-18 06:50:00 BST",
                         "2018-10-18 07:00:00 BST", "2018-10-18 07:10:00 BST", "2018-10-18 07:20:00 BST", "2018-10-18 07:30:00 BST", "2018-10-18 07:40:00 BST", "2018-10-18 07:50:00 BST",
                         "2018-10-18 08:00:00 BST", "2018-10-18 08:10:00 BST"), tz = "BST")
    
    Final <- data.frame(Date, Value)
    Final$D <- as.numeric(Final$Date)
    
    anim <- ggplot(Final, aes(x = D, y = Value)) + 
      geom_line(size = 1) + 
      geom_point() +
      scale_x_continuous(labels = function(x) { as.POSIXct(x, tz = "BST", origin = "1970-01-01") }) +
      view_zoom_manual(xmin = Final$D[1:21], xmax = Final$D[30:50], 
                       ymin = min(Final$Value), ymax = max(Final$Value))
    
    anim_save(filename = "anim.gif", animation = anim)
    

    enter image description here