让路径在 gganimate 中消失

have paths fade away in gganimate

我正在尝试在 gganimate 图中绘制多条路径。我希望线条在最后 N 帧中淡出(例如本例中的 N=5)。

数据如下所示:

set.seed(27)
df <- data.frame(Frame = rep(1:10, 3),
                 id = factor(rep(1:3, each = 10)),
                 x = runif(30),
                 y = runif(30))

head(df)

  Frame id          x          y
1     1  1 0.97175023 0.14257923
2     2  1 0.08375751 0.47864658
3     3  1 0.87386992 0.05182206
4     4  1 0.32923136 0.25514379
5     5  1 0.22227551 0.14262912
6     6  1 0.40164822 0.48288482

我尝试使用 shadow_mark 制作情节,但这似乎没有随着时间的推移淡出线条。

df %>%
  ggplot(aes(x = x, y = y, group = id, color = id)) +
  geom_path() +
  geom_point()+
  scale_color_manual(values=c("red","blue","green")) +
  transition_reveal(along = Frame) +
  shadow_mark(size = 0.75) +
  theme_void()

这只会产生以下内容:

有没有办法让这些线条变淡。理想情况下,我只是绘制了 N 帧的滚动路径。

这就是您要找的东西吗?改编自 the post mentioned in the comments。如果您使用 geom_segment().

,则无需使用 transition_reveal()
library(gganimate)
library(dplyr)
library(tidyr)

set.seed(27)
n <- 10
df <- data.frame(Frame = rep(1:n, 3),
                 id = factor(rep(1:3, each = n)),
                 x = runif(3*n),
                 y = runif(3*n))

newdf <- df %>%
  uncount(n, .id = "newframe") %>%
  filter(Frame <= newframe) %>%
  arrange(newframe, Frame) %>%
  group_by(newframe, id) %>%
  mutate(x_lag = lag(x), 
         y_lag = lag(y),
         tail = last(Frame) - Frame,
         # Make the points solid for 1 frame then alpha 0
         point_alpha = if_else(tail == 0, 1, 0),
         # Make the lines fade out over 3 frames
         segment_alpha = pmax(0, (3-tail)/3)) %>%
  ungroup()

ggplot(newdf, 
       aes(x = y, y = x, xend = y_lag, yend = x_lag, group = Frame, color = id)) +
  geom_segment(aes(alpha = segment_alpha)) +
  geom_point(aes(alpha = point_alpha)) +
  scale_alpha(range = c(0,1)) +
  guides(alpha = F) +
  transition_manual(newframe) +
  theme_void() +
  scale_color_manual(values = c("red","blue","green"))