【问题标题】:Multiple lines/traces for each button in a Plotly drop down menu in RR中的Plotly下拉菜单中每个按钮的多行/跟踪
【发布时间】:2019-07-18 09:25:32
【问题描述】:

我正在尝试在 Plotly 中为 30 个不同的销售办事处生成多个图表。每个图表将有 3 条线:销售额、销货成本和库存。我想将此保留在一张图表上,其中包含 30 个按钮,用于不同的办公室。这是我在 SO 上能找到的最接近的解决方案:

 ## Create random data. cols holds the parameter that should be switched
l <- lapply(1:100, function(i) rnorm(100))
df <- as.data.frame(l)
cols <- paste0(letters, 1:100)
colnames(df) <- cols
df[["c"]] <- 1:100

## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly(df,
      type = "scatter",
      mode = "lines",
      x = ~c, 
      y= ~df[[cols[[1]]]], 
      name = cols[[1]])
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[-1]) {
  p <- p %>% add_lines(x = ~c, y = df[[col]], name = col, visible = FALSE)
}

p <- p %>%
    layout(
      title = "Dropdown line plot",
      xaxis = list(title = "x"),
      yaxis = list(title = "y"),
      updatemenus = list(
        list(
            y = 0.7,
            ## Add all buttons at once
            buttons = lapply(cols, function(col) {
              list(method="restyle", 
                args = list("visible", cols == col),
                label = col)
            })
        )
      )
    )

print(p)

它有效,但仅适用于具有单线/迹线的图形。如何修改此代码以执行相同的操作,但使用具有 2 个或更多跟踪的图形?还是有更好的解决方案?任何帮助将不胜感激!

### EXAMPLE 2

#create fake time series data
library(plotly)
set.seed(1)
df <- data.frame(replicate(31,sample(200:500,24,rep=TRUE)))
cols <- paste0(letters, 1:31)
colnames(df) <- cols


#create time series

timeseries <- ts(df[[1]], start = c(2018,1), end = c(2019,12), frequency = 12)

fit <- auto.arima(timeseries, d=1, D=1, stepwise =FALSE, approximation = FALSE)
fore <- forecast(fit, h = 12, level = c(80, 95))


## Add trace directly here, since plotly adds a blank trace otherwise

p <- plot_ly() %>%
  add_lines(x = time(timeseries), y = timeseries,
            color = I("black"), name = "observed") %>%
  add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
              color = I("gray95"), name = "95% confidence") %>%
  add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
              color = I("gray80"), name = "80% confidence") %>%
  add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction")


## Add arbitrary number of traces
## Ignore first col as it has already been added

for (col in cols[2:31]) {


  timeseries <- ts(df[[col]], start = c(2018,1), end = c(2019,12), frequency = 12)


  fit <- auto.arima(timeseries, d=1, D=1, stepwise =FALSE, approximation = FALSE)
  fore <- forecast(fit, h = 12, level = c(80, 95))

  p <- p %>%
    add_lines(x = time(timeseries), y = timeseries,
              color = I("black"), name = "observed", visible = FALSE) %>%
    add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
                color = I("gray95"), name = "95% confidence", visible = FALSE) %>%
    add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
                color = I("gray80"), name = "80% confidence", visible = FALSE) %>%
    add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction", visible = FALSE)

}

p <- p %>%
  layout(
    title = "Dropdown line plot",
    xaxis = list(title = "x"),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.7,
        ## Add all buttons at once
        buttons = lapply(cols, function(col) {
          list(method="restyle", 
               args = list("visible", cols == col),
               label = col)
        })
      )
    )
  )
p

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    你很亲密! 例如,如果您想要带有 3 条轨迹的图形, 你只需要调整两件事:

    1. 设置前三个迹线可见,
    2. 修改按钮以显示三个一组的轨迹。

    我的代码:

    ## Create random data. cols holds the parameter that should be switched
    library(plotly)
    l <- lapply(1:99, function(i) rnorm(100))
    df <- as.data.frame(l)
    cols <- paste0(letters, 1:99)
    colnames(df) <- cols
    df[["c"]] <- 1:100
    
    ## Add trace directly here, since plotly adds a blank trace otherwise
    p <- plot_ly(df,
                 type = "scatter",
                 mode = "lines",
                 x = ~c, 
                 y= ~df[[cols[[1]]]], 
                 name = cols[[1]])
    p <- p %>% add_lines(x = ~c, y = df[[2]], name =  cols[[2]], visible = T)
    p <- p %>% add_lines(x = ~c, y = df[[3]], name =  cols[[3]], visible = T)
    ## Add arbitrary number of traces
    ## Ignore first col as it has already been added
    for (col in cols[4:99]) {
      print(col)
      p <- p %>% add_lines(x = ~c, y = df[[col]], name = col, visible = F)
    }
    
    p <- p %>%
      layout(
        title = "Dropdown line plot",
        xaxis = list(title = "x"),
        yaxis = list(title = "y"),
        updatemenus = list(
          list(
            y = 0.7,
            ## Add all buttons at once
            buttons = lapply(0:32, function(col) {
              list(method="restyle", 
                   args = list("visible", cols == c(cols[col*3+1],cols[col*3+2],cols[col*3+3])),
                   label = paste0(cols[col*3+1], " ",cols[col*3+2], " ",cols[col*3+3] ))
            })
          )
        )
      )
    
    print(p)
    

    PD:我只使用 99 列,因为我想要 33 组 3 个图表

    【讨论】:

    • @JohnnyCrunch 原帖中的第二个例子怎么样?它适用于时间序列数据,具有观测值、预测值和 80 95% 置信区间的线。我尝试将您的解决方案应用于上述问题,但按钮仍然偏移。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多