Skip navigation

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!!.

ARRRRRRR!

library(maps)
library(hexbin)
library(maptools)
library(ggplot2)
library(sp)
library(mapproj)
 
# piRate the data from the militaRy
download.file("http://msi.nga.mil/MSISiteContent/StaticFiles/Files/ASAM_shp.zip", destfile="ASAM_shp.zip")
unzip("ASAM_shp.zip")
 
# extRact the data fRame we need fRom the shape file
pirates.df <- as.data.frame(readShapePoints("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'))
  print(p)
  dev.off()
}
 
# requires imagemagick
system("convert -delay 45 -loop 0 arrr*g arrr500.gif")

12 Comments

  1. This is clean and awesome, good job done!

  2. 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

  3. That worked perfect. Thank you.

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

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

    any idea?

    • 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.

  6. Hi,

    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
    arrr
    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
    install.packages("imagemagick")
    Warning in install.packages :
    package ‘imagemagick’ is not available (for R version 3.0.2)

  7. 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

    requires(imagemagick)
    install.packages("imagemagick")
    library(imagemagick)

    Hope it help you.

  8. 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.

    • Hey Hiller! I’m an OS X R user as well. I use homebrew – http://brew.sh – 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.


3 Trackbacks/Pingbacks

  1. By Have an animated 2014! | FreshBiostats on 25 Jan 2014 at 3:13 am

    […] of the changes in each version) and world from the package maps (speaking of maps, have a look at this great dynamic map by Bob […]

  2. […] that’s an animated map of pirate attacks made in R. With ggplot2 to […]

  3. […] covered the Anti-shipping Activity Messages (ASAM) Database before for TLAPD before but getting, updating and working with the data has more pain points than it […]

Leave a Reply

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