2017-03-07 18 views
0

が、私はこのようになりますデータセット持っているドロップダウンメニューで複数の折れ線グラフを追加:RはPlotly

date  food transp housing other 
0 2015-06-01 1510 45.58 0  101.5 
1 2015-07-01 1163.91 74.14 210  106.7 
2 2015-08-01 101.3 95.03 210  54.5 
3 2015-09-01 1131.67 22.28 210  46.3 
4 2015-10-01 818.44 88.88 815.2 47.2 

私はリンクchartにチャート「オリンピックのメダル獲得」を目指していますが、それはパイソンを経由してploltyに実装されていますRで複製しようとしていますthis
最初のグレースでは類似していますが、すべての行を含むドロップダウンメニュー 'All'はありません。現在、オプション「All」がある最初のチャートとは異なり、すべてのラインを含むプロットを戻す方法はありません。次のコードは、私が2番目のプロットをどのようどんな助けでも大歓迎です。

plot <- plot_ly(newf, x = ~round_date, y = ~other, name='other', type='scatter',mode='lines+markers') %>% 
add_trace(y = ~food, name = 'food') %>% 
add_trace(y = ~housing, name = 'housing') %>% 
add_trace(y = ~transport, name = 'transport') %>% 
    layout(
title = "Button Restyle", 
xaxis = list(domain = c(0.1, 1)), 
yaxis = list(title = "y"), 
updatemenus = list(
    list(
    type = "buttons", 
    y = 0.8, 
    label = 'Category', 
    buttons = list(
    list(method = "restyle", 
     args = list("y",list(~other)), 
      label = "houshold"), 

    list(method = "restyle", 
     args = list("y",list(~food)), 
      label = "communication"), 

    list(method = "restyle", 
     args = list("y",list(~housing)), 
      label = "food"), 

    list(method = "restyle", 
     args = list("y",list(~transport)), 
      label = "transport") 

      )))) 

答えて

1

y値を変更する代わりに、軸を表示/非表示にすることをお勧めします。可視属性に配列を渡すことで、軸をプロットして非表示にすることができます。 args = list('visible', c(TRUE, FALSE, FALSE))は、最初の軸を示し、他の2つを非表示にします。

完全コードはをご覧ください。

散布図から棒グラフに変更すると、y値が変更されたときにスタイル変更が失敗するため、色は異なるがy値が同じ4つの棒が得られます。

library(plotly) 
plot_ly(mtcars, x = rownames(mtcars), y = ~mpg, name='mpg', type='scatter', mode='markers') %>% 
    add_trace(y = ~hp, name = 'power', type='scatter', mode='markers') %>% 
    add_trace(y = ~qsec, name = 'qsec', type='scatter', mode='markers') %>% 
    layout(
    updatemenus = list(
     list(
     type = "buttons", 
     x = -0.1, 
     y = 0.6, 
     label = 'Category', 
     buttons = list(
      list(method = "restyle", 
       args = list('visible', c(TRUE, TRUE, TRUE)), 
       label = "View all"), 
      list(method = "restyle", 
      args = list('visible', c(FALSE, FALSE, FALSE)), 
      label = "Hide all") 
     ) 
    ), 
     list(
     type = "buttons", 
     x = -0.1, 
     y = 0.7, 
     label = 'Category', 
     buttons = list(
      list(method = "restyle", 
       args = list('visible', c(TRUE, FALSE, FALSE)), 
       label = "mpg"), 
      list(method = "restyle", 
       args = list('visible', c(FALSE, TRUE, FALSE)), 
       label = "hp"), 
      list(method = "restyle", 
       args = list('visible', c(FALSE, FALSE, TRUE)), 
       label = "qsec") 
     ) 
    ) 
    ) 
) 
+0

ありがとう、私はちょうど私が軸を隠して表示できるかどうか分からなかった。 – Biranjan

関連する問題