Animated IRL Pirate Attacks In R

Avast me hearRties! (ok, enough of the pirate speak in a blog post)

It wouldn’t be TLAPD without out some modest code & idea pilfering from Mark Bulling & Simon Raper. While those mateys did a fine job hoisting up some R code (your really didn’t think I’d stop with the pirate speak, did you?) for their example, I took it one step furrrrther to build an animation of cumulative, yearly IRL pirate attacks from 1978 to the present. I found it a bit interesting to see how the hotspots shifted over time. Click on the graphic for the largeR version or I’ll make ye walk the plank!!.


# piRate the data from the militaRy
download.file("", destfile="")
# extRact the data fRame we need fRom the shape file
pirates.df <-"ASAM 19 SEP 13")) # you may need to use a diffeRent name depending on d/l date
# get the woRld map data
world <- map_data("world")
world <- subset(world, region != "Antarctica") # inteRcouRse AntaRctica
# yeaRs we want to loop thoRugh
ends <- 1979:2013
# loop thRough, extRact data, build plot, save plot: BOOM
for (end in ends) {
  png(filename=sprintf("arrr-%d.png",end),width=500,height=250,bg="white") # change to 1000x500 or laRgeR
  dec.df <- pirates.df[pirates.df$DateOfOcc > "1970-01-01" & pirates.df$DateOfOcc < as.Date(sprintf("%s-12-31",end)),] 
  rng <- range(dec.df$DateOfOcc)
  p <- ggplot() 
  p <- p + geom_polygon(data=world, aes(x=long, y=lat, group=group), fill="gray40", colour="white")
  p <- p + stat_summary_hex(fun="length", data=dec.df, aes(x=coords.x1, y=coords.x2, z=coords.x2), alpha=0.8)
  p <- p + scale_fill_gradient(low="white", high="red", "Pirate Attacks recorded")
  p <- p + theme_bw() + labs(x="",y="", title=sprintf("Pirate Attacks From %s to %s",rng[1],rng[2]))
  p <- p + theme(panel.background = element_rect(fill='#A6BDDB', colour='white'))
# requires imagemagick
system("convert -delay 45 -loop 0 arrr*g arrr500.gif")
Cover image from Data-Driven Security
Amazon Author Page

15 Comments Animated IRL Pirate Attacks In R

  1. Eddy

    I am running this code in R 3.0.1 on Windows 7 64 bit OS. I am having trouble running the last command
    system(“convert -delay 45 -loop 0 arrr*g arrr500.gif”)

    i have already installed imagemagick.

    This is the error message I am getting

    Invalid Parameter – 45
    Warning message:
    running command ‘convert -delay 45 -loop 0 arrr*g arrr500.gif’ had status 4

  2. keziah22

    I ran this code in R 3.0.1 on Ubuntu 12.04 LTS (64bit). I am done. Thank you so much.

  3. pierre

    many tks for this post; however i get the following error message
    Error in getinfo.shape(filen) : Error opening SHP file

    any idea?

    1. hrbrmstr

      One of the comments says “# you may need to use a diffeRent name depending on d/l date”. I suspect you may need to take a look at the unzipped archive to select a new date.

  4. pp


    I cannot run it either via system or shell command. Here is the output:

    system("convert -delay 45 -loop 0 arrrg arrr500.gif")
    Invalid Parameter - 45
    Warning message:
    running command 'convert -delay 45 -loop 0 arrr
    g arrr500.gif' had status 4
    shell("convert -delay 45 -loop 0 arrrg arrr500.gif")
    Invalid Parameter - 45
    Warning messages:
    1: running command 'C:\Windows\system32\cmd.exe /c convert -delay 45 -loop 0
    g arrr500.gif' had status 4
    2: In shell("convert -delay 45 -loop 0 arrrg arrr500.gif") :
    'convert -delay 45 -loop 0 arrr
    g arrr500.gif' execution failed with error code 4
    Warning in install.packages :
    package ‘imagemagick’ is not available (for R version 3.0.2)

  5. keziah22

    I use ubuntu 12.04 LTS (64bit). I also can not run system(“convert -delay 45 -loop 0 arrrg arrr500.gif”). And I searched on google and I found it:

    How to install imagemagick on Ubuntu

    $sudo apt-get install libmagickwand-dev

    works well for Ubuntu 12.10

    This is assuming you have installed all other dependencies viz.

    $sudo apt-get install imagemagick ruby ruby-dev gem

    And use imagemagick


    Hope it help you.

  6. Pingback: Have an animated 2014! | FreshBiostats

  7. Pingback: Piracy in R | Notes on Learnings About Data Analysis

  8. Hiller Jennings

    Was wondering what form of imagemagik is required for this. Ive been trying to download different types of the application and it doesn’t seem to be working. Also how do you get it into R. Im working from a Mac. Thank you for your time.

    1. hrbrmstr

      Hey Hiller! I’m an OS X R user as well. I use homebrew – – for package management and have imagemagick installed via brew install imagemagick. I just tried the code from the post today (changing 2013 & 13 references to 2014 & 14 and using the ASAM 02 DEC 14 shapefile) and it all worked fine. I don’t use the R imagemagick package as it’s (to me) just as easy to do the final system call to use it via shell.

  9. Pingback: A Package Full o’ Pirates & Makin’ Interactive Pirate Maps in arrrrrRstats |

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.