Search code examples
rggplot2plotlyr-plotlyggplotly

Ggplotly with tooltip has problems using geom_rect()


I am trying to plot some data in a ggplotly plot. The x-axis contains dates. Ggplotly doesn't work well with dates as when I hover over a point, the date is displayed as a number. I solved this by setting a tooltip like below.

Some sample data:

x <- data.frame(Date = as.Date(seq(Sys.Date(), Sys.Date() + 29, by = "days")), Amount = seq(-10000, 19000, by = 1000),
            stringsAsFactors = FALSE)

The plot:

ggplotly(ggplot(x, aes(x = Date, y = Amount, group = 1, text = paste("Date: ", Date, "<br>Amount: ", Amount))) + geom_line() + geom_point() 
     , tooltip = "text")

Now I want to use geom_rect() to get some background colors depending on the value of the y-axis. This gives me problems as the rectangles seem to be placed on top of the geom_line(). Also, the rectangles are labeled by ggplotly too, which I don't want either. Here is the code I tried (the background coloring works fine when I am not using a custom tooltip, but then the problem with the dates in the labels occurs):

ggplotly(ggplot(x, aes(x = Date, y = Amount, group = 1, text = paste("Date: ", Date, "<br>Amount: ", Amount))) + geom_line() + geom_point() 
     +
       geom_rect(aes(xmin = as.Date(Sys.Date()),
                     xmax = as.Date(Sys.Date() + 30),
                     ymin = 10000, ymax = max(max(x$Amount) + 1000, 11000), fill = "1")) +
       geom_rect(aes(xmin = as.Date(Sys.Date()),
                     xmax = as.Date(Sys.Date() + 30),
                     ymin = 0, ymax = 10000, fill = "2")) +
       geom_rect(aes(xmin = as.Date(Sys.Date()),
                     xmax = as.Date(Sys.Date() + 30),
                     ymin = min(min(x$Amount) - 1000, 0), ymax = 0, fill = "3"))
     +
       scale_fill_manual(values = alpha(c("green", "orange", "red"), 0.2))
     , tooltip = "text")

Result

Any help would be appreciated, thanks!

EDIT:

The following code results in working geom_rect():

ggplotly(ggplot(x, aes(x = Date, y = Amount)) + geom_line() + geom_point() 
     +
       geom_rect(aes(xmin = as.Date(Sys.Date()),
                     xmax = as.Date(Sys.Date() + 30),
                     ymin = 10000, ymax = max(max(x$Amount) + 1000, 11000), fill = "1")) +
       geom_rect(aes(xmin = as.Date(Sys.Date()),
                     xmax = as.Date(Sys.Date() + 30),
                     ymin = 0, ymax = 10000, fill = "2")) +
       geom_rect(aes(xmin = as.Date(Sys.Date()),
                     xmax = as.Date(Sys.Date() + 30),
                     ymin = min(min(x$Amount) - 1000, 0), ymax = 0, fill = "3"))
     +
       scale_fill_manual(values = alpha(c("green", "orange", "red"), 0.2)))

Result


Solution

  • You could try this:

    ggplotly(ggplot() +
               geom_rect(data = x, aes(xmin = as.Date(Sys.Date()),
                             xmax = as.Date(Sys.Date() + 30),
                             ymin = 10000, ymax = max(max(x$Amount) + 1000, 11000), fill = "1")) +
               geom_rect(data = x, aes(xmin = as.Date(Sys.Date()),
                             xmax = as.Date(Sys.Date() + 30),
                             ymin = 0, ymax = 10000, fill = "2")) +
               geom_rect(data = x, aes(xmin = as.Date(Sys.Date()),
                             xmax = as.Date(Sys.Date() + 30),
                             ymin = min(min(x$Amount) - 1000, 0), ymax = 0, fill = "3")) + 
               geom_line(data = x, aes(x = Date, y = Amount, group = 1, text = paste("Date: ", Date, "<br>Amount: ", Amount))) + 
               geom_point(data = x, aes(x = Date, y = Amount, text = paste("Date: ", Date, "<br>Amount: ", Amount))) +
                     scale_fill_manual(values = alpha(c("green", "orange", "red"), 0.2))
                   , tooltip = "text")