@dataknut
)To learn how to do mapping in R (markdown). Also uses data.table (Dowle et al. 2015) for data manipulation and ggplot (Wickham 2009).
Files we’re going to use:
Skipped due to google maps API requirement.
See https://r-spatial.github.io/sf/ for documentation
Let’s draw a map of the Dunedin district. For those who don’t know this is way bigger than the urban area itself - it includes a lot of forests for example!
Figure ?? shows two very basic sf-based maps of Dunedin, New Zealand. The first shows just boundaries, the second calculates a test value using qnorm and applies a colour aesthetic to map it.
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
myParams$dunners.shp <- sf::st_read(myParams$dunnersFile)
## Reading layer `AU_TA_Dunedin_CC' from data source `/Users/ben/Data/NZ_Census/gis/2006/Otago_L2_2006_NZMG_ArcShp/AU_TA_Dunedin_CC.shp' using driver `ESRI Shapefile'
## Simple feature collection with 73 features and 5 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 2255751 ymin: 5458074 xmax: 2335181 ymax: 5549615
## epsg (SRID): NA
## proj4string: +proj=nzmg +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150 +datum=nzgd49 +units=m +no_defs
m <- ggplot2::ggplot() +
geom_sf(data = myParams$dunners.shp)
m
Figure 2.1: sf - basic map examples
myParams$dunners.shp$testNumeric <- rnorm(n = nrow(myParams$dunners.shp)) # generate some numbers
m <- ggplot2::ggplot() +
geom_sf(data = myParams$dunners.shp,
aes(fill = myParams$dunners.shp$testNumeric)) + # aes works as you'd expect in ggplot
scale_fill_continuous(low = "red", high = "green")
m
Figure 2.2: sf - basic map examples
Next in ?? we try adding a map background using ggspatial
(Dunnington 2018). By default ggspatial
seems to use Open Street Map. The first one just shows the area unit boundaries. The second shows the test numeric value we calculated before. In each case we use the alpha aesthetic to make the data layer translucent so that we can see the background tile.
library(ggspatial)
myZoom = 10
myAlpha <- 0.3
# will fail if no internet
m <- ggplot2::ggplot() +
annotation_map_tile(zoom = myZoom) + # do this first otherwise it masks the data layers
geom_sf(data = myParams$dunners.shp, aes(alpha = myAlpha)) +
scale_fill_continuous(low = "red", high = "green")
m
## Zoom: 10
Figure 2.3: sf - map tile examples
m <- ggplot2::ggplot() +
annotation_map_tile(zoom = myZoom) + # do this first otherwise it masks the data layers
geom_sf(data = myParams$dunners.shp, aes(fill = myParams$dunners.shp$testNumeric,
alpha = myAlpha)) +
scale_fill_continuous(low = "red", high = "green")
m
## Zoom: 10
Figure 2.4: sf - map tile examples
Figure 2.5 shows where R started :-)
library(leaflet)
# will fail if no internet
# uses pipes (doesn't have to)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
Figure 2.5: leaflet - basic plot showing the birthplace of R!
How do we do .shp files in leaflet?
To do
Analysis completed in: 13.71 seconds using knitr in RStudio with R version 3.5.2 (2018-12-20) running on x86_64-apple-darwin15.6.0.
R packages used (data.table, ggplot2 plus others loaded where relevant):
Appelhans, Tim, Florian Detsch, Christoph Reudenbach, and Stefan Woellauer. 2018. Mapview: Interactive Viewing of Spatial Data in R. https://CRAN.R-project.org/package=mapview.
Cheng, Joe, Bhaskar Karambelkar, and Yihui Xie. 2018. Leaflet: Create Interactive Web Maps with the Javascript ’Leaflet’ Library. https://CRAN.R-project.org/package=leaflet.
Dowle, M, A Srinivasan, T Short, S Lianoglou with contributions from R Saporta, and E Antonyan. 2015. Data.table: Extension of Data.frame. https://CRAN.R-project.org/package=data.table.
Dunnington, Dewey. 2018. Ggspatial: Spatial Data Framework for Ggplot2. https://CRAN.R-project.org/package=ggspatial.
Exeter, Daniel John, Jinfeng Zhao, Sue Crengle, Arier Lee, and Michael Browne. 2017. “The New Zealand Indices of Multiple Deprivation (IMD): A New Suite of Indicators for Social and Health Research in Aotearoa, New Zealand.” PloS One 12 (8): e0181260.
Kahle, David, and Hadley Wickham. 2013. “Ggmap: Spatial Visualization with Ggplot2.” The R Journal 5 (1): 144–61. http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf.
Pebesma, Edzer. 2018. “Simple Features for R: Standardized Support for Spatial Vector Data.” The R Journal. https://journal.r-project.org/archive/2018/RJ-2018-009/index.html.
R Core Team. 2016. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2009. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. http://ggplot2.org.
Xie, Yihui. 2016. Knitr: A General-Purpose Package for Dynamic Report Generation in R. https://CRAN.R-project.org/package=knitr.