在 gganimate 中填充过渡

Fill transition in gganimate

我想为状态之间的填充值转换设置动画。我有一个 100x100 的 geom_points 网格,它的值介于 0 和 1 之间。本质上,这些点在每个时间点都保持在相同的位置,但它们的填充值会发生变化,我想对其进行动画处理。这里是数据的摘录

head(strg)

        Var1      Var2 const4       pred id
   1 -1.0000000   -1      1 3.337224e-37  1
   2 -0.9292929   -1      1 1.922538e-34  2
   3 -0.8585859   -1      1 2.561529e-32  3
   4 -0.7878788   -1      1 2.522569e-30  4
   5 -0.7171717   -1      1 1.433660e-28  5
   6 -0.6464646   -1      1 1.795601e-27  6

其中 const4 呈现不同的状态。这里是初始情节

ggplot(strg, aes(x = strg[,1], y = strg[,2])) + geom_point(aes(color = strg[,"pred"])) + scale_color_gradient("Predictions", low = "blue", high = "orange")

我现在的问题是

  1. 我必须如何构造我的数据框以构建 ggplot?我考虑过对 const4 中的所有状态重复它。
  2. 在 gganimate 中使用什么函数来可视化过渡?

的帮助下我自己弄明白了。

strg <- strg[with(strg,order(Var1, Var2)),]
strg$id <- rep(1:length(steps), times = num.interpolating.points^2)
strg$id <- as.factor(as.character(strg$id))
p <- ggplot(strg, aes(x = Var1, y = Var2)) + geom_point(data =  strg, aes(color = pred)) + scale_color_gradient("Predictions", low = "blue", high = "orange") + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + coord_cartesian(xlim = c(-0.75,6), ylim = c(-0.8,6))


p <- p + transition_manual(frames = id) 
p
anim_save("Transition1.mp4")

,这导致了所需的输出。变量id用于确定每10000个点的时间点