首页 > 解决方案 > 在 R gganimate 中做动画时如何保留以前的数据层?

问题描述

我正在使用 ggplot 和 gganimate 做动画。在之前的 gganimate 版本中有一个“累积”选项,似乎新版本不支持这个。

这是代码:

library(ggplot2)
library(gganimate)

x = data.frame(y = c(2000, 2001), x=c(1,2), z=c(3,4))
ggplot(x, aes(x,z))+geom_point() + transition_time(y)

它有效,但我想将第一个数据点保留在散点图上。

我试图转换数据,但它没有帮助:

x1 = data.frame(y = c(2000, 2001, 2001), x=c(1,2,1), z=c(3,4,3))
ggplot(x1, aes(x,z))+geom_point() + transition_time(y)

标签: ranimationggplot2gifgganimate

解决方案


是否shadow_mark()达到了你想要的行为?

x = data.frame(y = c(2000, 2001, 2002), x=c(1,2,3), z=c(3,4,5))

p <- ggplot(x, aes(x, z)) +
  geom_point() +
  transition_time(y) +
  shadow_mark()

animate(p)

在此处输入图像描述

它没有捕获“补间”,但它确实在data.


推荐阅读