我想用 R 中的绘图来表示以原点为根的 2D 向量。此外,我想根据分类变量为向量着色。问题是我可以创建颜色编码但没有箭头的线条:
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"))
Run Code Online (Sandbox Code Playgroud)
或者使用箭头但不使用颜色编码:
df %>%
plot_ly(x = ~x, y = ~y, color = ~is_basis) %>%
add_annotations(x = …Run Code Online (Sandbox Code Playgroud)