[英]How to link traces between subplots in R Plotly with shared Y axis so that hoverinfo appears on both?
我设法创建了一个由两个子图组成的图形,它们是水平条形图(棒棒糖),并排,共享 Y 轴:
但是,我希望每对水平棒棒糖都链接在它们之间,这样当您将鼠标悬停在一个上面时,会同时显示两个而不是一个的悬停模板信息。 有没有办法使用 Plotly R 来做到这一点,也许是自定义 JS 函数或类似的东西? 我认为使用图例组选项不容易。
到目前为止,我已经尝试了这两种方法,但它们都没有做我想要的: R 绘图链接子图,以便在悬停时显示多个工具提示How to facet a plot_ly() chart?
这是我的数据的链接: https : //www.dropbox.com/s/g6kqq4z2y6nsk2g/plotly_data.RData?dl=0
到目前为止我的代码:
custom_hover_t <- "%{x:.2f}%"
custom_hover_c <- "%{x:.2f}%"
t <- plot_ly(data = datos) %>%
#Barras tamaño
add_trace(x = ~T2019, y = ~EjeX,
type = 'bar',
width = 0.02,
marker = list(color = ~color),
orientation = "h",
hoverlabel = list(bordercolor="white"),
hovertemplate = custom_hover_t
) %>%
add_trace(x = ~T2019, y = ~EjeX,
type = 'scatter',mode = "markers",
marker = list(color = ~color, size = 7),
hoverlabel = list(bordercolor="white"),
hovertemplate = custom_hover_t
) %>%
plotly::layout(
xaxis = list(title = NULL,
autorange = T,
zeroline = T,
showline = F,
autotick = FALSE,
tickmode = "array",
showgrid = T,
showticklabels = F,
titlefont = list(color="transparent")
),
yaxis = list(title = NULL,
visible = FALSE,
autorange = TRUE,
visible = FALSE,
zeroline = FALSE,
showline = F,
showgrid = FALSE,
ticklen = 0,
titlefont = list(color="transparent")
), #para mostrar solo 2 decimales al hacer hover en un punto
showlegend = F#,
#margin = list(l = 1)
)
c <- plot_ly(data = datos) %>%
#Barras tamaño
add_trace(x = ~CambioRel, y = ~EjeX,
type = 'bar',
width = 0.02,
marker = list(color = ~color),
orientation = "h",
hoverlabel = list(bordercolor="white"),
hovertemplate = custom_hover_c
) %>%
add_trace(x = ~CambioRel, y = ~EjeX,
type = 'scatter',mode = "markers",
marker = list(color = ~color, size = 7),
hoverlabel = list(bordercolor="white"),
hovertemplate = custom_hover_c
) %>%
plotly::layout(
xaxis = list(title = NULL,
autorange = T,
zeroline = T,
showline = F,
autotick = FALSE,
tickmode = "array",
#tickvals = ~Etiqueta,
showgrid = T,
showticklabels = F,
titlefont = list(color="transparent")
),
yaxis = list(title = NULL,
visible = FALSE,
autorange = TRUE,
visible = FALSE,
zeroline = FALSE,
showline = F,
showgrid = FALSE,
#ticks = "outside",
#ticksuffix = ticks_pct(),
#showticklabels = TRUE,
ticklen = 0,
titlefont = list(color="transparent")
), #para mostrar solo 2 decimales al hacer hover en un punto
showlegend = F#,
#margin = list(l = 1)
)
fig <- subplot(t, c, shareY = TRUE)
fig
我真的很感激你能给我的任何帮助
在 plotly.js 中尚不提供跨子图的共享hoverinfo 。
但是,您可以在跨不同轨迹的单个图中使用hovermode = 'y unified'
:
library(plotly)
fig <- plot_ly()
fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, name = "yaxis data", mode = "lines+markers", type = "scatter")
fig <- fig %>% add_trace(x = ~4:6, y = ~4:6, name = "yaxis 2 data", mode = "lines+markers", type = "scatter")
fig <- fig %>% layout(
hovermode = 'y unified'
)
fig
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.