Maps allow us to locate objects geographically and the added features (such as shape, size and colour) can provide insight to characteristics of spatial data1

Three Strategies

  1. Display 1
  2. Analyze1
  3. Explore1

Consider audience, interactivity and nature of data.1

Seven Techniques

  1. Chloropleth Map: different colours/shadings for different regions2
  2. Heat Map: similar to a chloropleth map, but is not defined by regions2
  3. Hexagonal Binning: use regular hexagons to create bins in your map
  4. Dot Map: scatterplots on a map2
  5. Cluster Map: scatterplots on a map, where each point represents the number of points grouped together2
  6. Bubble Map: colour and size can represent two different variables
  7. Cartogram Map: regions of a map represent a variable2

Things to Consider

(and we will return to these items throughout our prsentation)

Highly Recommend this Talk!

Google Maps + HTML5 + Spatial Data Visualization: A Love Story

Packages Required

Leaflet

library(leaflet)
library(knitr)
library(rgdal)

ggmap

library(ggplot2)
library(tidyverse)
library(forcats)
library(labeling)
library(ggmap)

Leaflet

Leaflet for R is where I learned how to use Leaflet. It is a great resource, so refer to it if you need more clarification.

addTiles() gives the default base map in leaflet4

m <- leaflet() %>% 
  addTiles()
m

Base Maps

addProviderTiles() gives you a tile layer from a known map provider4

m %>% addProviderTiles(providers$Stamen.Toner) 
m %>% addProviderTiles(providers$CartoDB.Positron)
m %>% addProviderTiles(providers$Esri.NatGeoWorldMap)
BaseMap Pros Cons
Default Zoomed out - colours are fairly simple Zoom in - gives extra colour
Stamen.Tower None? Any colour added to plot will likely be hard to see
CartoDB.Positron Great for added colour as the background colour is mutual None?
Esri.NatGeoWorldMap Shows different landscapes of Earth, i.e. deepness of water & dry/wet regions Specific uses only

Example: Base Map versus CartoDB.Positron Map (Weather Systems)

addWMSTiles is for adding Web Map Service tiles4

leaflet() %>% addTiles() %>%
  addWMSTiles(
    "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
    layers = "nexrad-n0r-900913",
    options = WMSTileOptions(format = "image/png", transparent = TRUE),
    attribution = "Weather data © 2012 IEM Nexrad"
  )
leaflet() %>% addProviderTiles(providers$CartoDB.Positron) %>%
  addWMSTiles(
    "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
    layers = "nexrad-n0r-900913",
    options = WMSTileOptions(format = "image/png", transparent = TRUE),
    attribution = "Weather data © 2012 IEM Nexrad"
  )

Circle Markers

data(quakes)

leaflet(data = quakes) %>%  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(~long, ~lat, radius = ~sqrt(exp(mag))/2, label = ~as.character(mag), color = "black", fillColor = "red", fillOpacity = 0.1, stroke = FALSE, opacity = 0)
leaflet(data = quakes) %>%  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(~long, ~lat, radius = ~sqrt(exp(mag))/2, label = ~as.character(mag), color = "black", fillColor = "red", fillOpacity = 0.6, stroke = FALSE, opacity = 0)

Comments about the plot above:

  • when you zoom in, there is more detail shown
  • option to make the fillOpacity small so the patterns can be more distinguishable
  • problem: can’t distinguish between different magnitudes (which are represented in the size)
  • question: how should I scale the magnitude (as magnitude is in a log scale) and what colour should I represent to show earthquakes?
  • next step: find a way to represent plate boundaries to create another layer over this map.

Read OGR (OpenGIS Simple Features Reference Implementation)

library(rgdal)
states <- readOGR("shapefiles/cb_2016_us_state_20m.shp",
 layer = "cb_2016_us_state_20m", GDAL1_integer64_policy = TRUE)
## OGR data source with driver: ESRI Shapefile 
## Source: "shapefiles/cb_2016_us_state_20m.shp", layer: "cb_2016_us_state_20m"
## with 52 features
## It has 9 fields
## Integer64 fields read as doubles:  ALAND AWATER
leaflet(states) %>%
    addPolygons(color = "#444444", weight = 2, smoothFactor = 0.5,
                opacity = 0.1, fillOpacity = 0.8,
                fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
                highlightOptions = highlightOptions(color = "white", weight = 2,
                                                    bringToFront = TRUE))
leaflet(states) %>% addProviderTiles(providers$CartoDB.Positron) %>%
    addPolygons(color = "#444444", weight = 2, smoothFactor = 0.5,
                opacity = 0.1, fillOpacity = 0.8,
                fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
                highlightOptions = highlightOptions(color = "white", weight = 2,
                                                    bringToFront = TRUE))

Comment about the plot above:

  • question: when should one include \addProviderTiles() or \addTiles()?

Chloropleth Maps

