2017-04-26 62 views
0

我有一個圖表顯示兩種數據和他們的預測進一步幾個月。我使用hovermode = "x"在同一時間懸停兩行。而一切正常,但......在部分,其中amount等於forecast懸停加倍。這很合乎邏輯,但我想擺脫這個價值觀之一。你知道如何處理它嗎?擺脫雙重懸停在陰謀

enter image description here

我的數據:

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"它的工作原理,但我想保持它;)

回答

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") 

但也許有人知道解決方案更好