When you have a plot, the pan feature is very nice. However, it's possible to pan very far away from what has actually been plotted. Is there a way to constrain plotting so that a user interacting with the plot hits a wall that they can't pan beyond?
I've also asked in the plotly forum here.
I'm waiting to hear back from fborges22, but their answer looks like it might be for Python or perhaps Dash R? The structure looks different from what I'm used to seeing in base R for shiny apps. Then again, I'm no expert, so if it does work in R, I'm eager to learn how! That being said, I have come across something similar before. Below is a minimal example:
fig <- plot_ly()%>%
add_trace(x = c(1, 2, 3), y = c(1, 3, 1), type = 'scatter', mode = 'lines+markers') %>%
config(fig, scrollZoom = TRUE)%>%layout(plot_bgcolor='#e5ecf6',
xaxis = list(
# xaxis_range=c(0, 4), # ADDED
# xaxis_constrain='domain', # ADDED
range=c(0, 4), # ADDED
constrain='domain', # ADDED
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(
yaxis_autorange=FALSE, # ADDED
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff')
)
Most of this is lifted directly from the plotly website and just makes a simple plot. Based on what I had read, I added some lines to test different things that sounded like they might constrain panning. They did not. I believe I may have put them in the wrong place, but experimenting with sliding them around did not help. In fact, the use of some implementation of constrain actually lead to my plotly render getting squashed in the output window of my shiny app.
Yes, there are a few ways to constrain plotting in Plotly for R so that a user interacting with the plot hits a boundary that they can't pan beyond a predetermined value.
Set the axis range: This will specify the minimum and maximum values that can be displayed on the axis. For the x-axis:
Constrain the axis to the plot domain: This will prevent the axes from being zoomed or panned beyond the plot domain. For the x-axis:
Set the autorange attribute: This will prevent Plotly from automatically adjusting the range of the axes based on the data. To prevent the y-axis from automatically adjusting its range:
Use the updatemenus feature: This allows the user to select a predefined range for the axes. It's a great way to allow quick zoom-ins or zoom-outs on specific regions.
This creates a dropdown menu allowing the user to select a predefined range for the x-axis. The "Reset" button will reset the range to its default value.
Remember to set up the dummy data and the initial plot (fig) before using the layout adjustments. This guide focuses on layout options.