本文介绍了R 中 Plotly 下拉菜单中每个按钮的多行/轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to generate multiple graphs in Plotly for 30 different sales offices. Each graph would have 3 lines: sales, COGS, and inventory. I would like to keep this on one graph with 30 buttons for the different offices. This is the closest solution I could find on 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 个或更多轨迹的图形?或者有更好的解决方案吗?任何帮助将不胜感激!

It works but only on graphs with single lines/traces. How can I modify this code to do the same thing but with graphs with 2 or more traces? or is there a better solution? Any help would be appreciated!

### 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

推荐答案

你们非常接近!例如,如果您想要具有 3 条轨迹的图形,你只需要调整两件事:

You were very close! If for example you want graphs with 3 traces,You only need to tweak two things:

  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 个图

PD: I only use 99 cols because I want 33 groups of 3 graphs

这篇关于R 中 Plotly 下拉菜单中每个按钮的多行/轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:48