This is a question following up on this answer.
I integrated a selection button to a sunburst plot. I would like to use this button to also update the plot title based on the chosen column. In the plotly-R documentation it is stated, to use the '"update"' method should be used when modifying the data and layout sections of the graph So based on this information I tried
args=list(list(values=list(df2[, var_name]),layout.title.text=pate0("Zeitraum: ",sprintf('%s',var_name))))
in the following code
library(plotly)
library(dplyr)
df2 <- data.frame(KtoID = c(66,661,6610,6611,6612,6613,6614,6615,6616,6617,6618,6619,6651,6691),
label=c("A","B","Ba","Bc","Bd","Be","Bf","Bg","Bh","Bi","Bk","Bl","C","D"),
value_2020=c(208800494,3669502,NA,NA,14,NA,NA,940704,91560,2614368,22856,NA,163422732,41708260),
value_2019=c(202946326,14278132,NA,42,50,NA,NA,703978,129434,13417048,27580,NA,153535498,35132696),
parent_ID= c(NA,66,661,661,661,661,661,661,661,661,661,661,66,66))
y_axis_var_names <- c('value_2020', 'value_2019')
create_buttons2 <- function(df2, y_axis_var_names) {
lapply(y_axis_var_names,
FUN = function(var_name, df2) {
button <- list(
#method = 'restyle',
method = "update", #updating data and layout
#args = list('values', list(df2[, var_name])), #update of data
args=list(list(values=list(df2[, var_name]),layout.title.text=pate0("Zeitraum: ",sprintf('%s',var_name))))
label = gsub("value_","",sprintf('Year %s',var_name))
)},
df2)}
fig <- plot_ly(ids=df2$KtoID,
labels = df2$label,
parents = df2$parent_ID,
values = df2$value_2020,
type = 'sunburst',
maxdepth=2,
insidetextorientation="horizontal",
branchvalues = 'total')
fig <- fig %>% layout(title=list(text= "Zeitraum: 2020"),
separators = ', ',
autosize = T,
hoverlabel=list(bgcolor='#ffffff',
bordercolor = '#dcdcdc',
align = "left",
font = list(color="#000000"),
size=12),
updatemenus = list(
list(
y = 0.95,
x = 0.7,
xanchor = "left",
font= list(size = 12,
color = "#000000"),
bordercolor="#a3a3a3",
borderwidth = 1.5,
buttons = create_buttons2(df2, y_axis_var_names))
))
fig
This still works for the data, but not for the title. Has anybody an idea what is wrong with my code or a better idea?
Thanks in advance.