Plotly:如何使用 add_annotations 设置箭头的样式和颜色?

Plotly: How to set style and color for the arrows using add_annotations?

我想在 R 中用 plotly 表示以原点为根的二维向量。此外,我想根据分类变量为向量着色。问题是我可以创建颜色编码但没有箭头的线条:


library(plotly)
library(dplyr)

v <- c(1, 1)
b1 <- c(1, 0)
b2 <- c(0, 1)
df <- data.frame(
  x = c(v[1], b1[1], b2[1]),
  y = c(v[2], b1[2], b2[2]),
  is_basis = c(FALSE, TRUE, TRUE)
)
df %>% 
  plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
  add_segments(xend = ~x, yend = ~y, x = 0, y = 0, colors = c("red","black"))

或者带箭头但没有颜色编码:

df %>% 
  plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
  add_annotations(x = ~x, y = ~y, showarrow = TRUE, text = "", ax = 0, ay = 0,
                  axref = "x", ayref = "y", xref = "x", yref = "y")

所以,我的问题是:我可以添加带有 add_segments 的箭头吗?或者,我可以为使用 add_annotations 生成的箭头设置样式和颜色吗?

您可以通过函数add_annotations()中的参数arrowheadarrowcolor选择箭头并设置箭头的颜色。如果设置 axref = "x"ayref = "y",如果将 axay 设置为 0,则还要确保行从 origo 开始。 下面是一个使用内置数据集 mtcars 子集的示例:

完整代码:

library(plotly)
library(dplyr)

data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]
#data <- data %>% filter(row.names(data) %in% c("Honda Civic", "Fiat X1-9", "Datsun 710"))
#row.names(data) <- c("Honda Civic", "Fiat X1-9", "Datsun 710")

p <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
        marker = list(size = 2)) #%>%

p <- p %>%
  add_annotations(x = data["Honda Civic","wt"],
                  y = data["Honda Civic","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='blue',
                  arrowhead = 1,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

p <- p %>%
  add_annotations(x = data["Fiat X1-9","wt"],
                  y = data["Fiat X1-9","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='green',
                  arrowhead = 2,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

p <- p %>%
  add_annotations(x = data["Datsun 710","wt"],
                  y = data["Datsun 710","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='red',
                  arrowhead = 3,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

p <- p %>%
  add_annotations(x = data["Fiat 128","wt"],
                  y = data["Fiat 128","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='black',
                  arrowhead = 4,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

p <- p %>%
  add_annotations(x = data["Mazda RX4 Wag","wt"],
                  y = data["Mazda RX4 Wag","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='purple',
                  arrowhead = 5,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))


p <- p %>%
  add_annotations(x = data["Volvo 142E","wt"],
                  y = data["Volvo 142E","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='grey',
                  arrowhead = 6,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

p <- p %>%
  add_annotations(x = data["Toyota Corolla","wt"],
                  y = data["Toyota Corolla","mpg"],
                  text = "",
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowcolor='aquamarine',
                  arrowhead = 7,
                  arrowsize = 2,
                  ax = 0,
                  ay = 0,
                  axref="x",
                  ayref='y',
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

p