sp and maps hands on - PMD code

library(readxl)
library(sp)
library(RgoogleMaps)
## Warning: package 'RgoogleMaps' was built under R version 3.6.2

1)

iapop <- read_excel('IAcountyPop.xlsx')
iapop <- as.data.frame(iapop)
names(iapop)
## [1] "number"    "Name"      "Latitude"  "Longitude" "Seat"      "pop2019"  
## [7] "growth"
iapop.sp <- iapop
iapop.sp$Longitude <- -iapop.sp$Longitude
# if you forgot to make negative longitude, you'll 
#   find out when you plot locations in #2

coordinates(iapop.sp) <- c('Longitude', 'Latitude')
proj4string(iapop.sp) <- CRS('+proj=longlat +datum=NAD83')
# no output needed

2)

plot(iapop.sp, axes=T)

3)

iapop.utm <- spTransform(iapop.sp, 
  CRS('+proj=utm +zone=15'))
plot(iapop.utm, axes=T)

4)

# find which rows are Pottawattamie and Dubuque Co's
# match() looks for exact matches, pmatch() partial matches
# of the first argument in the second (a vector)
#  returns position in the second
#  and a vector of positions if 1st arg is a vector

pmatch(c('Pott', 'Dub'), iapop$Name)
## [1] 41 17
# calculate great circle distance (in km)
spDistsN1(iapop.sp[41,], iapop.sp[17,], longlat=T)
## [1] 452.5637
# and Euclidean distance converted to km
spDistsN1(iapop.utm[41,], iapop.utm[17,])/1000
## [1] 452.4723

5)

spplot(iapop.sp, 'pop2019', col.regions=cm.colors(10) )

6)

iapop.sp$logpop <- log10(iapop.sp$pop2019)
spplot(iapop.sp, 'logpop', col.regions=cm.colors(10) )

7)

bubble(iapop.sp, 'pop2019')

8)

bubble(iapop.sp, 'growth')

9)

sc.lat <- as.numeric(char2dms("42d01'N"))
sc.long <- as.numeric(char2dms("93d09'49\"W"))
c(lat=sc.lat, long=sc.long)
##       lat      long 
##  42.01667 -93.16361

10)

ia5 <- GetMap(c(sc.lat, sc.long), zoom=5)
# too large
ia6 <- GetMap(c(sc.lat, sc.long), zoom=6)
# too large
ia7 <- GetMap(c(sc.lat, sc.long), zoom=7)
# just right
PlotOnStaticMap(ia7)

11)

temp <- coordinates(iapop.sp)
PlotOnStaticMap(ia7, temp[,2], temp[,1], pch=19, col=4)

12)

bubbleMap(iapop.sp, map=ia7, zcol='pop2019')