Working with Spatial data

  • tmap uses ESRI Shapefiles
  • read them in using read_shape
  • Shortcut to creating maps qtm
    • quick thematic map
  • tmap objects quite similar to ggplot objects
  • First up, load the packages you need
    • tmap works well with dplyr
library(tmap)
library(dplyr)

Create the map…

read_shape('./counties/counties.shp') -> cty 
cty %>% qtm

Adding a variable to the map

cty %>% qtm(fill="POPDENSITY")

Adding decorations

cty %>% qtm(fill='POPDENSITY') -> cty_dens
cty_dens + tm_compass()

Alternate map styles

cty %>% qtm(fill='POPDENSITY') -> cty_dens
cty_dens + tm_compass() + tm_style_natural()

Alternate map styles - colour blind

cty %>% qtm(fill='POPDENSITY') -> cty_dens
cty_dens + tm_compass() + tm_style_col_blind()

Irish National Grid

qtm(cty,projection="+init=epsg:29900",fill="POPDENSITY") + tm_scale_bar()

To create a new shapefile …

cty %>% set_projection("+init=epsg:29900") -> cty2
tm_shape(cty2) + tm_style_classic() + tm_fill(col="POPDENSITY",title='Population Density') + 
  tm_borders() + tm_compass(size=4) 

Cartograms

cty2 %>% cartogram(weight="TOTAL2011") -> cart
cart %>% qtm

Example - a cartogram of vacant housing

tm_shape(cart) + tm_fill(col="PCVAC20111",title='% Vacant Houses') + tm_borders() 

Faceting

tm_shape(cty2) + tm_fill(col=c("PCVAC20111","POPDENSITY"),title=c('% Vacant Houses','Population Density')) + 
  tm_borders() 

Faceting 2

tm_shape(cty2) + tm_fill(col=c("PCVAC20111","POPDENSITY"),title=c('% Vacant Houses','Population Density')) + 
  tm_borders() + tm_layout(panel.labels=c("Vacancy","Density"))

Faceting 3 - Other Options

tm_shape(cty2) + tm_fill(col=c("PCVAC20111","POPDENSITY"),title=c('% Vacant Houses','Population Density')) + 
  tm_borders() + tm_layout(panel.labels=c("Vacancy","Density")) + tm_scale_bar()

Leaflet map

tm_shape(cty) + tm_fill(col="PCVAC20111",title='% Vacant Houses',style='jenks') + tm_borders() -> cmap
tmap_leaflet(cmap) 

More sophisticated effects…

tmap_mode("view")
tm_shape(cty) + tm_fill(col="PCVAC20111",title='% Vacant Houses',style='jenks') + tm_borders() -> cmap
cmap + tm_view(basemaps = c("Stamen.Watercolor","OpenStreetMap.BlackAndWhite"), alpha=0.5)

More Layers

gael <- read_shape('./Gael/Census2011_Gaeltacht.shp')
tm_shape(cty) + tm_fill(col="PCVAC20111",title='% Vacant Houses',style='jenks') +
  tm_borders() + tm_shape(gael) + tm_fill(col='darkblue',alpha=0.6) -> cmap
cmap + tm_view(basemaps = c("OpenStreetMap.BlackAndWhite","Stamen.Watercolor"), alpha=0.5)

Conclusion

  • What's new?
    • tmap
    • cartograms
    • leaflet and interactive maps
    • Facets for maps