Вы можете выбрать стрелку и установить цвет стрелки с помощью аргументов arrowhead
и arrowcolor
в функции add_annotations()
. Если вы установите axref = "x"
и ayref = "y"
, вы также убедитесь, что строки начинаются с ori go, если для ax
и ay
установлено значение 0
. Вот пример использования подмножества встроенных наборов данных 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