Displaying Maps and Spatial Data

Mahbubul Majumder, PhD
Sep 11, 2014

Data types and display standard

  • Add color and faceting to explore more variable
  • Careful about overplotting
  • If not necessary, avoid 3D plots, instead use contour
  • Common plots depending on data types
Data Type Common Plot Type
Numerical histogram, dotplot, density plot, scatterplot, boxplot
Categorical bar chart, mosaic
Date time series
Mixed boxplot, parallel cordinate plot
Spatial maps, google map, contour plot or 2d density

Map visualization

library(maps)
library(ggplot2)
map.dat <- map_data("world")
ggplot() + geom_polygon(aes(long,lat, group=group), fill="grey65", data=map.dat) + theme_bw() + theme(axis.text = element_blank(), axis.title=element_blank()) 

plot of chunk unnamed-chunk-3

Colourful world

ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region))+ geom_polygon() + theme(legend.position = "none")

plot of chunk unnamed-chunk-4

  • Color should be used to display data

USA county map

us.dat <- map_data("state")
ct.dat <- map_data("county")
ggplot() + geom_polygon(aes(long,lat, group=group), fill="grey65", data=ct.dat) + geom_polygon(aes(long,lat, group=group), color='white', fill=NA, data=us.dat) + theme_bw() + theme(axis.text = element_blank(), axis.title=element_blank()) 

plot of chunk unnamed-chunk-5

USA polyconic map

library(scales) # for function alpha()
us.dat <- map_data("state")
ggplot(us.dat, aes(x=long, y=lat, group=group)) + geom_polygon(fill="grey65", colour = alpha("white", 1/2), size = 0.2) + theme_bw() + theme(legend.position = "none", text = element_blank(), line = element_blank()) + coord_map("polyconic") 

plot of chunk unnamed-chunk-6

Showing data on maps

pdat <- read.csv("./tempdat.csv")
ggplot(pdat, aes(x=long, y=lat,group=group)) + 
  geom_polygon(aes(fill=temp), colour = alpha("white", 1/2), size = 0.2) + theme_bw() + 
  theme(legend.position = "none", text = element_blank(), line = element_blank()) + 
  scale_fill_continuous(low="blue", high="hotpink") 

plot of chunk unnamed-chunk-7

Showing data on maps

  • World petroleum use data, obtained from Nasa website

  • We need to clean the data before plotting such a map

  • Yet we see trouble with the data

  • Map data is obtained from R package rgdal

refer to help/maps.R

Showing more data on maps

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  scale_fill_brewer(palette = "PuRd")

plot of chunk unnamed-chunk-9

Geo-spatial data visualization

  • ggmap package of R works with ggplot
## install.packages("ggmap")
library(ggmap)
  • Uses Google API to connect geographical location information

Google API in action

geocode("University NE, Omaha")
     lon   lat
1 -96.01 41.26
geocode("PKI, Omaha")
  lon   lat
1 -96 41.25
geocode("Disney land")
     lon   lat
1 -117.9 33.81
revgeocode(c(-96,41))
[1] "5615 Nebraska 66, Plattsmouth, NE 68048, USA"
mapdist("Omaha","lincoln")
   from      to     m    km miles seconds minutes  hours
1 Omaha lincoln 88119 88.12 54.76    3442   57.37 0.9561
  • Please try ? mapdist

Omaha crimes map (spotcrime.com)

crimes <- read.csv("./omaha-crimes.csv")
omaps <- get_map(location = 'Omaha', source = 'stamen', maptype = 'toner')
ggmap(omaps) +
  geom_point(size=5, alpha = 1/2, aes(lon,lat, color=type), data=crimes) 

plot of chunk unnamed-chunk-13

Map options

  • get_map(location = 'Omaha') plot of chunk unnamed-chunk-14
  • get_map(location = 'Omaha', maptype = 'satellite') plot of chunk unnamed-chunk-15

Plotting data as block on maps

ggmap(omaps) + stat_bin2d(aes(x = lon, y = lat, colour = type, fill = type), size = .5, bins = 30, alpha = 1/2, data = crimes) + xlim(c(-96.25,-95.85)) + ylim(c(41.15,41.4))

plot of chunk unnamed-chunk-16

Road map with zoom option

get_map(location = 'Omaha', maptype = 'roadmap', zoom = 11) plot of chunk unnamed-chunk-17

Where are the crimes?

ggmap(omapg) + stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), 
size = 2, bins = 4, data = crimes, geom = "polygon")

plot of chunk unnamed-chunk-18

Black and white map

get_map(location = 'Omaha', maptype = 'roadmap', zoom = 11, color='bw') plot of chunk unnamed-chunk-19

Reading assignment and references