# Load vector layers from the working directory
tracts = st_read("slc_tract_2015.shp", quiet = TRUE)
lightrail = st_read("LightRail_UTA.shp", quiet = TRUE)
stations = st_read("LightRailStations_UTA.shp", quiet = TRUE)
# Check coordinate reference systems
st_crs(tracts)$epsg[1] 4269
st_crs(lightrail)$epsg[1] 26912
# Reproject the tracts to match the light rail data
tracts = st_transform(tracts, st_crs(lightrail))
# Set static tmap mode
tmap_mode("plot")ℹ tmap modes "plot" - "view"
ℹ toggle with `tmap::ttm()`
# Basic polygon outline map
tm_shape(tracts) +
tm_borders()# Fill polygons by density
tm_shape(tracts) +
tm_polygons(
fill = "density",
fill.scale = tm_scale_continuous(values = "brewer.greens"),
fill.legend = tm_legend(title = "Popn density")
)# Use a color palette with quantile breaks
tm_shape(tracts) +
tm_polygons(
fill = "density",
fill.scale = tm_scale_intervals(
values = "brewer.greens",
style = "quantile"
),
fill.legend = tm_legend(title = "Popn density")
) +
tm_borders("lightgray")# Different palette
# run cols4all::c4a_palettes() to see valid colors
tm_shape(tracts) +
tm_polygons(
fill = "density",
fill.scale = tm_scale_continuous(values = "bivario.plum_mint"),
fill.legend = tm_legend(title = "Popn density")
) +
tm_borders("lightgray")#Add light rail
tm_shape(tracts) +
tm_fill("density", palette = "brewer.greens", style = "quantile") +
tm_borders("lightgray") +
tm_shape(lightrail) +
tm_lines(lwd = 2, lty = 'dashed', col = "darkorange")
── tmap v3 code detected ───────────────────────────────────────────────────────
[v3->v4] `tm_fill()`: instead of `style = "quantile"`, use fill.scale =
`tm_scale_intervals()`.
ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
'tm_scale_intervals(<HERE>)'
# Use the route variable to theme the light rail lines
tm_shape(tracts) +
tm_borders("lightgray") +
tm_shape(lightrail) +
tm_lines(lwd = 4, col = "ROUTE")# Add the light rail tracks and stations
tm_shape(tracts) +
tm_polygons(
fill = "density",
fill.scale = tm_scale_intervals(values = "brewer.greens", style = "quantile"),
fill.legend = tm_legend(title = "Popn density")
) +
tm_borders("lightgray") +
tm_shape(lightrail) +
tm_lines(lwd = 2) +
tm_shape(stations) +
tm_dots(size = 0.25, shape = 23)# Add map elements
tm_shape(tracts) +
tm_graticules(col = "lightgray") +
tm_polygons(
fill = "density",
fill.scale = tm_scale_intervals(values = "brewer.greens", style = "quantile"),
fill.legend = tm_legend(title = "Popn density")
) +
tm_borders("lightgray") +
tm_shape(lightrail) +
tm_lines(lwd = 2) +
tm_shape(stations) +
tm_dots(size = 0.25, shape = 23) +
tm_compass(position = c("left", "bottom")) +
tm_scalebar(position = c("right", "top")) +
tm_title("Salt Lake County Light Rail")# Clip the map to downtown Salt Lake using the bbox argument
tracts_sub = tracts %>%
dplyr::filter(TRACTCE %in% c(102500, 102600, 114000))
tm_shape(tracts, bbox = st_bbox(tracts_sub)) +
tm_borders("lightgray") +
tm_shape(lightrail) +
tm_lines(lwd = 2) +
tm_shape(stations) +
tm_dots(size = 0.25, shape = 23) +
tm_text("STATIONNAM", ymod = -1, bg.color = "white", size = 0.8)
── tmap v3 code detected ───────────────────────────────────────────────────────
# Set interactive tmap mode
tmap_mode("view")ℹ tmap modes "plot" - "view"
tm_shape(tracts) +
tm_polygons(
fill = "density",
fill.scale = tm_scale_intervals(values = "brewer.greens", style = "quantile"),
fill.legend = tm_legend(title = "Popn density")
) +
tm_borders("lightgray") +
tm_shape(lightrail) +
tm_lines(lwd = 2) +
tm_shape(stations) +
tm_dots(size = 0.25, shape = 23)# Reset to static mode for the remaining examples
tmap_mode("plot")ℹ tmap modes "plot" - "view"