plotly:使用下拉选择项更新数据 [英] plotly: Updating data with dropdown selection

查看:325
本文介绍了plotly:使用下拉选择项更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可行,但这是我想做的.我想通过从下拉菜单中进行选择来更新plotly图中的数据.

作为一个简单的例子,让我们假设我有一个数据框

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

我在散点图中使用df$xdf$y

.我想使用下拉菜单实现两种数据处理方案:

  1. df$y替换为df$z
  2. 仅绘制df$xdf$y
  3. 的前n个值

我看了以下两个示例,可以很容易地重现它们: https://plot.ly/r/dropdowns/

但是,我不知道如何基于下拉选择传递有关要绘制的数据的信息.对于方案2,例如我已经用args = list("data", df[1:n,])尝试了,但没有用.

对于方案1(根据示例),走的(唯一的?)方法似乎分别是隐藏/显示迹线.这也是场景2的唯一方法吗?

还有其他想法吗?

更新1:添加可复制的示例

所以这是一个示例,可以实现我在场景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)

结果在这里: https://plot.ly/~spietrzyk /96/drop-down-menus-styleling/ 这是基于 https://plot.ly/r/dropdowns/ 中的示例

但是,我想知道是否可以传递要绘制的数据,而不是触发对单个迹线的visible属性的更改.

我尝试过的一件事是:

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-styleling/ 此方法无法正常工作,因为df$z中的数据未发布到网格( https://plot .ly/〜spietrzyk/99/).

因此,我想知道是否仍然可以根据下拉选择来操纵要绘制的数据,而不是绘制所有迹线,而不是通过下拉选择来切换visible属性.

解决方案

这是你的追求吗?

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

I am not sure if this is possible, but here is what I would like to do. I would like to update the data in a plotly plot by selecting from a dropdown menu.

As a simple example, let's assume I have a data frame

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

from which I use df$x and df$y in a scatter plot. Two scenarios of data manipulation I would like to achieve using a dropdown:

  1. Replace df$y with df$z
  2. Plot only the first n values of df$x and df$y

I looked at the following two examples, which I can easily reproduce: https://plot.ly/r/dropdowns/

However, I have no idea how to pass the information regarding the data to be plotted based on the dropdown selection. For scenario 2 e.g. I have tried it with args = list("data", df[1:n,]) which did not work.

For scenario 1 the (only?) way to go (according to the examples) seems to be hiding/showing the traces respectively. Is that the only way for scenario 2 as well?

Any alternative ideas?

Update 1: Add reproducible example

So here is an example which achieve what I would like 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)

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

However, I am wondering if one could pass the data to be plotted instead of triggering changes to the visible property of individual traces.

The one 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")))
))

Result here: https://plot.ly/~spietrzyk/98/drop-down-menus-styling/ This approach cannot work, as the data from df$z is not posted to the grid (https://plot.ly/~spietrzyk/99/).

So I was wondering is there anyway to manipulate the data to be plotted based on dropdown selection, beyond plotting all traces and than switching the visible property by dropdown selections.

解决方案

Is this what you were after?

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

这篇关于plotly:使用下拉选择项更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