我试图创建一个散点图,用户可以动态地更改X轴和Y轴。我已经完成了它,但我遇到了这种奇怪的打嗝。
第一次更改X下拉列表时,散点图将不正确。但是,一旦我更改了Y下拉列表,散点图就会自行修正,但它会丢失它的颜色分组。示例代码:
# Get libraries
library(plotly)
set.seed(123)
index <- 1:24
x1 <- rnorm(24, 4, 3)
x2 <- rnorm(24, 3, 4)
y1 <- rnorm(24, 5, 2)
y2 <- rnorm(24, 2, 5)
type <- rep(c("A", "B", "C"), 8)
df <- data.frame(x1, x2, y1, y2, type)
p <- plot_ly(df, x = ~x1, y = ~y1, color = ~type, type = "scatter", mode = "markers",
text = ~paste("Index: ", index)) %>%
layout(
updatemenus = list(
## X Axis ##
list(
y = 0.6,
buttons = list(
list(method = "restyle",
args = list("x", list(df$x1)), # put it in a list
label = "x1"),
list(method = "restyle",
args = list("x", list(df$x2)), # put it in a list
label = "x2"))),
## Y Axis ##
list(
y = 0.5,
buttons = list(
list(method = "restyle",
args = list("y", list(df$y1)), # put it in a list
label = "y1"),
list(method = "restyle",
args = list("y", list(df$y2)), # put it in a list
label = "y2")))
))
p
在您的查看器中弹出后,单击&#34; X1&#34;下拉并选择&#34; X1&#34;再次:注意图表如何更新错误。接下来,单击&#34; Y1&#34;下拉并选择&#34; Y1&#34;:注意图形与原始图形相同,但颜色分组不再存在,我相信索引混淆了。
是否可以轻松解决我做错的事情?