首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >序贯gganimate图

序贯gganimate图
EN

Stack Overflow用户
提问于 2022-02-07 12:39:20
回答 1查看 301关注 0票数 0

我正在尝试使用gganimate创建一个动画时间序列情节。下面的代码几乎产生了所需的输出。它创建两个随机时间序列,构造一个数据帧并呈现所需的动画:

代码语言:javascript
复制
# necessary packages
require(ggplot2)
require(gganimate)
require(transformr)
require(gifski)

# construct data frame with two random time series and time index t
TS      = matrix(rnorm(100 * 2), 100, 2)
plot.df = data.frame(TS, t = 1:100)
plot.df = reshape2::melt(plot.df, id.vars = "t")

# usual ggplot line plot structure
p = ggplot(plot.df, aes(x = t, y = value, col = variable)) + 
  geom_line()
  
# animation that reveals time series along time dimension (= x-axis)
play = p + transition_reveal(t)

# render plot using gifski
animate(play,renderer = gifski_renderer())

在输出中,两个时间序列同时显示。是否有一种选择:首先显示第一个时间序列,然后在第一个时间序列之上显示第二个时间序列?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-07 15:53:33

一个能帮上忙的解决办法呢。这样做的目的是创建一个从数据集的1到nrow的渐进式列,并在transition_reveal()中使用它:显然,数据集应该按照时间和变量排序,就像在数据中一样。

代码语言:javascript
复制
# necessary packages
require(ggplot2)
require(gganimate)
require(gifski)

# construct data frame with two random time series and time index t
TS      <- matrix(rnorm(100 * 2), 100, 2)
plot.df <- data.frame(TS, t = 1:100)
plot.df <- reshape2::melt(plot.df, id.vars = "t")

# reorder in case
# plot.df <- plot.df[order(plot.df$variable,plot.df$t),]

# adding a new column to make progressive the reveal of the data
plot.df$transition_value <- 1:nrow(plot.df)

# your ggplot plot
p <- ggplot(plot.df, aes(x = t, y = value, col = variable)) + 
  geom_line()

# reveal with the new value
play <- p + transition_reveal(transition_value)

# render plot using gifski
animate(play,renderer = gifski_renderer())

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71018725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档