I'm trying to create a lineplot of a timeseries with ggplot2 and convert it to plotly. A part of the background of this plot is supposed to be shaded in a different color (like this one: Using geom_rect for time series shading in R). Unfortunately annotate() as well as geom_rect aren't transferred to the ggplotly-object as it seems. Therefore I tried to retroactively add a shape using plotly-code (based on this example: https://plot.ly/r/shapes/), but it is not working either, as shown in this reproducible example:
library(plotly)
library(ggplot2)
plot <-ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line()
plot <- ggplotly(plot)
layout(plot,
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.9,
x0 = "1980-01-01", x1 = "1990-01-01",
y0 = 0, y1 = 4000
)
)
)
So how can I get this shading for my ggplotly-object? Recreating the whole plot in plotly is unfortunately not possible.
Thanks!