代码之家  ›  专栏  ›  技术社区  ›  SNT

更新按钮不适用于绘图仪图表

  •  1
  • SNT  · 技术社区  · 7 年前

    library(plotly)
    
    Animals <- c("giraffes", "orangutans", "monkeys")
    SF_Zoo <- c(20, 14, 23)
    LA_Zoo <- c(12, 18, 29)
    data <- data.frame(Animals, SF_Zoo, LA_Zoo)
    
    chart_types <- list(
      type = "buttons",
      direction = "right",
      xanchor = 'center',
      yanchor = "top",
      pad = list('r'= 0, 't'= 10, 'b' = 10),
      x = 0.5,
      y = 1.27,
      buttons = list(
            list(method = "restyle",
             args = list("type", "stack"),
             label = "Stack"),
        list(method = "restyle",
             args = list("type", "group"),
             label = "Group")
      ))
    
    p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
      add_trace(y = ~LA_Zoo, name = 'LA Zoo')%>% layout(updatemenus = list(chart_types))
    p 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   ismirsehregal    7 年前

    你就快到了。barmode(组/堆栈)是一个布局选项,不是不同的图表类型:

    library(plotly)
    
    Animals <- c("giraffes", "orangutans", "monkeys")
    SF_Zoo <- c(20, 14, 23)
    LA_Zoo <- c(12, 18, 29)
    data <- data.frame(Animals, SF_Zoo, LA_Zoo)
    
    chart_layouts <- list(
      type = "buttons",
      direction = "right",
      xanchor = 'center',
      yanchor = "top",
      pad = list('r'= 0, 't'= 10, 'b' = 10),
      x = 0.5,
      y = 1.27,
      buttons = list(
        list(method = "relayout",
             args = list("barmode", "stack"),
             label = "Stack"),
        list(method = "relayout",
             args = list("barmode", "group"),
             label = "Group")
      ))
    
    p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
      add_trace(y = ~LA_Zoo, name = 'LA Zoo') %>% layout(updatemenus = list(chart_layouts), barmode="group")
    p 
    

    因此,您需要使用 relayout 而不是 restyle

    documentation 详情请参阅。

    推荐文章