tmap
is a package for making thematic maps. In this Notebook, we’ll use tmap
to make a map that includes the Yosemite:
We’ll make two versions of the map - a traditional static map and an interactive map.
First, import the park boundary, cell towers, and roads. At the same time, we’ll (re)project everything into UTM 11N WGS 84.
library(sf)
Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(dplyr)
Attaching package: 㤼㸱dplyr㤼㸲
The following objects are masked from 㤼㸱package:stats㤼㸲:
filter, lag
The following objects are masked from 㤼㸱package:base㤼㸲:
intersect, setdiff, setequal, union
epsg_utm11n_wgs84 <- 32611
## Park boundary
yose_bnd_utm <- st_read(dsn="./data", layer="yose_boundary") %>%
st_transform(epsg_utm11n_wgs84)
Reading layer `yose_boundary' from data source `D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_data\data' using driver `ESRI Shapefile'
Simple feature collection with 1 feature and 11 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -119.8864 ymin: 37.4947 xmax: -119.1964 ymax: 38.18515
Geodetic CRS: North_American_Datum_1983
## Roads
yose_roads_utm <- st_read("./data/yose_roads.gdb", "Yosemite_Roads") %>%
st_transform(epsg_utm11n_wgs84)
Reading layer `Yosemite_Roads' from data source `D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_data\data\yose_roads.gdb' using driver `OpenFileGDB'
Simple feature collection with 823 features and 40 fields
Geometry type: MULTILINESTRING
Dimension: XY
Bounding box: xmin: 234658.1 ymin: 4139484 xmax: 324852.6 ymax: 4250252
Projected CRS: NAD83 / UTM zone 11N
## Cell towers
gdb_fn <- "./data/yose_communications.gdb"; file.exists(gdb_fn) ## two commands separated by ;
[1] TRUE
yose_celltwrs_utm <- st_read(gdb_fn, "Cell_Towers") %>%
st_transform(epsg_utm11n_wgs84)
Reading layer `Cell_Towers' from data source `D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_data\data\yose_communications.gdb' using driver `OpenFileGDB'
Simple feature collection with 5 features and 6 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 251532.4 ymin: 4158756 xmax: 293307.2 ymax: 4194328
Projected CRS: NAD83 / UTM zone 11N
## Campsites
yose_campgrounds_utm <- st_read(dsn="./data", layer="yose_poi") %>%
st_transform(epsg_utm11n_wgs84) %>%
filter(POITYPE == "Campground")
Reading layer `yose_poi' from data source `D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_data\data' using driver `ESRI Shapefile'
Simple feature collection with 2720 features and 30 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 246416.2 ymin: 4153717 xmax: 301510.7 ymax: 4208419
Projected CRS: NAD83 / UTM zone 11N
How many cell towers are there? How many campgrounds are there? Answer
# Your answer here
Let’s start with a simple map of just the park boundary:
library(tmap)
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
tmap_mode("plot")
tmap mode set to plotting
tm_shape(yose_bnd_utm) + tm_polygons()
Now make the fill and outline dark green, the fill 20% transparent (alpha = 0), the border line a little thicker (lwd = 2):
tm_shape(yose_bnd_utm) +
tm_polygons(col = "darkgreen", alpha = 0.2, border.col = "darkgreen", lwd = 2)
Next, we’ll add a title by adding tm_layout()
to our tmap object:
tm_shape(yose_bnd_utm) +
tm_polygons(col = "darkgreen", alpha = 0.2, border.col = "darkgreen", lwd = 2) +
tm_layout(main.title = "Yosemite NP Cell Towers")
Add a scale bar to the map. Hint: add tm_scale_bar()
to the map. Answer
# Your answer here
Next we’ll add the roads. This requires tacking on another tm_shape()
, followed by a function that draws lines.
tm_shape(yose_bnd_utm) +
tm_polygons(col = "darkgreen", alpha = 0.2, border.col = "darkgreen", lwd = 2) +
tm_shape(yose_roads_utm) +
tm_lines(col = "gray50")
In a similar fashion, we’ll add the cell towers as little blue dots:
tm_shape(yose_bnd_utm) +
tm_polygons(col = "darkgreen", alpha = 0.2, border.col = "darkgreen", lwd = 2) +
tm_shape(yose_roads_utm) +
tm_lines(col = "gray50") +
tm_shape(yose_celltwrs_utm) +
tm_symbols(col = "blue", size = 0.5)
Add the campgrounds to the map as little red dots. Hint: you can render point layers with tm_symbols()
or tm_dots()
. Answer
# Your answer here
We can switch to ‘interactive map mode’ by running tmap_mode()
:
tmap_mode("view")
tmap mode set to interactive viewing
## tmap_mode("plot") # go back to plot mode
Now that we’re in ‘view’ mode, tmap objects be rendered as little interactive maps. We can ‘redraw’ the last tmap object using tmap_last()
:
tmap_last()
Switch-out the basemap:
tm_shape(yose_bnd_utm %>% st_geometry()) +
tm_polygons(col = "darkgreen", alpha = 0.2, border.col = "darkgreen", lwd = 2) +
tm_shape(yose_roads_utm %>% st_geometry()) +
tm_lines(col = "gray50") +
tm_shape(yose_celltwrs_utm %>% st_geometry()) +
tm_symbols(col = "blue", size = 0.5) +
tm_shape(yose_campgrounds_utm %>% st_geometry()) +
tm_symbols(col = "red", size = 0.5) +
tm_basemap("Esri.WorldTopoMap")
Lastly we’ll disable interactivity (i.e., hover-over text and popup windows) on all layers except for the campgrounds:
tm_shape(yose_bnd_utm %>% st_geometry()) +
tm_polygons(col = "darkgreen", alpha = 0.2, border.col = "darkgreen", lwd = 2, interactive = FALSE) +
tm_shape(yose_roads_utm %>% st_geometry()) +
tm_lines(col = "gray50", interactive = FALSE) +
tm_shape(yose_celltwrs_utm %>% st_geometry()) +
tm_symbols(col = "blue", size = 0.5, interactive = FALSE) +
tm_shape(yose_campgrounds_utm %>% select(POINAME)) +
tm_symbols(col = "red", size = 0.5, id = "POINAME") +
tm_basemap("Esri.WorldTopoMap")
Tip: for more control over pop-ups, use leaflet.
Congratulations, you have completed the Notebook!
To view your Notebook at HTML, save it (again), then click the ‘Preview’ button in the RStudio toolbar. If