Используя plotly в R, обновите видимость форм и графиков - PullRequest
0 голосов
/ 24 октября 2018

Я пытаюсь обновить как форму, так и график, используя график с помощью кнопок.Когда я нажимаю кнопку 1, появляются shape1 и plot1;в то время как кнопка2 вызовет появление shape2 и plot2.Я пытался и с обновлением и с ретрансляцией, но ни один не работал.

Любая помощь приветствуется.

data1 <- runif(100,0,1000)
data2 <- runif(5,0,1000)
data3 <- runif(100,0,1000)
data4 <- runif(500,0,1000)

p <- plot_ly() %>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data1, visible=T, marker = list(color = 'blue'))  %>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data2, visible=F, marker = list(color = 'red'))%>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data3, visible=T, marker = list(color = 'gray'))  %>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data4, visible=F, marker = list(color = 'black')) %>%
  layout(title ="Data12",
    updatemenus = list(
      list(type='buttons',

        buttons = list(
          list(method = "update",
               args = list(list(visible=c(T, F,T,F)),list(title 
          ="Data12"),list(shapes=c(type="rect",x0=0,x1=100, xref="x",
                                y0=0,y1=100, yref="y",fillcolor = "green", 
         line = list(color = "green")))),

               label = 'data12'),

          list(method = "update",
               args = list(list(visible=c(F,T,F,T)),list(title 
               ="Data34"),list(shapes=c(type="rect",x0=0,x1=100, xref="x",
                                           y0=0,y1=100, yref="y",fillcolor = 
               "blue", line = list(color = "blue")))),
               label = 'data34')
        ))))
p

1 Ответ

0 голосов
/ 25 октября 2018

Может быть, вы могли бы попробовать это:

library(plotly)
shapes12 = list(type="rect",x0=0,x1=100, xref="x",
           y0=0,y1=100, yref="y",fillcolor = "green", 
           line = list(color = "green"))
shapes34 = list(type="rect",x0=0,x1=100, xref="x",
           y0=0,y1=100, yref="y",fillcolor = "blue", 
           line = list(color = "blue"))

plot_ly() %>%
   add_trace(type = 'scatter', mode = 'markers', name = "Data1",
                y = data1, marker = list(color = 'blue'))  %>%
   add_trace(type = 'scatter', mode = 'markers', name = "Data2",
                y = data2, marker = list(color = 'red'))%>%
   add_trace(type = 'scatter', mode = 'markers', name = "Data3",
                y = data3,  marker = list(color = 'gray'))  %>%
   add_trace(type = 'scatter', mode = 'markers', name = "Data4",
                y = data4, marker = list(color = 'black')) %>%
   layout(updatemenus = list(
               list(type='buttons',     
                    buttons = list(
                      list(method = "update",
                           args = list(list(visible=c(T, T,F,F)),list(title ="Data12", shapes = list(shapes12))),
                           label = 'data12'),
                      list(method = "update",
                           args = list(list(visible=c(F,F,T,T)),list(title ="Data34", shapes = list(shapes34))),
                           label = 'data34')
                    )))
             )
...