【问题标题】:Fill transition in gganimategganimate中的填充过渡
【发布时间】:2020-09-27 23:31:58
【问题描述】:

我想为状态之间的填充值转换设置动画。我有一个 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 中使用什么函数来可视化过渡?

【问题讨论】:

  • 您能否提供可重现的数据样本?
  • 我的错 @Robin Turkington steps <- seq(-1, 6) num.interpolating.points <- 100 x.values <- seq(-1, 6, len = num.interpolating.points) y.values <- seq(-1, 6, len = num.interpolating.points) test.points <- data.frame(expand.grid(x.values, y.values)) test.points$pred <- runif(dim(test.points)[2]) strg <- do.call(rbind, replicate(length(steps), test.points, simplify=FALSE)) strg <- as.data.frame(strg) 其中 steps 是我想要动画的状态数
  • 谢谢,我现在就想办法。

标签: r ggplot2 gganimate


【解决方案1】:

Possible to animate polygon fill using gganimate in R? 的帮助下,我自己想通了。

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 用于确定10,000 个点中每个点的时间点。

【讨论】:

    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 2019-07-04
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多