The new version of the book is now available mdsr2e. The new chapter Chapter 17 Working with geospatial data used the new R package sf and could be further updated to the the new R package cholera.
library(tidyverse)
library(mdsr)
library(sf)
plot(CholeraDeaths["Count"])
It is probably best to do this in an R Project.
download.file("http://rtwilson.com/downloads/SnowGIS_SHP.zip",
dest="SnowGIS.zip", mode="wb")
trying URL 'http://rtwilson.com/downloads/SnowGIS_SHP.zip'
Content type 'application/zip' length 6875824 bytes (6.6 MB)
==================================================
downloaded 6.6 MB
unzip("SnowGIS.zip")
dsn <- fs::path("SnowGIS_SHP")
list.files(dsn)
[1] "Cholera_Deaths.dbf" "Cholera_Deaths.prj" "Cholera_Deaths.sbn"
[4] "Cholera_Deaths.sbx" "Cholera_Deaths.shp" "Cholera_Deaths.shx"
[7] "OSMap_Grayscale.tfw" "OSMap_Grayscale.tif" "OSMap_Grayscale.tif.aux.xml"
[10] "OSMap_Grayscale.tif.ovr" "OSMap.tfw" "OSMap.tif"
[13] "Pumps.dbf" "Pumps.prj" "Pumps.sbx"
[16] "Pumps.shp" "Pumps.shx" "README.txt"
[19] "SnowMap.tfw" "SnowMap.tif" "SnowMap.tif.aux.xml"
[22] "SnowMap.tif.ovr"
st_layers(dsn)
Driver: ESRI Shapefile
Available layers:
layer_name geometry_type features fields
1 Pumps Point 8 1
2 Cholera_Deaths Point 250 2
CholeraDeaths <- st_read(dsn, layer = "Cholera_Deaths")
Reading layer `Cholera_Deaths' from data source `/home/esuess/classes/2020-2021/01 - Fall 2020/Stat651/Presentations/08_spatial/spatial4/SnowGIS_SHP' using driver `ESRI Shapefile'
Simple feature collection with 250 features and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: 529160.3 ymin: 180857.9 xmax: 529655.9 ymax: 181306.2
epsg (SRID): NA
proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
class(CholeraDeaths)
[1] "sf" "data.frame"
CholeraDeaths
Simple feature collection with 250 features and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: 529160.3 ymin: 180857.9 xmax: 529655.9 ymax: 181306.2
epsg (SRID): NA
proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
First 10 features:
Id Count geometry
1 0 3 POINT (529308.7 181031.4)
2 0 2 POINT (529312.2 181025.2)
3 0 1 POINT (529314.4 181020.3)
4 0 1 POINT (529317.4 181014.3)
5 0 4 POINT (529320.7 181007.9)
6 0 2 POINT (529336.7 181006)
7 0 2 POINT (529290.1 181024.4)
8 0 2 POINT (529301 181021.2)
9 0 3 POINT (529285 181020.2)
10 0 2 POINT (529288.4 181031.8)
summary(CholeraDeaths)
Id Count geometry
Min. :0 Min. : 1.000 POINT :250
1st Qu.:0 1st Qu.: 1.000 epsg:NA : 0
Median :0 Median : 1.000 +proj=tmer...: 0
Mean :0 Mean : 1.956
3rd Qu.:0 3rd Qu.: 2.000
Max. :0 Max. :15.000
ggplot(CholeraDeaths) +
geom_sf()
In the new edition of the book the authors use Open Street Maps instead of Google Maps API.
library(ggspatial)
ggplot(CholeraDeaths) +
annotation_map_tile(type = "osm", zoomin = 0) +
geom_sf(aes(size = Count), alpha = 0.7)
st_bbox(CholeraDeaths)
xmin ymin xmax ymax
529160.3 180857.9 529655.9 181306.2
library(mapproj)
library(maps)
map("world", projection = "mercator", wrap = TRUE)
projection failed for some data
map("world", projection = "cylequalarea", param = 45, wrap = TRUE)
map(
"state", projection = "lambert",
parameters = c(lat0 = 20, lat1 = 50), wrap = TRUE
)
map(
"state", projection = "albers",
parameters = c(lat0 = 20, lat1 = 50), wrap = TRUE
)
st_crs(CholeraDeaths)
Coordinate Reference System:
No EPSG code
proj4string: "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs"
st_crs(4326)$epsg
[1] 4326
st_crs(3857)
Coordinate Reference System:
EPSG: 3857
proj4string: "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"
st_crs(27700)$proj4string
[1] "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs"
cholera_4326 <- CholeraDeaths %>%
st_transform(4326)
st_bbox(cholera_4326)
xmin ymin xmax ymax
-0.1384685 51.5113460 -0.1313274 51.5153252
ggplot(cholera_4326) +
annotation_map_tile(type = "osm", zoomin = 0) +
geom_sf(aes(size = Count), alpha = 0.7)
help("spTransform-methods", package = "rgdal")
st_crs(CholeraDeaths)$proj4string
[1] "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs"
cholera_latlong <- CholeraDeaths %>%
st_set_crs(27700) %>%
st_transform(4326)
st_crs<- : replacing crs does not reproject data; use st_transform for that
snow <- ggplot(cholera_latlong) +
annotation_map_tile(type = "osm", zoomin = 0) +
geom_sf(aes(size = Count))
pumps <- st_read(dsn, layer = "Pumps")
Reading layer `Pumps' from data source `/home/esuess/classes/2020-2021/01 - Fall 2020/Stat651/Presentations/08_spatial/spatial4/SnowGIS_SHP' using driver `ESRI Shapefile'
Simple feature collection with 8 features and 1 field
geometry type: POINT
dimension: XY
bbox: xmin: 529183.7 ymin: 180660.5 xmax: 529748.9 ymax: 181193.7
epsg (SRID): NA
proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
pumps_latlong <- pumps %>%
st_set_crs(27700) %>%
st_transform(4326)
st_crs<- : replacing crs does not reproject data; use st_transform for that
snow +
geom_sf(data = pumps_latlong, size = 3, color = "red")
library(tidygeocoder)
white_house <- tibble(
address = "The White House, Washington, DC"
) %>%
tidygeocoder::geocode(address, method = "osm")
library(leaflet)
white_house_map <- leaflet() %>%
addTiles() %>%
addMarkers(data = white_house)
Assuming "long" and "lat" are longitude and latitude, respectively
white_house_map
white_house <- white_house %>%
mutate(
title = "The White House",
street_address = "1600 Pennsylvania Ave"
)
white_house_map %>%
addPopups(
data = white_house,
popup = ~paste0("<b>", title, "</b></br>", street_address)
)
Assuming "long" and "lat" are longitude and latitude, respectively