Plotly具有同时显示多行的悬停文本的功能.例如:
dt <- data.table(x = 1:10, y = rnorm(10), z = rnorm(10)+2) plot_ly(type = "scatter", mode = "lines") %>% add_trace(x = dt$x, y = dt$y, name = "curve 1", mode = "lines") %>% add_trace(y = dt$z, name = "curve 2", mode = "lines")
但是,如果它们的模式不同,则不会对悬停文本进行分组.例如:
dt <- data.table(x = 1:10, y = rnorm(10), z = rnorm(10)+2) plot_ly(type = "scatter", mode = "lines") %>% add_trace(x = dt$x, y = dt$y, name = "curve 1", mode = "lines+markers") %>% add_trace(y = dt$z, name = "curve 2", mode = "lines")
在python中有一个方法可以做到这一点(Line Plot Modes)我无法在R中找到解决方案.
谢谢
如果查看链接中提供的示例的原始JSON,您可以看到hovermode
设置为x
(默认值为all
).
如果设置hovermode
为x
in layout
,R
则应获得所需的输出.
library(data.table) library(plotly) set.seed(42) dt <- data.table(x = 1:10, y = rnorm(10), z = rnorm(10)+2) plot_ly(type = "scatter", mode = "lines") %>% add_trace(y = dt$y, name = "curve 1", mode = "lines+markers") %>% add_trace(y = dt$z, name = "curve 2", mode = "lines") %>% layout(hovermode = 'x')