Dropdown menu to select x, y and color (plotly)

人盡茶涼 提交于 2020-01-06 23:00:50

问题


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.

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

Example:

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)

回答1:


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.

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

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.

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



来源:https://stackoverflow.com/questions/40719220/dropdown-menu-to-select-x-y-and-color-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!