plot - R + Plotly: multiple sliders -
i want create interactive graph plotly , r analogous sine wave example on plotly website. instead of having 1 slider changes frequency of sine wave, want include second slider changes amplitude of wave. copy of code plotly website, including 1 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 , plot traces steps <- list() p <- plot_ly() (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) step <- list(args = list('visible', rep(false, length(aval))), method = 'restyle') step$args[[2]][i] = true steps[[i]] = step } # add slider control plot p <- p %>% layout(sliders = list(list(active = 3, currentvalue = list(prefix = "frequency: "), steps = steps))) suppose in addition 11 sine waves of different frequencies want add slider changes amplitude of 1 (by default) 2. add new data aval:
aval <- list(list(), list()) (amplitude in 1:2){ for(step in 1:11){ aval[[amplitude]][[step]] <-list(visible = false, name = paste0('v = ', step), x=x, y=amplitude*sin(step*x)) } } however how go adding second slider give me option change amplitude of sine wave? i'm not concerned adding new traces plotly object i'm unsure on how define step , layout(sliders = ...)
the plotly website gives example of plotly object 2 sliders (scroll down) it's more complex , i'm unable adjust simpler needs. suggestions on start?
ps. tried looking tutorials on sliders can hardly find any...
Comments
Post a Comment