Home > front end >  Sequential gganimate plot
Sequential gganimate plot

Time:02-08

I'm trying to create an animated time series plot using gganimate. The following code produces almost the desired output. It creates two random time series, constructs a data frame and renders the desired animation:

# 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())

enter image description here

  •  Tags:  
  • Related