如何在ggplot2中显示obs的方向(航向)

How to show direction (heading) of obs in ggplot2

如何使用 ggplot2 显示观察的方向(航向)?有没有办法调整 shape=17(三角形) 使其 "points" 到下一次观察?

示例代码

library(ggplot2)

dat <- data.frame(id = c(1, 1, 2, 2, 3, 3), 
                  time = c(1, 2, 1, 2, 1, 2),
                  x = c(.1, .2, .3, .4, .5, .6),
                  y = c(.6, .25, .4, .33, .2, .51))


ggplot(dat, aes(x, y, color=factor(id))) + 
  geom_point(shape=17) + 
  # geom_line() +
  NULL

我们可以使用 ggplot2::geom_segment 重塑数据后使用 dplyrtidyr::pivot_wider:

dat <- data.frame(id = c(1, 1, 2, 2, 3, 3), 
                  time = c(1, 2, 1, 2, 1, 2),
                  x = c(.1, .2, .3, .4, .5, .6),
                  y = c(.6, .25, .4, .33, .2, .51))
library(dplyr)
library(tidyr)
library(ggplot2)
dat %>% 
  pivot_wider(names_from = time, values_from = c(x, y)) %>% 
  ggplot(aes(x=x_1, y=y_1, color=factor(id))) + 
  geom_segment(aes(xend = x_2, yend = y_2),
               arrow = arrow(length = unit(.3,"cm"))) +
  labs(x="x", y="y", color="id")

编辑:

but I just want the arrow pointing without lines.

我不确定我们应该如何处理每个 id 的第二个点(因为它 不是方向 )但是如果我们想从图中省略它们,我们可以做:

library(dplyr)
library(tidyr)
library(ggplot2)
dat %>% 
  group_by(id) %>% 
  arrange(id, time) %>% 
  mutate(x_2 = x + 0.0001 * (lead(x) - x),
         y_2 = y + 0.0001 * (lead(y) - y)) %>%
  filter(!is.na(x_2)) %>% 
  ggplot(aes(x=x, y=y, color=factor(id))) + 
  geom_segment(aes(xend = x_2, yend = y_2),
               arrow = arrow(length = unit(.3,"cm"))) +
  labs(x="x", y="y", color="id")

或者如果我们希望箭头指向下一个测量值,独立于颜色我们可以使用下面的代码(现在由于没有方向而只缺少最后一个点):

library(dplyr)
library(tidyr)
library(ggplot2)
dat %>% 
  arrange(id, time) %>% 
  mutate(x_2 = x + 0.0001 * (lead(x) - x),
         y_2 = y + 0.0001 * (lead(y) - y)) %>%
  filter(!is.na(x_2)) %>% 
  ggplot(aes(x=x, y=y, color=factor(id))) + 
  geom_segment(aes(xend = x_2, yend = y_2),
               arrow = arrow(length = unit(.3,"cm"))) +
  labs(x="x", y="y", color="id")

如果我们想保留 'last' 措施 我们可以将它们添加到另一个 geom_point 层...

结合 dario 的回答How to scale a 2D vector and keep direction and Arranging arrows between points nicely in ggplot2

中的想法
library(dplyr)
library(tidyr)
library(ggplot2)

dat %>% 
  pivot_wider(names_from = time, values_from = c(x, y)) %>% 
  group_by(id) %>% 
  mutate(x_v = x_2 - x_1, y_v = y_2 - y_1) %>% 
  mutate_at(vars("x_v", "y_v"), 
            list(units =~ (. / sqrt((x_v)^2 + (y_v)^2))/1000)) %>% 
  ggplot(aes(x=x_1, y=y_1, colour = factor(id))) + 
  geom_segment(aes(xend = x_1 + x_v_units, yend = y_1 + y_v_units),
              show.legend = F,
               arrow = arrow(length = unit(.3,"cm"), type="closed", angle = 20)) +
  geom_point(data = (dat %>% filter(time == 2)), aes(x, y), shape=15, size=2) +
  labs(x="x", y="y", colour="id") +
  theme_bw()

数据:

dat <- data.frame(id = c(1, 1, 2, 2, 3, 3), 
                  time = c(1, 2, 1, 2, 1, 2),
                  x = c(.1, .2, .3, .4, .5, .6),
                  y = c(.6, .25, .4, .33, .2, .51))