Home > Software design >  Adding a static element across slider steps in a R plotly graph
Adding a static element across slider steps in a R plotly graph

Time:07-27

I am giving a tutorial on MLE and am trying to figure out how to add a static grouping of points to a plotly graph. Obviously the idea is that as I slide the normal distributions over you can see that the points correspond to a lower or higher likeihood. However, I can only get the points to appear on the first frame.

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

aval <- list()
for (step in 1:6) {
  aval[[step]] <- list(
    visible = FALSE,
    name = paste0('v = ', step),
    x = x,
    y = dnorm(x, step 1)
  )
  
}

aval[3][[1]]$visible = TRUE

steps <- list()
fig <- plot_ly()

for (i in 1:6) {
  fig <-
    add_lines(
      fig,
      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
  
}


fig <- fig %>% add_markers(x = c(4.5,5,5.5), y = c(0,0,0))
# add slider control to plot

fig <- fig %>%
  layout(sliders = list(list(
    active = 0,
    currentvalue = list(prefix = "Frequency: "),
    steps = steps
  )))


fig

CodePudding user response:

Why don't you use plotly's result

  • Related