如何 link 在具有共享 Y 轴的 R Plotly 子图之间进行跟踪,以便 hoverinfo 出现在两者上?
How to link traces between subplots in R Plotly with shared Y axis so that hoverinfo appears on both?
我已经设法创建了一个由两个子图组成的图形,这两个子图是并排的水平条形图(棒棒糖),具有共享的 Y 轴:
但是,我希望将每对水平棒棒糖 link 插入它们之间,这样当您将鼠标悬停在一个棒棒糖上时,会显示两个而不是一个的悬停模板信息。有没有办法使用 Plotly R 来做到这一点,也许是自定义 JS 函数或类似的东西?我认为使用图例组选项并不容易。
到目前为止,我已经尝试了这两种方法,其中 none 可以满足我的要求:
这是我的数据 link: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
我真的很感激你能给我的任何帮助
子图中共享的 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 轴:
但是,我希望将每对水平棒棒糖 link 插入它们之间,这样当您将鼠标悬停在一个棒棒糖上时,会显示两个而不是一个的悬停模板信息。有没有办法使用 Plotly R 来做到这一点,也许是自定义 JS 函数或类似的东西?我认为使用图例组选项并不容易。
到目前为止,我已经尝试了这两种方法,其中 none 可以满足我的要求:
这是我的数据 link: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
我真的很感激你能给我的任何帮助
子图中共享的 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