我正在尝试创建一个带有可选 x、y 和颜色变量的绘图图,部分基于这个先前的问题。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)