I am using Plotly to generate multiple bar charts based on a dropdown menu in R. My dataset contains hundreds of variables, and I am able to create the dropdown menu automatically thanks to this function I found here :
create_buttons <- function(df, y_axis_var_names) {
lapply(
y_axis_var_names,
FUN = function(var_name, df) {
button <- list(
method = 'restyle',
args = list('y', list(df[, var_name])),
label = sprintf(var_name) #'Show %s',
)
},
df
)
}
However, I am struggling to automatically add a line for each bar chart based on the variable selected in the dropdown menu. The dropdown menu itself is generated successfully, but I can't seem to find a way to add a line for each selected variable.
Here's an example dataset I am using:
structure(list(YW = structure(1:58, .Label = c("47-2021", "48-2021",
"49-2021", "50-2021", "51-2021", "52-2021", "1-2022", "2-2022",
"3-2022", "4-2022", "5-2022", "6-2022", "7-2022", "8-2022", "9-2022",
"10-2022", "11-2022", "12-2022", "13-2022", "14-2022", "15-2022",
"16-2022", "17-2022", "18-2022", "19-2022", "20-2022", "21-2022",
"22-2022", "23-2022", "24-2022", "25-2022", "26-2022", "27-2022",
"28-2022", "29-2022", "30-2022", "31-2022", "32-2022", "33-2022",
"34-2022", "35-2022", "36-2022", "37-2022", "38-2022", "39-2022",
"40-2022", "41-2022", "42-2022", "43-2022", "44-2022", "45-2022",
"46-2022", "47-2022", "48-2022", "49-2022", "50-2022", "51-2022",
"52-2022"), class = "factor"), X1 = c(0, 0, 0, 2, 0, 2, 0, 6,
3, 2, 2, 4, 0, 0, 0, 0, 2, 2, 2, 5, 2, 3, 2, 2, 2, 2, 0, 2, 0,
0, 2, 4, 4, 2, 2, 0, 0, 0, 2, 0, 6, 4, 4, 6, 6, 13, 2, 2, 0,
4, 2, 0, 3, 6, 4, 4, 0, 2), X2 = c(0, 0, 0, 2, 0, 2, 0, 6, 3,
2, 2, 4, 0, 0, 0, 4, 10, 2, 2, 5, 2, 3, 2, 2, 2, 2, 0, 2, 0,
0, 2, 4, 4, 2, 2, 0, 0, 2, 2, 0, 6, 6, 4, 6, 6, 13, 2, 2, 0,
4, 2, 0, 3, 6, 4, 4, 0, 2), X3 = c(0, 0, 0, 2, 0, 2, 0, 6, 2,
2, 2, 4, 2, 0, 0, 0, 2, 2, 2, 5, 2, 3, 2, 2, 2, 2, 0, 2, 0, 0,
2, 4, 4, 4, 2, 0, 0, 0, 2, 0, 6, 4, 4, 6, 6, 13, 2, 2, 0, 4,
4, 0, 3, 6, 4, 4, 0, 2), X4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA, -58L
))
p = plot_ly(data = test, x=~YW, y= ~X1 ,type ="bar", width = 1500, height = 500 )%>%
config(locale = 'fr')%>%
layout(autosize=T,
title = "",
xaxis = list(title = "Weeks"),
yaxis = list(title = "Frequency"),
updatemenus = list(
list(y = 0.7,
buttons = create_buttons(test, names(test[,!names(test) %in% "YW"])))))
I also have a separate dataframe that contains the values I want to use for the lines, where each column corresponds to a different variable. For example:
lines_x = data.frame(X1 = 5, X2 = 3, X3 = 4, X4 = 1)
My goal is to have a graph that looks like the one below, where the dropdown menu allows the user to select different variables and the lines update accordingly:
plot_ly(data = test, x=~YW, y= ~X1 ,type ="bar", width = 1500, height = 500 )%>%
add_lines(y= lines_x$X1)
How can I modify my code to achieve this? Thanks for the help !