1 Introduction

We will use gganimate to do some basic plot animations.

2 Animate Time Series

2.1 Resting state pupillometry

read in resting state pupillary data (4 min at 1000Hz) 240,000 observations eliminate values <400 (high pass filter), smooth and interpolate

bz <- read.csv("BZrev.csv", header = T)
bz.all <- data.frame(bz)
bz.all$left <- as.numeric(bz.all$left)
bz.all[bz.all < 400] <- NA
to.ts <- as.ts(bz.all)  #convert to time series
plot(to.ts, col = "red")

whole <- to.ts %>% zoo()
maybe <- na.approx(object = whole)
try <- as.ts(SMA(maybe, n = 5))
plot(try, col = "blue")


repeat all steps on the dataframe (not time series), add sequence. detrend by lagging the time series

convert <- zoo(bz.all)
interp <- data.frame(na.approx(object = convert))
smooth <- SMA(interp, n = 10)
all.there <- na.omit(smooth)
b <- as.data.frame(all.there)
b$time <- seq_along(b$all.there)
lagged <- b %>% mutate(lag.obs = all.there - lag(all.there, 5))
lag.all <- na.omit(lagged)
ggplot(lag.all, aes(time, lag.obs)) + geom_line(size = 0.5, color = "blue") + ylim(c(-20, 
    20)) + jamie.theme

# plot lagged amplitude by time
lag.less <- lag.all %>% filter(time < 10001)  #generate just a 10sec sampling window, retain only the first 10000 observations from lag.all
l <- ggplot(lag.less, aes(time, lag.obs)) + geom_line(size = 0.5, color = "red") + 
    ylim(c(-10, 10)) + jamie.theme


animate it!

new <- l + transition_reveal(time)
animate(new, width = 900, height = 450)


aim for a smaller window

x <- ggplot(lag.less, aes(time, lag.obs)) + geom_point(size = 3, color = "red") + 
    geom_line(color = "gray", size = 0.25) + ylim(c(-10, 10)) + jamie.theme
newer <- x + transition_reveal(time) + view_follow(fixed_y = TRUE)
animate(newer, fps = 10, width = 900, height = 450)

anim_save("newer.gif")


Plot the time series with a bouncing mountain climber from the Price is ight

climber <- image_read("climber.jpg")
print(climber)
## # A tibble: 1 x 7
##   format width height colorspace matte filesize density
##   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
## 1 JPEG     333    508 sRGB       FALSE    31545 144x144

lag.less$image <- "climber.jpg"
x <- ggplot(lag.less, aes(time, lag.obs)) + geom_image(aes(image = image), size = 0.15) + 
    geom_line(color = "gray", size = 0.25) + ylim(c(-10, 10)) + jamie.theme
newer <- x + transition_reveal(time) + view_follow(fixed_y = TRUE)
animate(newer, fps = 3, width = 900, height = 600)

anim_save("climber2.gif")