2017-04-26 3 views
0

私は、さらに2ヶ月間、2種類のデータとその予測を示すグラフを持っています。私はhovermode = "x"を使用して、同じ時間に2行のホバーを取得します。そしてすべてが機能しますが...部分では、amountforecastに等しく、ホバーは2倍になります。これはかなり論理的ですが、私はこの値の1つを取り除きたいと思います。あなたはそれに対処する方法を知っていますか?プロットしてダブルホバーを取り除く

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 

ありがとう! ;オプションhovermode = "x"せず、それは動作しますが、私はそれを維持したいと私は知っている

PS)

答えて

1

[OK]を、私はそのようなアイデアを思い付いてきました:

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

しかし、おそらく誰かが知っていますより良い解決策!

関連する問題