如何将 R Plotly 中的子图之间的轨迹与共享的 Y 轴链接起来,以便 hoverinfo 出现在两者上?
Posted
技术标签:
【中文标题】如何将 R Plotly 中的子图之间的轨迹与共享的 Y 轴链接起来,以便 hoverinfo 出现在两者上?【英文标题】:How to link traces between subplots in R Plotly with shared Y axis so that hoverinfo appears on both? 【发布时间】:2021-12-22 10:54:18 【问题描述】:我设法创建了一个由两个子图组成的图形,它们是水平条形图(棒棒糖),并排,共享 Y 轴:
但是,我希望将每对水平棒棒糖在它们之间链接起来,这样当您将鼠标悬停在一个棒棒糖上时,就会显示两者的悬停模板信息,而不仅仅是一个。有没有办法使用 Plotly R 来做到这一点,也许是自定义 JS 函数或类似的东西?我认为使用图例组选项并不容易。
到目前为止,我已经尝试了这两种方法,但它们都没有达到我想要的效果: R plotly link subplots so that multiple tooltips shown on hover 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
非常感谢您能给我的任何帮助
【问题讨论】:
【参考方案1】:跨子图共享的 hoverinfo 是 not yet available in plotly.js。
但是,您可以在跨不同轨迹的单个图中使用 hovermode = 'y unified'
:
library(plotly)
fig <- plot_ly()
fig <- fig %>% add_trace(x = ~2:4, y = ~4:6, text = ~LETTERS[4:6], name = "yaxis data", mode = "lines+markers", type = "scatter", hovertemplate = "<b>%text</b><extra></extra>")
fig <- fig %>% add_trace(x = ~4:6, y = ~4:6, name = "yaxis 2 data", mode = "lines+markers", type = "scatter")
fig <- fig %>% add_trace(x = ~6:8, y = ~4:6, name = "omit_hoverinfo", mode = "lines+markers", type = "scatter", hoverinfo='skip')
fig <- fig %>% layout(
hovermode = 'y unified' # alternativ: hovermode = 'y'
)
fig
【讨论】:
谢谢,我重做了我的情节以使用“y 统一”选项。但是,有没有办法从出现的 Hoverinfo 中省略一条痕迹?谢谢! 是的。请看我的编辑。 谢谢!这似乎并没有忽略 y 统一视图中的跟踪,但仍然显示跟踪,只是没有任何信息。有没有办法完全省略跟踪? 您要求从 Hoverinfo 中省略跟踪。这就是使用hoverinfo='skip'
时发生的情况。请看截图。
您能告诉我您使用的是哪个 plotly 软件包版本吗?我正在运行您提供的示例代码,但无法复制您共享的屏幕截图。当我说跟踪仍然显示时,我的意思是跟踪仍然显示在 hoverinfo 统一视图中(只是没有关于值编号的相应信息)谢谢。以上是关于如何将 R Plotly 中的子图之间的轨迹与共享的 Y 轴链接起来,以便 hoverinfo 出现在两者上?的主要内容,如果未能解决你的问题,请参考以下文章