R Plotly下拉变量选择颜色

R Plotly dropdown variable selection with color

这个问题与 SO 上的一些其他问题有关,但我还没有找到解决方案。

我希望使用 plotlydropdown functionality to select which variable gets plotted on the x-axis, similar to this question/answer,它对我到达现在的位置非常有帮助。

我现在正在尝试使用 plot_ly 中的 color 参数来为我的绘图中的标记着色。但是,当我使用下拉菜单更改 x-variable 时,数据似乎变得混乱或混乱。这是一个最小的可重现示例:

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-variable 更改为 Sepal.Width 时,这与 y-variable 相同,因此应该会产生一条简单的点线y=x 轴,我们得到下面的图:

有一些关于 SO 的讨论表明 选择 颜色变量不受 R-plotly API 支持,但是我对改变颜色不感兴趣。有趣的是,当我从情节中删除 color = ~Species 参数时,这个问题就消失了。

谢谢大家 - 不确定最好的地方在哪里!

回答我自己的问题,希望能在未来帮助其他人。

只要您对图例不感兴趣,您实际上可以通过将颜色映射到 plot_ly()marker 参数中的点来解决这个数据绘图问题。

library(plotly)
iris2 <- mutate(iris, iris.col = factor(Species, label = c("red", "blue", "green")))

plot_ly(data = iris2, x = ~Sepal.Length, y = ~Sepal.Width, marker = list(color = ~iris.col), 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"
          )
        )
      )
    )
  )

预期输出: