如何使用plotly滑块更新图表标题

时间:2019-09-25 09:28:59

标签: r plot slider plotly

我制作了一个带有滑块的图形,该图形紧紧跟随这个例子。 'Sine Wave Slider'

我真的很想添加一个标题,该标题会随着滑块中的每个步骤而变化,但是当滑块移动时却无法将其更新为“更新”(我只是得到了与每个步骤相同的标准标题) 。有人可以帮忙吗?

这是示例代码:

    library(plotly)

    x <- seq(0,10, length.out = 1000)

    # create data
    aval <- list()
    for(step in 1:11){
      aval[[step]] <-list(visible = FALSE,
                          name = paste0('v = ', step),
                          x=x,
                          y=sin(step*x))
    }
    aval[3][[1]]$visible = TRUE

    # create steps and plot all traces
    steps <- list()
    p <- plot_ly()
    for (i in 1:11) {
      p <- add_lines(p,x=aval[i][[1]]$x,  y=aval[i][[1]]$y, visible = aval[i][[1]]$visible, 
                     name = aval[i][[1]]$name, type = 'scatter', mode = 'lines', hoverinfo = 'name', 
                     line=list(color='00CED1'), showlegend = FALSE)%>%
layout(title = paste("this is chart",i)) # this line adds the title but doesn't update with the slider

      step <- list(args = list('visible', rep(FALSE, length(aval))),
                   method = 'restyle')
      step$args[[2]][i] = TRUE  
      steps[[i]] = step 
    }  

    # add slider control to plot
    p <- p %>%
      layout(sliders = list(list(active = 3,
                                 currentvalue = list(prefix = "Frequency: "),
                                 steps = steps)))

0 个答案:

没有答案