Creating dynamics in a hexagonal world in the R environment

I've recently been collaborating on a project on the spread of wildlife disease. We have an interesting spatially structured system with a reservoir host occupying the interior of islands and the species of concern on the periphery of the islands (see last week's post: Creating a hexagonal world).

There is disease spread within the interior of the island, within the periphery of the island, and between the interior and periphery, which makes understanding the simulations difficult. We have a variety of summary statistics over space and time to give us a sense of what is happening, but a key part of understanding the dynamics is to visually inspect what's happening.

Over the past week I wrote a function specific to our project (GitHub link to HexGraph) that allows us to visualize the disease dynamics:

In the video, the colors represent the reservoir host (red, interior) and species of interest (yellow, periphery). The brightness of the colors correspond to a high density of infected organisms. I absolutely love watching these videos, and could watch them all day!

One of the reasons that I enjoy these so much is that I was able to run the animation package in R. One function, in particular, saveVideo(), is able to generate .mp4 files of our daily simulations over 25 years (9,125 iterations per simulation!). This is preferable to the only other way I've generated video before (i.e., print all files as images, upload into ImageJ, generate video file, delete all printed images) because it's faster, takes less memory, and is less prone to error.

Below are the steps I followed to be able to use animation and make a silly test video for OSX:

  1. Install Homebrew
    1. In Terminal, run:
      /usr/bin/ruby -e "$(curl -fsSL"
  2. Install ffmpeg
    1. In Terminal, run:
      brew install ffmpeg
  3. Install and load animation package in R
    1. In R, run:
    2. In R, run:
  4. Make a video
    1. In R, run:
      	message <- "Hello world!"
      	n <- nchar(message)
      	time = 3
      	ani.options(interval = time/n, nmax = n)
      	for (i in 1:n){
      		plot(i, i, pch = paste("", substr(message, i, i), "", sep = ""), cex = 3, xlim = c(0, (n+1)), ylim = c(0, n+1))
      }, = "~/Desktop/Points.mp4"

      Created by Pretty R at

And that's more or less it. I just thought I'd put that out there to post the video and in case anybody's looking for a simple reference as to how to use animation