Translate

Friday, May 6, 2016

Battle Maps Using R and Leaflet (Updated)

“Here are those damned black hat fellows again.”
Attributed to men of the 26th North Carolina, Pettigrew's Brigade at the Battle of Gettysburg in describing the "Iron Brigade".




*********************************************************************************
NOTE:  The content below was originally published on 5/6/2016.  I'm working on the following Kampfgruppe Peiper/Malmedy Massacre map - 

http://rpubs.com/Custer/KGPeiper_v2

**********************************************************************************

A while back I created a post about plotting Russian airstrikes in Syria using the ggmap package. Of note was the commentary and method of taking that data and building an interactive map using the leaflet package by Bhaskar Karambelkar.  It inspired me to start work on a long-term goal I've had of creating a series of maps around the Battle of the Little Bighorn.  That project is on hold until I can do further research as the mystery of what happened to Custer and the 5 Companies of troopers under his direct command is quite contentious with pro and amateur historians.  Thus, I've decided to begin building a series of battle maps that I find interesting.  This first effort is around the initial engagement of Wadsworth's Division from the Union 1st Corps against Heth's Confederate Division on the 1st day of fighting.  The map depicts the attack by two of Heth's Brigades under Archer and Davis against Cutler's and Meredith's Iron Brigade.  Both Archer's and Davis's Brigades were virtually wiped out with Davis's men slaughtered in the infamous Railroad Cut.

The primary source I used was from the Civil War Trust website:

http://www.civilwar.org/battlefields/gettysburg/maps/gettysburg-battle-for.html

I served in the modern day namesake of The Iron Brigade, (2nd Brigade, 1st Armored Division). I hope to expand on the Gettysburg map and also include other maps such Kampfgruppe Peiper during the Battle of the Bulge, looking at the WikiLeaks data for Iraq and yes, tackle the Gordian Knot that is Custer's Last Stand.

The link to the map is on RPubs:  http://rpubs.com/Custer/iron_brigade  

The markers on the map signify individual regiments or artillery batteries.  You can click the markers to identify the unit.

And finally, here is the code.  Note that I used PowerPoint to create the .png files.

df = read.csv("getty_ironbde.csv")
str(df)
> str(df)
'data.frame': 65 obs. of  7 variables:
 $ Regiment: Factor w/ 24 levels "13th AL","147th NY",..: 16 7 5 19 9 3 21 11 15 22 ...
 $ Brigade : Factor w/ 10 levels "Archer","Cutler",..: 7 7 7 7 7 2 2 4 4 9 ...
 $ Icon    : Factor w/ 13 levels "cCdr","cInf1",..: 11 11 11 11 11 11 11 8 8 5 ...
 $ lat     : num  39.8 39.8 39.8 39.8 39.8 ...
 $ long    : num  -77.2 -77.2 -77.2 -77.3 -77.3 ...
 $ Time    : Factor w/ 3 levels "10:15 to 10:45 a.m.",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ Unit    : Factor w/ 25 levels "13th AL/Archer's Brigade",..: 16 7 5 19 9 3 21 11 15 22 ...

library(leaflet)

# Incorporate NATO symbology for icons
milIcons <- iconList(
  uInf1 = makeIcon("uInf1.png", 48, 36),
  cInf1 = makeIcon("cInf1.png", 48, 36),
  uCav1 = makeIcon("uCav1.png", 48, 36),
  uArt1 = makeIcon("uArt1.png", 48, 36),
  uInf2 = makeIcon("uInf2.png", 48, 36),
  cInf2 = makeIcon("cInf2.png", 48, 36),
  uCav2 = makeIcon("uCav2.png", 48, 36),
  uArt2 = makeIcon("uArt2.png", 48, 36),
  uInf3 = makeIcon("uInf3.png", 48, 36),
  cInf3 = makeIcon("cInf3.png", 48, 36),
  uCav3 = makeIcon("uCav3.png", 48, 36),
  uArt3 = makeIcon("uArt3.png", 48, 36),
  cCdr = makeIcon("cCdr.png", 54, 40),
  uCdr = makeIcon("uCdr.png", 54, 40)
)

# Spatial dataframe seems to work best
df2 <- sp::SpatialPointsDataFrame(
  cbind(df$long, df$lat), data = data.frame(df$Unit, df$Icon, df$Time))

leaflet(df2) %>% 
  addProviderTiles("OpenStreetMap.BlackAndWhite", group="Black&White") %>%
  addProviderTiles("Esri.NatGeoWorldMap", group = "NatGeo") %>%
  addProviderTiles("MapQuestOpen.Aerial", group = "Aerial")%>%
  
  addLayersControl(
    baseGroups = c("Black&White", "NatGeo", "Aerial"),
    overlayGroups = df$Time,
    options = layersControlOptions(collapsed = FALSE))  %>%
      
  addMarkers(data=df2[1:22,], icon = ~ milIcons[df.Icon], 
             group="9:30 to 10:15 a.m.",
             popup = ~ df.Unit) %>%
  addMarkers(data=df2[23:43,], icon = ~ milIcons[df.Icon], 
             group="10:15 to 10:45 a.m.",
             popup = ~ df.Unit) %>%
  addMarkers(data=df2[44:65,], icon = ~ milIcons[df.Icon], 
             group="10:45 to 11:15 a.m.", 
             popup = ~ df.Unit)