Setting initial plot to be a subset of input data using buttons and drop downs

I am trying to create a chart which has dropdowns included. These will select specified values of one of the plotted variables to give 11 different plots.
I am able to use updatemenus and butons to create the dropdowns and they all select the correct observations when selected. However, because I am limited to using one input dataset, the initial view of the plot contains all the data in the input dataset - I do not want this, I would like my initial view to be of a subset of the data. Is this possible? I have tried to subset the initial data argument in at the beginning of the plot_ly statement but to no avail.

If it is not possible to give an initial view of a subset of the data, is it possible to use dropdowns and buttons to select different datasets? That way i could have 11 separate datasets and cycle between them with the dropdowns.

A sample of mu code is below:

l <- list(x = 1, y = 1, bgcolor = “#E2E2E2”, bordercolor = “#FFFFFF”, borderwidth = 2)
m <- list(b = 120)

p <- plot_ly(test_full) %>%

add_trace(
x = ~ S_NO,
y = ~ U_L_n,
type = ‘scatter’,
mode = ‘lines’,
name = ‘Control Limits’,
line = list(color = ‘rgba(190, 190, 190, .9)’, width = 1),
opacity = 0.8,
hoverinfo = ‘none’

) %>%

add_trace(
x = ~ S_NO,
y = ~ L_L_n,
type = ‘scatter’,
mode = ‘lines’,
showlegend = FALSE,
line = list(color = ‘rgba(190, 190, 190, .9)’, width = 1),
opacity = 0.8,
hoverinfo = ‘none’

) %>%

add_trace(
x = ~ S_NO,
y = ~ V_n,
type = “scatter”,
mode = “lines”,
name = “Score”,
line = list(color = ‘rgba(80, 134, 81, .9)’, width = 1),

) %>%

layout(
updatemenus = list(list(
buttons = list(
list(
method = “restyle”,
args = list(“x”, list(
subset(test_full$S_NO, test_full[, 12] == 99)
)),
label = “Show A”
),
list(
method = “restyle”,
args = list(“x”, list(
subset(test_full$S_NO, test_full[, 12] == 101)
)),
label = “Show B”
)
)
)),
xaxis = list(
title = “S number (chronological)”,
titlefont = list(size = 20),
rangeslider = list(type = “date”)
),
yaxis = list(title = “Cumulative Score”, titlefont = list(size = 20)),
legend = l,
margin = m,
hovermode = “x”

)

Hey @kyleh90

Sounds like you may be after transforms https://plot.ly/r/#transforms which can be used with dropdowns like https://plot.ly/r/aggregations/#aggregate-functions More specifically, you may want to use filters:

https://plot.ly/~bdun9/2319/

p <- plot_ly(
  type = 'scatter',
  x = mtcars$hp,
  y = mtcars$qsec,
  text = rownames(mtcars),
  hoverinfo = 'text',
  mode = 'markers',
  transforms = list(
    list(
      type = 'filter',
      target = 'y',
      operation = '>',
      value = mean(mtcars$qsec)
    )
  )
) %>% layout(
  updatemenus = list(
    list(
      type = 'dropdown',
      active = 1,
      buttons = list(
        list(method = "restyle",
             args = list("transforms[0].value", 0),
             label = "All"),
        list(method = "restyle",
             args = list("transforms[0].value", mean(mtcars$qsec)),
             label = "> mean")
      )
    )
  )
)

Branden,

Thanks for this, the example code seems to do the specific actions I need. However, and bear with me as I am new to both R and Plotly, I am a little confused about what the “transforms” function is doing in the first piece. Is it setting a value which is then transformed in the layout statement later on? I am unsure how I would combine this functionality with my above code. I have my code using args to subset the values which x will plot, I just want my initial plot to show one of those subsets and not all values of x. You seem to have achieved this in your example as the mean filter appears as the default view but I am not sure what part of the code is doing that. If possible I would like the full data not to appear at all and just the subsets.

I hope this makes sense,

Apologies for my lack of understanding!

Kyle

Not sure but this post may help: ScatterGeo with a slider