你就快到了。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
详情请参阅。