Chapter 1-2
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
# First plot
nc <- st_read(system.file("shape/nc.shp", package = "sf"),
quiet = TRUE
)
plot(nc)
## Warning: plotting the first 10 out of 14 attributes; use max.plot = 14 to plot
## all

plot(st_geometry(nc))

ggplot(data = nc, aes(fill = SID74)) + geom_sf() +
scale_fill_viridis() + theme_bw()

# second Diagram From The Geospatial Book (Rainfall Map)
library(geoR)
## --------------------------------------------------------------
## Analysis of Geostatistical Data
## For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
## geoR version 1.9-6 (built on 2025-08-29) is now loaded
## --------------------------------------------------------------
ggplot(data.frame(cbind(parana$coords, Rainfall = parana$data)))+
geom_point(aes(east, north, color = Rainfall), size = 2) +
coord_fixed(ratio = 1) +
scale_color_gradient(low = "blue", high = "orange") +
geom_path(data = data.frame(parana$border), aes(east, north)) +
theme_bw()

#Cholera Outbreak Map
library(cholera)
rng<-mapRange()
plot(fatalities[,c("x","y")],
pch =15,col ="black",
cex =0.5,xlim =rng$x,ylim =rng$y,asp =1,
frame.plot =FALSE,axes =FALSE,xlab ="",ylab =""
)
addRoads()

# name of the shapefile of North Carolina of the sf package
nameshp <- system.file("shape/nc.shp", package = "sf")
#readshapefilewith st_read()
library(sf)
map<-st_read(nameshp,quiet=TRUE)
class(map)
## [1] "sf" "data.frame"
head(map)
## Simple feature collection with 6 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
## Geodetic CRS: NAD27
## AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74
## 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1
## 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0
## 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5
## 4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1
## 5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9
## 6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7
## NWBIR74 BIR79 SID79 NWBIR79 geometry
## 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
## 2 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
## 3 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
## 4 123 830 2 145 MULTIPOLYGON (((-76.00897 3...
## 5 1066 1606 3 1197 MULTIPOLYGON (((-77.21767 3...
## 6 954 1838 5 1237 MULTIPOLYGON (((-76.74506 3...
plot(map)
## Warning: plotting the first 10 out of 14 attributes; use max.plot = 14 to plot
## all

# Mapofsudden infant deaths in North Carolina in 1974, created with ggplot2
library(ggplot2)
map <- st_as_sf(map)
ggplot(map) + geom_sf(aes(fill = SID74)) + theme_bw()

# Mapofsudden infant deaths in North Carolina in 1974, created with ggplot2 and viridis scale.
library(viridis)
map <- st_as_sf(map)
ggplot(map) + geom_sf(aes(fill = SID74)) +
scale_fill_viridis() + theme_bw()

# Map of sudden infant deaths in North Carolina in 1974, created with leaflet
st_crs(map)
## Coordinate Reference System:
## User input: NAD27
## wkt:
## GEOGCRS["NAD27",
## DATUM["North American Datum 1927",
## ELLIPSOID["Clarke 1866",6378206.4,294.978698213898,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4267]]
map <- st_transform(map, 4326)
library(leaflet)
pal <- colorNumeric("YlOrRd", domain = map$SID74)
leaflet(map) %>%
addTiles() %>%
addPolygons(
color = "white", fillColor = ~ pal(SID74),
fillOpacity = 1
) %>%
addLegend(pal = pal, values = ~SID74, opacity = 1)
# Map of sudden infant deaths in North Carolina in 1974, created with mapview
library(mapview)
mapview(map, zcol = "SID74")
# Map of sudden infant deaths in North Carolina in 1974, created with mapview and with legend with RColorBrewer color palette
library(RColorBrewer)
pal <- colorRampPalette(brewer.pal(9, "YlOrRd"))
mapview(map,
zcol = "SID74",
map.types = "CartoDB.DarkMatter",
col.regions = pal
)
#Snapshot of synchronized maps of sudden infant deaths in North Carolina in 1974 and 1979.
library(leafsync)
m74 <- mapview(map, zcol = "SID74")
m79 <- mapview(map, zcol = "SID79")
m <- sync(m74, m79)
m
# Interactive map of sudden infant deaths in North Carolina in 1974, created with tmap.
library(tmap)
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
tm_shape(map) + tm_polygons("SID74")