下拉菜单选择x,y和颜色(图解)
问题描述:
我想创建一个可选的x,y和颜色变量的阴谋图,部分基于this previous question。 x和y变量选择似乎可行,但是当选择新的x和y变量时,点颜色会丢失。下拉菜单选择x,y和颜色(图解)
此外,我试图使用类似的策略来选择点颜色,但不幸的是,这似乎并不奏效。另一个选择是在先前链接的问题中使用“设置可见”策略。
实施例:
library(plotly)
library(pcaMethods)
pca <- pcaMethods::pca(mtcars, nPcs=3)
df <- as.data.frame([email protected])
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映射。
这在下面的链接解释:https://github.com/ropensci/plotly/issues/803
此功能可使用plotly.js和HTML来实现。必须将select
元素添加到HTML页面,并添加事件侦听器以在更新时调用Plotly.newPlot()
。
Downvotes without feedback are not helpful – aocall