如何在绘图中创建具有垂直和水平尖峰线的绘图?

Sno*_*n01 3 r plotly

layout(hovermode = "x unified")Plotly 提供了通过添加或显示可移动的水平或垂直“尖峰线”的选项layout(hovermode = "y unified")(请参阅文档)。例如:

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
Run Code Online (Sandbox Code Playgroud)

使用跟随光标的垂直线创建此图:

在此输入图像描述

我想同时显示垂直和水平尖峰线。像这样: 在此输入图像描述

我尝试像这样修改布局:

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")
Run Code Online (Sandbox Code Playgroud)

这些都不起作用。有人有建议吗?

Qui*_*ten 6

您可以在布局中使用和spikemode的选项。这是一个可重现的示例:acrossxaxisyaxis

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'))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

创建于 2022 年 10 月 29 日,使用reprex v2.0.2

如您所见,显示了垂直和水平尖峰线。


如果你只想让尖峰线到你的轴上,你可以toaxisspikemode这样使用:

fig %>%
  layout(xaxis = list(spikemode = 'toaxis'),
         yaxis = list(spikemode = 'toaxis'))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

创建于 2022 年 10 月 29 日,使用reprex v2.0.2

如您所见,这仅显示轴的尖峰线。