Maggie Hallerud
March 14, 2018
A choropleth map with the sp package. Include a title and use RColorBrewer to create your own palette. Don’t forget to add a title to your map! (2)
# load required packages
library(ggmap)
library(rgdal)
library(RColorBrewer)
library(tmap)
library(tidyverse)
library(spdplyr)
# load spatial data abilities
getClass('Spatial')
## Class "Spatial" [package "sp"]
##
## Slots:
##
## Name: bbox proj4string
## Class: matrix CRS
##
## Known Subclasses:
## Class "SpatialPoints", directly
## Class "SpatialMultiPoints", directly
## Class "SpatialGrid", directly
## Class "SpatialLines", directly
## Class "SpatialPolygons", directly
## Class "SpatialPointsDataFrame", by class "SpatialPoints", distance 2
## Class "SpatialPixels", by class "SpatialPoints", distance 2
## Class "SpatialMultiPointsDataFrame", by class "SpatialMultiPoints", distance 2
## Class "SpatialGridDataFrame", by class "SpatialGrid", distance 2
## Class "SpatialLinesDataFrame", by class "SpatialLines", distance 2
## Class "SpatialPixelsDataFrame", by class "SpatialPoints", distance 3
## Class "SpatialPolygonsDataFrame", by class "SpatialPolygons", distance 2
# load census data (polygons)
tracts <- readOGR("C:/Users/Maggie/Documents/GEOG4950/census/CensusTracts2010.shp")
## OGR data source with driver: ESRI Shapefile
## Source: "C:/Users/Maggie/Documents/GEOG4950/census/CensusTracts2010.shp", layer: "CensusTracts2010"
## with 588 features
## It has 31 fields
## Integer64 fields read as strings: P0010011 P0010012 P0010013
data <- tracts@data
head(data)
## STATEFP10 COUNTYFP10 TRACTCE10 GEOID10 NAME10 FUNCSTAT10
## 0 49 003 960601 49003960601 9606.01 S
## 1 49 003 960602 49003960602 9606.02 S
## 2 49 003 960702 49003960702 9607.02 S
## 3 49 003 960400 49003960400 9604 S
## 4 49 003 960300 49003960300 9603 S
## 5 49 003 960200 49003960200 9602 S
## INTPTLAT10 INTPTLON10 LOGRECNO AREALAND AREAWATR POP100 HU100
## 0 +41.5159999 -112.0089180 0000009 2185421 0 3720 1395
## 1 +41.5292534 -111.9843889 0000010 31111836 0 2429 793
## 2 +41.4966363 -112.0069151 0000012 3141945 0 2281 861
## 3 +41.5599748 -112.2854779 0000007 657894066 101858980 4872 1632
## 4 +41.7027473 -112.1516054 0000006 35869732 0 8186 2748
## 5 +41.7070478 -112.0582397 0000005 259632270 547505 6901 2303
## P0020001 P0020002 P0020003 P0020004 P0020005 P0020006 P0020007 P0020008
## 0 3720 396 3324 3262 3188 17 27 15
## 1 2429 109 2320 2276 2245 3 16 8
## 2 2281 292 1989 1947 1887 11 28 18
## 3 4872 289 4583 4531 4437 20 13 51
## 4 8186 927 7259 7158 6959 22 34 120
## 5 6901 406 6495 6416 6289 16 18 74
## P0020009 P0020010 MTFCC P0010011 P0010012 P0010013 P0010014 SqMiles
## 0 6 9 G5020 11 30 17 0 0
## 1 4 0 G5020 9 23 12 0 0
## 2 3 0 G5020 2 24 16 0 0
## 3 2 8 G5020 2 26 26 0 0
## 4 14 9 G5020 8 43 45 0 0
## 5 18 1 G5020 9 34 27 0 0
## SHAPE_Leng SHAPE_Area
## 0 6709.768 2184057
## 1 31613.347 31092117
## 2 7088.986 3139977
## 3 147127.817 759360502
## 4 43217.387 35849141
## 5 123915.036 260023433
# check data class of var of interest and make mappable as 'numeric'
class(data$SHAPE_Area)
## [1] "numeric"
tracts$AREAWATR <- as.numeric(tracts$AREAWATR)
# create color palette of choice
pal <- brewer.pal(n=9,name='Blues')
spplot(tracts, # plot census tracts
"AREAWATR", # `AREAWATR` as choropleth sybmoblized variable
col.regions=pal, # specify color palette
cuts=8, # specify # breaks (n-1 from pal)
col='transparent',
main="Water Resources in Utah (square-meters)") # title
A map with two layers (point, polygon, or polyline) and a basemap using ggplot and ggmap. Include a title and be sure to fix the coordinate ratio. (4)
# load trails (polylines) and cities (points) data
trails <- readOGR("C:/Users/Maggie/Documents/GEOG4950/HistoricTrails/HistoricTrails.shp")
## OGR data source with driver: ESRI Shapefile
## Source: "C:/Users/Maggie/Documents/GEOG4950/HistoricTrails/HistoricTrails.shp", layer: "HistoricTrails"
## with 3415 features
## It has 9 fields
cities <- readOGR("C:/Users/Maggie/Documents/GEOG4950/CitiesTownsLocations/CitiesTownsLocations.shp")
## OGR data source with driver: ESRI Shapefile
## Source: "C:/Users/Maggie/Documents/GEOG4950/CitiesTownsLocations/CitiesTownsLocations.shp", layer: "CitiesTownsLocations"
## with 460 features
## It has 7 fields
## Integer64 fields read as strings: POPULATION
# transform to ensure matching coordinate systems
crs <- CRS("+init=epsg:4269")
cities <- spTransform(cities, crs)
trails <- spTransform(trails, crs)
# filter out desired trails and save as new object
unique(trails$TrailName)
## [1] California Trail Mormon Pioneer Trail
## [3] Old Spanish Trail Pony Express Trail
## [5] Stansbury Route Dominguiez-Escalante Route
## [7] Fremont Route Hole-In-The-Rock Trail
## 8 Levels: California Trail Dominguiez-Escalante Route ... Stansbury Route
routes <- trails %>%
filter(TrailName %in% c("Stansbury Route","Fremont Route"))
# convert target data to df's & merge with shp's for proper mapping
cities_df <- data.frame(cities)
routes_df <- fortify(routes)
routes@data$id <- rownames(routes@data)
routes_df <- merge(routes_df, routes@data, by="id")
# specify basemap
base_map <- get_map(location = "utah", zoom = 6, maptype = "roadmap", source = "google", crop = T)
ggmap(base_map) + # baselayer
ggtitle("Major Foot Routes Centered in Utah\nStansbury (N) and Fremont (S)") + # map title
# simplify background and specify title elements
theme(plot.title=element_text(color="black", face="bold", size=14),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text=element_blank()) +
# plot cities using small black points
geom_point(data=cities_df,
mapping=aes(x=coords.x1,y=coords.x2),
color='black', pch=20) +
# plot trails as red medium-thick lines
geom_path(data=routes_df,
mapping=aes(x=long,y=lat,group=group),color='red',lwd=2) +
# fix coordinates
coord_fixed()
A map with two layers using tmap. Add a title, compass, and scale bar. (4)
# plot census data
tm_shape(tracts) +
# choropleth with AREAWATR as symbolized variable
tm_fill(col="AREAWATR",style="jenks",
alpha=0.4, #transparency of layer
title="Water Area (sq-m)", #legend title
palette=pal) + #color palette
tm_borders(alpha=0.3) + #transparency of polygon borders
tm_compass(position=c("right","top")) + #add compass
tm_scale_bar() + #add scale bar
tm_shape(cities) + #draw cities
tm_dots(col='brown',size=0.08) + #specify city points' elements
tm_style_gray() + #specify background style
tm_legend(bg.color='gray', #legend background
bg.alpha=1, #background transparency
frame="gray50", #frame color
height=0.6) + #?
#legend layout
tm_layout(legend.outside=T,
legend.position=c("left","top"))