I'm trying to draw a beta distribution in R using plotly. The goal is to get 2 sliders to select the a and b parameters of the function to see what is the effect of modifying one at the time.
I'm able to draw the graph and the sliders, but the only issue is that the sliders are actually not connected to the graph. I'm not seeing clear anymore. Perhaps it's just a little detail in the code, but it just doesn't want to connect the sliders to the "a" and "b" of the dbeta(x,a,b) in R.
# Load the library
library(plotly)
# Define the squences of X and the different values that a and b will take
x = seq(from = 0, to =1, by = 0.01)
# sequence for a and b
seqi = seq(0,5,by = .26)
# Define a and b for beta distributions
all_a <- list()
for (i in 1:length(seqi)) {
all_a[[i]] <- list(method = "relayout",
args = list(list(a = seqi[i])),
label = seqi[i])
}
all_b <- list()
for (i in 1:length(seqi)) {
all_b[[i]] <- list(method = "relayout",
args = list(list(b = seqi[i])),
label = seqi[i])
}
# Define the steps
steps <- list()
# Make empty plot
fig <- plot_ly()
# Empty list of available data
aval <- list()
for(step in 1:length(seqi)){
aval[[step]] <-list(visible = FALSE,
name = paste0('Density = ', step),
x=x,
y=dbeta(x,step,step)) # This is the beta density curve in R
}
aval[3][[1]]$visible = TRUE
# Draw the actual beta distribution
for (i in 1:length(seqi)) {
fig <- add_lines(p = 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
}
# Show the 2 slides of a and b to modify the average of the distribution
fig <- fig %>% layout(sliders = list(
list(
active = (length(x)-1),
currentvalue = list(prefix = "a: "),
pad = list(t = 20),
steps = all_a),
list(
active = (length(x)-1),
currentvalue = list(prefix = "b: "),
pad = list(t = 100),
steps = all_b)))
# Show the figure
fig
This gives this image:
But whenever I change the sliders, nothing happens...
