【问题标题】:Dropdown menu to select x, y and color (plotly)用于选择 x、y 和颜色的下拉菜单(绘图)
【发布时间】:2017-04-04 19:00:30
【问题描述】:

我正在尝试创建一个带有可选 x、y 和颜色变量的绘图图,部分基于this previous question。 The x and y variable selection appears to work, however when new x and y variables are selected, the point color is lost.

此外,我尝试使用类似的策略来选择点颜色,但不幸的是这似乎不起作用。

另一种选择是在之前链接的问题中使用“设置可见”策略。

示例:

library(plotly)
library(pcaMethods)

pca <- pcaMethods::pca(mtcars, nPcs=3)

df <- as.data.frame(pca@scores)

colors1 <- sample(c("red", "green", "blue"), nrow(df), replace=TRUE)
colors2 <- sample(c("red", "green", "blue"), nrow(df), replace=TRUE)

p <- plotly::plot_ly(df, x = ~PC1, y = ~PC2, type = "scatter",
    color = sample(c("red", "green", "blue"), nrow(df), replace=TRUE),
    mode = "markers") 

p <-  plotly::layout(
    p,
    title = "Dropdown PCA plot",
    updatemenus = list(
        list(
            y = 0.7,
            buttons = list(
                list(method = "restyle",
                   args = list(
                    "x", list(df$PC1)
                    ),
                   label = "PC1"),
                list(method = "restyle",
                   args = list(
                    "x", list(df$PC2)
                    ),
                   label = "PC2"),
                list(method = "restyle",
                   args = list(
                    "x", list(df$PC3)
                    ),
                   label = "PC3")
                )
            ),
        list(
            y = 0.5,
            buttons = list(
                list(method = "restyle",
                   args = list(
                    "y", list(df$PC1)
                    ),
                   label = "PC1"),
                list(method = "restyle",
                   args = list(
                    "y", list(df$PC2)
                    ),
                   label = "PC2"),
                list(method = "restyle",
                   args = list(
                    "y", list(df$PC3)
                    ),
                   label = "PC3")
                )
            )
        )
    )

htmlwidgets::saveWidget(p, "test.html", selfcontained=FALSE)

【问题讨论】:

  • 没有反馈的否决票没有帮助
  • 嗨@aocall - 我想我正在尝试做与你在这个问题中类似的事情。你有没有想过如何启用选择哪个变量来着色点? stackoverflow.com/questions/50790759/…
  • 是的,但我制作了一个自定义 HTMLwidget 而不是使用标准的 plotly R API。
  • 好的,感谢您的回复!
  • 作者似乎并不想使用 Shiny 下拉菜单,但是对于那些登陆这里使用 Shiny 的人,请参阅:stackoverflow.com/questions/55076025/…

标签: r plotly r-plotly


【解决方案1】:

目前这在 R API 中是不可能的,因为从变量到绘图输出的映射是在 R 端完成的,而不是通过 plotly.js。

这在以下链接中进行了解释:https://github.com/ropensci/plotly/issues/803

这个功能可以使用 plotly.js 和 HTML 来完成。必须将select 元素添加到HTML 页面,并添加事件侦听器以在更新时调用Plotly.newPlot()

可以在此处查看示例实现:https://github.com/Alanocallaghan/plotlyutils/blob/master/inst/htmlwidgets/lib/selectable_scatter_plot/selectable_scatter_plot.js

【讨论】:

    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多