【发布时间】:2021-09-28 22:53:39
【问题描述】:
这个问题与 SO 上的其他一些问题有关,但我还没有找到解决方案。
我希望使用 plotly 的 dropdown functionality 来选择在 x 轴上绘制的变量,类似于 this question/answer,这对我到达现在的位置非常有帮助。
我现在尝试使用plot_ly 中的color 参数为我的绘图中的标记着色。但是,当我使用下拉菜单更改 x 变量时,数据似乎变得混乱或混淆。这是一个最小的可重现示例:
library(plotly)
plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species,
type ="scatter", mode = "markers",text = ~Species,
hovertemplate = paste('<i>Species</i>: %{text}',
'<br><b>X</b>: %{x}<br>',
'<b>Y</b>: %{y}')
) %>%
layout(
annotations = list(
list(
text = "<b>X-Axis Variable:</b>", x=0.05, y=1.13,
xref='paper', yref='paper',xanchor = "left", showarrow=FALSE
)
),
updatemenus = list(
list(
type = "list",
x = 0.25,
xanchor = "left",
y = 1.15,
buttons = list(
list(
method = "update",
args = list(list(x = list(iris$Sepal.Length)),
list(xaxis = list(title = "Sepal.Length"))),
label = "Sepal.Length"
),
list(
method = "update",
args = list(list(x =list(iris$Sepal.Width)),
list(xaxis = list(title = "Sepal.Width"))),
label = "Sepal.Width"
),
list(
method = "update",
args = list(list(x = list(iris$Petal.Length)),
list(xaxis = list(title = "Petal.Length"))),
label = "Petal.Length"
),
list(
method = "update",
args = list(list(x = list(iris$Petal.Width)),
list(xaxis = list(title = "Petal.Width"))),
label = "Petal.Width"
)
)
)
)
)
我们知道这是不正确的,因为当我们将 x 变量更改为 Sepal.Width 时,这与 y 变量相同,因此应该导致沿 y=x 轴的简单点线,我们留下下面的情节:
questions like this 的一些关于 SO 的讨论表明 R-plotly API 不支持 选择颜色变量,但我对更改颜色不感兴趣。有趣的是,当我从情节中删除 color = ~Species 参数时,这个问题就消失了。
谢谢大家 - 不知道哪里最好看!
【问题讨论】:
标签: r plotly data-visualization plotly.js