摆脱情节中的双重悬停
Get rid of double hover in plotly
我有一张图表显示了两种数据以及它们对未来几个月的预测。我使用 hovermode = "x"
同时悬停两行。一切正常,但......在 amount
等于 forecast
的部分,悬停加倍。这是很合乎逻辑的,但我想摆脱其中一个价值观。你知道怎么处理吗?
我的数据:
dane <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
amount1 = c(1086L, 1027L, 1024L, 1080L, 1072L, 1144L, NA, NA, NA, NA, NA, NA),
amount2 = c(1057, 1067, 1068, 1060, 1056, 1040, NA, NA, NA, NA, NA, NA),
amount1_forecast = c(NA, NA, NA, NA, NA, 1144L, 1119L, 1165L, 1145L, 1170L, 1158L, 1115L),
amount2_forecast = c(NA, NA, NA, NA, NA, 1040L, 1178L, 1122L, 1145L, 1158L, 1175L, 1119L)),
row.names = c(NA, -12L),
.Names = c("month", "amount1", "amount2", "amount1_forecast", "amount2_forecast"),
class = "data.frame")
我的剧情:
library("plotly")
wyk <- plot_ly(dane, x = ~month)
wyk %>%
add_trace(y = ~amount1,
type = "scatter",
mode = "lines+markers",
marker = list(color = "blue"),
line = list(color = "blue")) %>%
add_trace(y = ~amount1_forecast,
type = "scatter",
mode = "lines+markers",
marker = list(color = "blue"),
line = list(color = "blue", dash = "dot"),
showlegend = FALSE) %>%
add_trace(y = ~amount2,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
line = list(color = "red")) %>%
add_trace(y = ~amount2_forecast,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
line = list(color = "red", dash = "dot"),
showlegend = FALSE) %>%
layout(hovermode = "x") -> wyk
wyk
感谢任何想法!
PS 我知道,没有选项 hovermode = "x"
它可以工作,但我想保留它 ;)
好的,我想到了这样一个主意:
library("dplyr")
dane %>%
mutate(amount1_hover = ifelse(is.na(amount1_forecast), amount1, NA),
amount2_hover = ifelse(is.na(amount2_forecast), amount2, NA)) -> dane
wyk <- plot_ly(dane, x = ~month)
wyk %>%
add_trace(y = ~amount1,
type = "scatter",
mode = "lines+markers",
text = dane$amount1_hover,
hoverinfo = "text",
marker = list(color = "blue"),
line = list(color = "blue")) %>%
add_trace(y = ~amount1_forecast,
type = "scatter",
mode = "lines+markers",
text = ~amount1_forecast,
hoverinfo = "text",
marker = list(color = "blue"),
line = list(color = "blue", dash = "dot"),
showlegend = FALSE) %>%
add_trace(y = ~amount2,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
text = dane$amount2_hover,
hoverinfo = "text",
line = list(color = "red")) %>%
add_trace(y = ~amount2_forecast,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
text = ~amount2_forecast,
hoverinfo = "text",
line = list(color = "red", dash = "dot"),
showlegend = FALSE) %>%
layout(hovermode = "x")
但也许有人知道更好的解决方案!
我有一张图表显示了两种数据以及它们对未来几个月的预测。我使用 hovermode = "x"
同时悬停两行。一切正常,但......在 amount
等于 forecast
的部分,悬停加倍。这是很合乎逻辑的,但我想摆脱其中一个价值观。你知道怎么处理吗?
我的数据:
dane <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
amount1 = c(1086L, 1027L, 1024L, 1080L, 1072L, 1144L, NA, NA, NA, NA, NA, NA),
amount2 = c(1057, 1067, 1068, 1060, 1056, 1040, NA, NA, NA, NA, NA, NA),
amount1_forecast = c(NA, NA, NA, NA, NA, 1144L, 1119L, 1165L, 1145L, 1170L, 1158L, 1115L),
amount2_forecast = c(NA, NA, NA, NA, NA, 1040L, 1178L, 1122L, 1145L, 1158L, 1175L, 1119L)),
row.names = c(NA, -12L),
.Names = c("month", "amount1", "amount2", "amount1_forecast", "amount2_forecast"),
class = "data.frame")
我的剧情:
library("plotly")
wyk <- plot_ly(dane, x = ~month)
wyk %>%
add_trace(y = ~amount1,
type = "scatter",
mode = "lines+markers",
marker = list(color = "blue"),
line = list(color = "blue")) %>%
add_trace(y = ~amount1_forecast,
type = "scatter",
mode = "lines+markers",
marker = list(color = "blue"),
line = list(color = "blue", dash = "dot"),
showlegend = FALSE) %>%
add_trace(y = ~amount2,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
line = list(color = "red")) %>%
add_trace(y = ~amount2_forecast,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
line = list(color = "red", dash = "dot"),
showlegend = FALSE) %>%
layout(hovermode = "x") -> wyk
wyk
感谢任何想法!
PS 我知道,没有选项 hovermode = "x"
它可以工作,但我想保留它 ;)
好的,我想到了这样一个主意:
library("dplyr")
dane %>%
mutate(amount1_hover = ifelse(is.na(amount1_forecast), amount1, NA),
amount2_hover = ifelse(is.na(amount2_forecast), amount2, NA)) -> dane
wyk <- plot_ly(dane, x = ~month)
wyk %>%
add_trace(y = ~amount1,
type = "scatter",
mode = "lines+markers",
text = dane$amount1_hover,
hoverinfo = "text",
marker = list(color = "blue"),
line = list(color = "blue")) %>%
add_trace(y = ~amount1_forecast,
type = "scatter",
mode = "lines+markers",
text = ~amount1_forecast,
hoverinfo = "text",
marker = list(color = "blue"),
line = list(color = "blue", dash = "dot"),
showlegend = FALSE) %>%
add_trace(y = ~amount2,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
text = dane$amount2_hover,
hoverinfo = "text",
line = list(color = "red")) %>%
add_trace(y = ~amount2_forecast,
type = "scatter",
mode = "lines+markers",
marker = list(color = "red"),
text = ~amount2_forecast,
hoverinfo = "text",
line = list(color = "red", dash = "dot"),
showlegend = FALSE) %>%
layout(hovermode = "x")
但也许有人知道更好的解决方案!