plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

ggplotly doesn't display geom_rect with ymin = -Inf and ymax = Inf

Open yyuan-convoy opened this issue 5 years ago • 5 comments

In ggplot::geom_rect(), one can specify the aesthetics ymin = -Inf and ymax = Inf, which will draw the rectangles to the limit of the plotting area. Apparently, ggplotly does not support this behaviour, and will fail to display the rectangles when a ggplot object with such a geom_rect is passed.

Motivation This behaviour of geom_rect is especially useful for indicating time periods of interest when plotting time series. Workarounds such as manually specifying the ymin and ymax limits to mimic the behaviour are much less effective: it can be difficult to know in advance what those limits should be, and they are affected by expand settings in the scale specification.

# Plot definition
example_plot = economics %>%
  ggplot() +
  geom_line(aes(x = date, y = psavert)) +
  geom_rect(data = data.frame(xmin = seq(as.Date('1970-01-01'),
                                         as.Date('2010-01-01'),
                                         '10 years'),
                              xmax = seq(as.Date('1975-01-01'),
                                         as.Date('2015-01-01'),
                                         '10 years'),
                              ymin = -Inf, ymax = Inf),
            aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax))
# Base ggplot object
example_plot

image

# Calling ggplotly
example_plot %>% ggplotly()

image

Note ggplotly image above captured using 'export' function on Viewer pane in RStudio.

yyuan-convoy avatar Jul 01 '19 07:07 yyuan-convoy

Hi @cpsievert thanks for all your effort put in this package. Do you know if there is any update on this bug?

YannisChourdakis avatar Feb 05 '21 16:02 YannisChourdakis

Hi @cpsievert, I'm also interested to know if there are any updates here. Thanks!

jpettit2 avatar Feb 25 '21 16:02 jpettit2

Same problem here (ggplot2 3.3.5, plotly 4.9.4.1) - any updates on this?

tkasci avatar Sep 18 '21 12:09 tkasci

Hi, I'm also having this problem. It would be hugely helpful to show geom_rect options (e.g., for colour-coding specific regions of a chart).

tony352 avatar May 19 '22 13:05 tony352

+1 for this.

landisrm avatar May 26 '22 17:05 landisrm