tmp <- tempdir()
url <- "http://personal.tcu.edu/kylewalker/data/mexico.zip"
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmp)
mexico <- readOGR(dsn = tmp, layer = "mexico", encoding = "UTF-8")
## OGR data source with driver: ESRI Shapefile 
## Source: "/var/folders/p8/3y2n3_k56yb89c_qmc8jrpjc0000gn/T//RtmpL67zfu", layer: "mexico"
## with 32 features
## It has 9 fields
## Integer64 fields read as strings:  id
pal <- colorBin("Reds", mexico$gdp08, 6, pretty = FALSE) ##6 is the number of bins
state_popup <- paste0("<strong>Estado: </strong>", 
                      mexico$name, 
                      "<br><strong>PIB per c?pita, miles de pesos, 2008: </strong>", 
                      mexico$gdp08)
leaflet(data = mexico) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(fillColor = ~pal(gdp08), 
              fillOpacity = 0.8, 
              color = "grey", 
              weight = 2, 
              dashArray = "3", 
              highlight = highlightOptions(weight = 5, 
                                            
                                           bringToFront = TRUE),
              label = ~as.character(mexico$state))

Comments on the plot above

  • The bin size needs to be organized better

  • It would be good to add the value of the density to the label

  • Colour is important here - started with blue and it wasn’t very helpful as blue can be mixed with the grey in the background

  • Legend would be very useful and positioning of legend is important

tmp <- tempdir()
url <- "http://personal.tcu.edu/kylewalker/data/mexico.zip"
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmp)
mexico <- readOGR(dsn = tmp, layer = "mexico", encoding = "UTF-8")
## OGR data source with driver: ESRI Shapefile 
## Source: "/var/folders/p8/3y2n3_k56yb89c_qmc8jrpjc0000gn/T//RtmpL67zfu", layer: "mexico"
## with 32 features
## It has 9 fields
## Integer64 fields read as strings:  id
bin<-c(0,40,60, 80, 100, Inf)
pal <- colorBin("Reds", mexico$gdp08, bin, pretty = FALSE) ##6 is the number of bins
state_popup <- paste0("<strong>Estado: </strong>", 
                      mexico$name, 
                      "<br><strong>PIB per c?pita, miles de pesos, 2008: </strong>", 
                      mexico$gdp08)
leaflet(data = mexico) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(fillColor = ~pal(gdp08), 
              fillOpacity = 0.8, 
              color = "grey", 
              weight = 2, 
              dashArray = "3", 
              highlight = highlightOptions(weight = 5, 
                                            
                                           bringToFront = TRUE),
              label = ~as.character(mexico$state)) %>% addLegend(pal = pal, values = ~gdp08, opacity = 0.7, title = "GDP",
  position = "topright")

Next steps: Continuous colour

tmp <- tempdir()
url <- "http://personal.tcu.edu/kylewalker/data/mexico.zip"
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmp)
mexico <- readOGR(dsn = tmp, layer = "mexico", encoding = "UTF-8")
## OGR data source with driver: ESRI Shapefile 
## Source: "/var/folders/p8/3y2n3_k56yb89c_qmc8jrpjc0000gn/T//RtmpL67zfu", layer: "mexico"
## with 32 features
## It has 9 fields
## Integer64 fields read as strings:  id
pal <- colorNumeric("Reds", mexico$gdp08) ##6 is the number of bins
state_popup <- paste0("<strong>Estado: </strong>", 
                      mexico$name, 
                      "<br><strong>PIB per c?pita, miles de pesos, 2008: </strong>", 
                      mexico$gdp08)
leaflet(data = mexico) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(fillColor = ~pal(gdp08), 
              fillOpacity = 0.8, 
              color = "grey", 
              weight = 2, 
              dashArray = "3", 
              highlight = highlightOptions(weight = 5, 
                                            
                                           bringToFront = TRUE),
              label = ~as.character(mexico$state)) %>% addLegend(pal = pal, values = ~gdp08, opacity = 0.7, title = "GDP",
  position = "topright")

We have the same problem as before with the first discrete colour plot

References

[1] Kraak, M.J., “Visualising spatial distributions.” In P.A. Longley, &. et al, Geographical information systems: priciples, techniques, management and applications: abridged. (2005). Retrieved from https://pdfs.semanticscholar.org/36e4/8a926046f02f42fb7e18aa50d10fbf8d1f11.pdf

[2] Ayalasomayajula, V., “7 Techniques to Visualize Geospatial Data.” Social Cops. (2016) . Retrieved from https://blog.socialcops.com/academy/resources/7-techniques-to-visualize-geospatial-data/

[3]Zheng, Y., Ou, Y., Lex, A., & Phillips, J.M., “Visualization of Big Spatial Data using Coresets for Kernel Density Estimates.” Cornell University. (2017). Retrieved from https://arxiv.org/abs/1709.04453

[4] Leaflet for R., “Leaflet.” R Studio. (2016). Retrieved from https://rstudio.github.io/leaflet/

Intro Slide talking about overview of spatial data

  • what is
  • why/when use
  • advantages/disadvantages

Leaflet section

Stuff

GGmap section

citation(‘ggmap’) #david kahle & hadley wickham

Can put words here on the tile too

Overview

test, no title

Packages