2017-03-07 31 views
0

我有一個數據集,看起來像這樣:我在鏈接chart瞄準圖「奧運獎牌」[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 

,它是在plolty通過Python實現我試圖在R中複製。我所能做的是this
在第一個窗口中它看起來很相似,但我沒有包含所有行的下拉菜單「全部」。目前沒有辦法讓所有的線條與第一個有「選項」選項的圖表不同。下面的代碼顯示我是如何創建我的第二個情節任何幫助是極大的讚賞。

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值,最好隱藏/顯示座標軸。您可以通過將數組傳遞給visible屬性來陰謀隱藏和顯示軸,例如args = list('visible', c(TRUE, FALSE, FALSE))將顯示第一個軸並隱藏另外兩個。

請在下方查看完整代碼或here在線。

將散點圖更改爲條形圖會在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