Reputation: 397
Plotly offers the option to display a moveable horizontal or vertical "spikeline" by adding layout(hovermode = "x unified")
or layout(hovermode = "y unified")
(see documentation). For example:
library(plotly)
x <- seq(from = 0, to = 2*pi, length = 100)
y <- sin(x)
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers")
fig <- fig %>% layout(hovermode = "x unified")
fig
creates this plot with a vertical line that follows the cursor:
I would like to have both the vertical and the horizontal spikeline displayed. Like this:
I tried to modify the layout like this:
fig <- fig %>% layout(hovermode = "x unified") %>% layout(hovermode = "y unified")
# or
fig <- fig %>% layout(hovermode = c("x unified", "y unified"))
# or
fig <- fig %>% layout(hovermode = "x unified|y unified")
None of this worked. Would anybody have a suggestion?
Upvotes: 3
Views: 1158
Reputation: 41225
You could use spikemode
with the option across
in your layout for both xaxis
and yaxis
. Here is a reproducible example:
library(plotly)
x <- seq(from = 0, to = 2*pi, length = 100)
y <- sin(x)
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers")
fig %>%
layout(xaxis = list(spikemode = 'across'),
yaxis = list(spikemode = 'across'))
Created on 2022-10-29 with reprex v2.0.2
As you can see both vertical and horizontal spike lines are shown.
If you only want to have the spike lines to your axis, you could use toaxis
in your spikemode
like this:
fig %>%
layout(xaxis = list(spikemode = 'toaxis'),
yaxis = list(spikemode = 'toaxis'))
Created on 2022-10-29 with reprex v2.0.2
As you can see this only shows the spike lines to your axis.
Upvotes: 6
Reputation: 1
Check out this resource on 3d plots - I had the same issue and applied the xaxis = list() and yaxis = list() arguments to layout() and it worked well for me.
https://plotly.com/r/3d-hover/
Upvotes: 0