Update buttons not working on plotly R charts

I am trying to use the update buttons in plotly r charts where in the user can change the chart type to group or stacked. The group bar chart works for the first time but stacked never works. Below is the code I am using.

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

I'm not totally sure what you mean by a chart type style of group, but for stacked and regular bar chats, the following should work for you;

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)

layout_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 = "relayout",
         args = list("barmode", "bar"),
         label = "Bar"),
    list(method = "relayout",
         args = list("barmode", 'stack'),
         label = "Stack")
  ))

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(layout_types))
p


image


Here's great guidance on dealing with buttons

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.