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中的plotly有一种方法可以做到这一点(Line Plot Modes)
我在R里找不到解决办法。
谢谢
最佳答案
如果查看链接中提供的示例的原始JSON,可以看到hovermode
设置为x
(默认值为all
)。
如果将hovermode
设置为x
inlayout
inR
ininin则也应获得所需的输出。
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')
关于python - 在绘图R中以不同模式将多行悬停文本分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41600579/