ggplotly fails when trying to plot an abline on a date

I can't add an abline to my ggplotly graph if it's set for a date. Reading the error, this seems like a Plotly issue, but maybe I'm wrong, or maybe there's a workaround. Is there a way to add a line to chart if the line is defined by a date?

require(tidyverse)
require(plotly)

rev <- tibble(date=as.Date(c("2019-01-01","2019-01-02","2019-01-03")),
                    rev=c(100,200,150))
rev

rev_plot <- ggplot(data=rev, aes(x=date, y=rev, group=1)) +
          geom_line() +
          geom_vline(xintercept = as.Date('2019-01-02')) 
        # geom_hline(yintercept = 200) ### an integer abline works fine

# Plot it - works fine with ggplot2
rev_plot

# With Plotly, it fails
ggplotly(rev_plot)

ERROR Error in Ops.Date(z[[xy]], 86400000): * not defined for “Date” objects
Error in Ops.Date(z[[xy]], 86400000): * not defined for “Date” objects
Traceback:

  1. ggplotly(rev_plot)
  2. ggplotly.ggplot(rev_plot)
  3. gg2list(p, width = width, height = height, tooltip = tooltip,
    . dynamicTicks = dynamicTicks, layerData = layerData, originalData = originalData,
    . source = source, ...)
  4. gg2list_legacy(p, width = width, height = height, tooltip = tooltip,
    . layerData = layerData, originalData = originalData, source = source,
    . ...)
  5. lapply(traces, function(z) {
    . z[[xy]] <- z[[xy]] * 86400000
    . z
    . })
  6. FUN(X[[i]], ...)
  7. Ops.Date(z[[xy]], 86400000)
  8. stop(gettextf(“%s not defined for \“Date\” objects”, .Generic),
    . domain = NA)

This is definitely a bug. I was able to fix it by changing my date into a numeric value. Fortunately, it still plots correctly when you change this:

geom_vline(xintercept = as.Date('2019-01-02')) 

to this:

geom_vline(xintercept = as.numeric(as.Date('2019-01-02')) )
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.