Plotly: Updating Dropdown Data

I'm not sure if this is possible, but here is what I would like to do. I would like to update the data on the plot graph by selecting from the drop-down menu.

As a simple example, suppose I have a data frame

df <- data.frame(x = runif(200), y = runif(200), z = runif(200))

from which I use df$xand df$yin the scatterplot. Two data manipulation scenarios that I would like to use using the drop-down list:

  • Replace df$ywithdf$z
  • Select only the first nvalues df$xanddf$y

I examined the following two examples that I can easily reproduce: https://plot.ly/r/dropdowns/

However, I do not know how to transfer information about the data that will be built based on the choice of a drop-down list. For scenario 2, for example. I tried it with args = list("data", df[1:n,])one that didn't work.

In scenario 1, the path (only?) (According to the examples) seems to hide / show traces respectively. Is this the only way for scenario 2?

Any alternative ideas?

Update 1: Add Playable Example

So, here is an example that I would like to get in scenario 1.

require(plotly)
df <- data.frame(x = runif(200), y = runif(200), z = runif(200))
Sys.setenv("plotly_username"="xxx") #actual credentials replaced
Sys.setenv("plotly_api_key"="xxx") #actual credentials replaced

p <- plot_ly(df, x = df$x, y = df$y, mode = "markers", name = "A", visible = T) %>%
  add_trace(mode = "markers", y = df$z, name = "B", visible = T) %>%
  layout(
    title = "Drop down menus - Styling",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(TRUE, TRUE)),
               label = "Show All"),

          list(method = "restyle",
               args = list("visible", list(TRUE, FALSE)),
               label = "Show A"),

          list(method = "restyle",
               args = list("visible", list(FALSE, TRUE)),
               label = "Show B")))
    ))

plotly_POST(p)

The result is here: https://plot.ly/~spietrzyk/96/drop-down-menus-styling/ This is based on the example https://plot.ly/r/dropdowns/

However, I am wondering if it is possible to pass data for construction instead of triggering changes to the property of visibleindividual traces.

The only thing I tried was the following:

p <- plot_ly(df, x = df$x, y = df$y, mode = "markers", name = "A", visible = T) %>%
  layout(
    title = "Drop down menus - Styling",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(
      list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("y", df$y),
               label = "Show A"),
          list(method = "restyle",
               args = list("y", df$z),
               label = "Show B")))
))

: https://plot.ly/~spietrzyk/98/drop-down-menus-styling/ , df$z (https://plot.ly/~spietrzyk/99/).

, , , visible .

+3
1

, ?

require(plotly)
df <- data.frame(x = runif(200), y = runif(200), z = runif(200))
p <- plot_ly(df, x = ~x, y = ~y, mode = "markers", name = "A", visible = T) %>%
layout(
  title = "Drop down menus - Styling",
  xaxis = list(domain = c(0.1, 1)),
  yaxis = list(title = "y"),
  updatemenus = list(
    list(
      y = 0.7,
      buttons = list(
        list(method = "restyle",
             args = list("y", list(df$y)),  # put it in a list
             label = "Show A"),
        list(method = "restyle",
             args = list("y", list(df$z)),  # put it in a list
             label = "Show B")))
))
p
+4

Source: https://habr.com/ru/post/1657632/


All Articles