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
Consider audience, interactivity and nature of data.1
(and we will return to these items throughout our prsentation)
Google Maps + HTML5 + Spatial Data Visualization: A Love Story
library(leaflet)
library(knitr)
library(rgdal)
library(ggplot2)
library(tidyverse)
library(forcats)
library(labeling)
library(ggmap)
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
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 |
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"
)
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:
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:
\addProviderTiles()
or \addTiles()
?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
[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/
Stuff
citation(‘ggmap’) #david kahle & hadley wickham
Can put words here on the tile too
test, no title