I wanted to replicate @It_is_Chris answer to perform a plotly dropdown menu but using R. However it’s quite challenging because I got several strange results.
I was wondering if somebody could help me!
My data are stored here
Best I could do is:
<code>
grps <- split(df, df$centroid)
buttons <- list()
figs <- list()
# Iterate over the groupby object
for (i in seq_along(grps)) {
name <- names(grps)[i]
visible <- rep(FALSE, length(grps) * 3)
visible[(i - 1) * 3 + 1 : i * 3] <- TRUE
fig <- plot_ly(grps[[i]], x = ~chrPos, y = ~depth_total, type = 'scatter', mode = 'lines', color = I('#333131'))
fig <- fig %>% add_bars(x = ~chrPos, y = ~depth_5, name = "5' clusters", marker = list(color = 'forestgreen'))
fig <- fig %>% add_bars(x = ~chrPos, y = ~depth_3, name = "3' clusters", marker = list(color = 'firebrick'))
if (i == 1) {
fig <- fig %>% layout(title = paste("Cluster n°:", name))
}
figs[[i]] <- fig
buttons[[i]] <- list(label = paste("Cluster", name),
method = "update",
args = list(list(visible = visible),
list(title = paste("Cluster n°:", name))))
}
updatemenus <- list(list(active = 0, buttons = buttons, showactive = TRUE))
figs <- figs %>% subplot(nrows = 1, margin = 0.05) %>% layout(title = "Cluster n°: ", title_x = 0.5, updatemenus = updatemenus, template = "plotly_white")
</code>
<code>
grps <- split(df, df$centroid)
buttons <- list()
figs <- list()
# Iterate over the groupby object
for (i in seq_along(grps)) {
name <- names(grps)[i]
visible <- rep(FALSE, length(grps) * 3)
visible[(i - 1) * 3 + 1 : i * 3] <- TRUE
fig <- plot_ly(grps[[i]], x = ~chrPos, y = ~depth_total, type = 'scatter', mode = 'lines', color = I('#333131'))
fig <- fig %>% add_bars(x = ~chrPos, y = ~depth_5, name = "5' clusters", marker = list(color = 'forestgreen'))
fig <- fig %>% add_bars(x = ~chrPos, y = ~depth_3, name = "3' clusters", marker = list(color = 'firebrick'))
if (i == 1) {
fig <- fig %>% layout(title = paste("Cluster n°:", name))
}
figs[[i]] <- fig
buttons[[i]] <- list(label = paste("Cluster", name),
method = "update",
args = list(list(visible = visible),
list(title = paste("Cluster n°:", name))))
}
updatemenus <- list(list(active = 0, buttons = buttons, showactive = TRUE))
figs <- figs %>% subplot(nrows = 1, margin = 0.05) %>% layout(title = "Cluster n°: ", title_x = 0.5, updatemenus = updatemenus, template = "plotly_white")
</code>
grps <- split(df, df$centroid)
buttons <- list()
figs <- list()
# Iterate over the groupby object
for (i in seq_along(grps)) {
name <- names(grps)[i]
visible <- rep(FALSE, length(grps) * 3)
visible[(i - 1) * 3 + 1 : i * 3] <- TRUE
fig <- plot_ly(grps[[i]], x = ~chrPos, y = ~depth_total, type = 'scatter', mode = 'lines', color = I('#333131'))
fig <- fig %>% add_bars(x = ~chrPos, y = ~depth_5, name = "5' clusters", marker = list(color = 'forestgreen'))
fig <- fig %>% add_bars(x = ~chrPos, y = ~depth_3, name = "3' clusters", marker = list(color = 'firebrick'))
if (i == 1) {
fig <- fig %>% layout(title = paste("Cluster n°:", name))
}
figs[[i]] <- fig
buttons[[i]] <- list(label = paste("Cluster", name),
method = "update",
args = list(list(visible = visible),
list(title = paste("Cluster n°:", name))))
}
updatemenus <- list(list(active = 0, buttons = buttons, showactive = TRUE))
figs <- figs %>% subplot(nrows = 1, margin = 0.05) %>% layout(title = "Cluster n°: ", title_x = 0.5, updatemenus = updatemenus, template = "plotly_white")
I managed to get a dropdown menu but layers overlap and when I select another variable from the dropdown menu, the plot results emtpy!
Thanks in advance!