Description
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.