下拉菜单选择x,y和颜色(按图) [英] Dropdown menu to select x, y and color (plotly)

查看:127
本文介绍了下拉菜单选择x,y和颜色(按图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有可选xycolor变量的绘图图,部分基于

I am trying to create a plotly graph with selectable x, y and color variables, based in part on 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.

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

Further, I have tried to use a similar strategy to select the point colour, but unfortunately this does not seem to work.

另一种选择是在先前链接的问题中使用set visible策略.

Another option would be to use the set visible strategy in the previously linked question.

示例:

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)

推荐答案

R API目前无法实现,因为从变量到绘图输出的映射是在R端完成的,而不是通过plotly.js完成.

This is not currently possible in the R API, as mapping from variables to plot output is done on the R side, not by plotly.js.

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

This is explained at the following link: https://github.com/ropensci/plotly/issues/803

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

This functionality can be accomplished using plotly.js and HTML. One would have to add select elements to a HTML page, and add event listeners to call Plotly.newPlot() on update.

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

An example implementation can be seen here: https://github.com/Alanocallaghan/plotlyutils/blob/master/inst/htmlwidgets/lib/selectable_scatter_plot/selectable_scatter_plot.js

这篇关于下拉菜单选择x,y和颜色(按图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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