使用 R 中的下拉菜单为绘图添加图例

问题描述 投票:0回答:0

我编写了以下代码来生成带有两个下拉菜单的绘图。

library(dplyr)
library(plotly)

# initial setup
iris.col <- factor(iris$Species, label = c("red", "blue", "green"))
iris.symbol <- factor(iris$Species, labels = c("diamond", "cross", "square"))

plot <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, 
                marker = list(color = alpha(as.character(iris.col), 0.2), 
                              symbol = ~iris.symbol, size = 8, 
                              line = list(color = 'rgb(0, 0, 0)', width = 1)),
                type = "scatter", mode = "markers", text = ~Species,
                hovertemplate = paste('<i>Species</i>: %{text}',
                                      '<br><b>X</b>: %{x}<br>',
                                      '<b>Y</b>: %{y}'))

# X variable
x_var<-list(
  type = "list",
  x = 0.2,
  xanchor = "left",
  y = 1.2,
  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"
    )
  )
)

# Y variable
y_var<-list(
  type = "list",
  x = 0.8,
  xanchor = "left",
  y = 1.2,
  buttons = list(
    list(
      method = "update",
      args = list(list(y = list(iris$Sepal.Length)),
                  list(yaxis = list(title = "Sepal.Length"))),
      label = "Sepal.Length"
    ),
    list(
      method = "update",
      args = list(list(y =list(iris$Sepal.Width)),
                  list(yaxis = list(title = "Sepal.Width"))),
      label = "Sepal.Width"
    ),
    list(
      method = "update",
      args = list(list(y = list(iris$Petal.Length)),
                  list(yaxis = list(title = "Petal.Length"))),
      label = "Petal.Length"
    ),
    list(
      method = "update",
      args = list(list(y = list(iris$Petal.Width)),
                  list(yaxis = list(title = "Petal.Width"))),
      label = "Petal.Width"
    )
  )
)

# add layout
plot <- plot %>% layout(
  updatemenus = list(x_var,y_var),
  showlegend = TRUE,
  annotations = list(
    list(
      text = "<b>X-Axis:</b>", x=0.04, y=1.18, 
      xref='paper', yref='paper',xanchor = "left", showarrow=FALSE
    ),
    list(
      text = "<b>Y-Axis:</b>", x=0.63, y=1.18, 
      xref='paper', yref='paper',xanchor = "left", showarrow=FALSE
    )
  )
)
#make the plot
plot

我得到这个情节:

但是,我无法为基于物种的情节添加图例。 我试过这篇文章 R+Plotly: Customize Legend Entries 它使用

add_trace
来设置每个图例类。

然而,当我从下拉菜单中选择变量时,这种方法使标记的颜色不再起作用。也许有人可能有一些解决方案。

r plotly
© www.soinside.com 2019 - 2024. All rights reserved.