I have a plotly chart where the x-axis is date. I have added a rectangle shape to highlight a specified date period. Sometimes this date period will lie within the date range of the data displayed in the chart, in which case there is no problem. The chart appears as normal. However, it is possible that the date period lies outside of the date range in the chart (or it overlaps, with part of it outside the date range of the chart). In this case, when the chart is produced, the x-axis is autoscaled to show the full rectangle as well as the data (or the traces).
This is not what I want. I want it to be autoscaled on the date range of the data (traces) only (as if the rectangle didn't exist), but with the rectangle added in the background of the chart. If the user manually zooms out they should be able to see the full rectangle, including the part that lies outside of the data, but this should not be the default.
See the below code as an example. It is an edited version of the code taken from here: https://plotly.com/r/shapes/#lines
The date range of the data is 1967-2015. The autoscale of the x-axis should be based on this only. However, as one of the rectangles has x coordinates of 1940 and 1945, the x-axis is autoscaled to 1940-2015 in order to display the full rectangle, leaving a large gap between 1940 and 1967 with no data. I want it to be so that if the user manually zooms out to a scale of 1940-2015, this is what they would see, but the autoscale should ignore the rectangle and default to a range of 1967-2015.
library(plotly)
fig <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")
# add shapes to the layout
fig <- layout(fig, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1940-01-01", x1 = "1945-01-01", xref = "x",
y0 = 0, y1 = 1, yref = "paper"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
y0 = 0, y1 = 1, yref = "paper")))
fig
There does not appear to be anything in the documentation that addresses this issue, and I can't find anything on Google either. But I'd be surprised if I'm the only person who's ever wanted to do this, so I think there must surely be an option.
Does anyone know?
EDIT: I have discovered that I can define the range of the x-axis manually, and can take this to be the minimum and maximum of the x column. However, two drawbacks of this approach. One is that the outer points lie exactly on the boundary, so half of the point or bar (if you have a bar chart) gets cropped out which doesn't look good.
The other potential drawback is when you click the autoscale button it still zooms right out to see the rectangles outside of the range. This may or may not be desirable, but it will be good to know if an alternative method exists.
library(plotly)
fig <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")
minx <- min(economics$date)
maxx <- max(economics$date)
x_axis <- list(
range = c(minx,maxx)
)
# add shapes to the layout
fig <- layout(fig, xaxis= x_axis, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1940-01-01", x1 = "1945-01-01", xref = "x",
y0 = 0, y1 = 1, yref = "paper"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
y0 = 0, y1 = 1, yref = "paper")),
xaxis = list(range=c(as.Date("1980-01-01"),as.Date("2000-01-01")))
)
fig