与plotly R 等效的下拉菜单

问题描述 投票:0回答:1

我想复制 @It_is_Chris answer 来执行一个情节下拉菜单,但使用 R。然而,这非常具有挑战性,因为我得到了几个奇怪的结果。 我想知道是否有人可以帮助我!

我的数据存储在这里

我能做的最好的是:


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")

我设法获得一个下拉菜单,但图层重叠,当我从下拉菜单中选择另一个变量时,绘图结果为空!

提前致谢!

r plotly
1个回答
0
投票

我认为这样的东西可以让你开始使用 R 中的下拉菜单:

library(plotly)
library(dplyr)

df <- mtcars

fig <- df |> 
  plot_ly(x = ~hp, y = ~drat, alpha = 0.3) |> 
  add_markers(marker = list(line = list(color = "black", width = 1))) |> 
  layout(
  title = "Plotly Dropdown",
  xaxis = list(domain = c(0.1, 1)),
  yaxis = list(title = "y"),
  updatemenus = list(
    list(
      y = 0.8,
      buttons = list(
        
        list(method = "restyle",
             args = list("type", "scatter"),
             label = "Scatter"),
        
        list(method = "restyle",
             args = list("type", "histogram2d"),
             label = "Histogram")))
  ))

fig

enter image description here

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.