plotly.R
plotly.R copied to clipboard
ggplotly doesn't display geom_rect with ymin = -Inf and ymax = Inf
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

# Calling ggplotly
example_plot %>% ggplotly()

Note ggplotly image above captured using 'export' function on Viewer pane in RStudio.
Hi @cpsievert thanks for all your effort put in this package. Do you know if there is any update on this bug?
Hi @cpsievert, I'm also interested to know if there are any updates here. Thanks!
Same problem here (ggplot2 3.3.5, plotly 4.9.4.1) - any updates on this?
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).
+1 for this.