我有一张图表,显示了两种数据以及未来几个月的预测。我使用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"
它有效,但我想保留它;)
答案 0 :(得分:1)
好的,我已经提出了这样一个想法:
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")
但也许有人知道更好的解决方案!