9.1 Creating a thematic map
In this post, we go through all the steps required to produce a complete good-looking map. We will see how to add a title, a legend, a scale, axis and a North arrow and choose a good color palette. To do so, we will use the Quebec province as our sampled area.
So, first, we import 2 vector layers readily available in R, the Canadian provincial boundaries and USA country boundary.
#R> Warning in getData("GADM", country = "CAN", level = 1, path = "data"): getData will be removed in a future version of raster
#R> . Please use the geodata package instead
#R> Warning in getData("GADM", country = "USA", level = 0, path = "data"): getData will be removed in a future version of raster
#R> . Please use the geodata package instead
Transform to sf
objects to facilitate manipulation
Retrieve Quebec polygon and surrounding provinces
qc <- can1_sf[can1_sf$NAME_1 == "Québec", ]
qc_neigh <- can1_sf[can1_sf$NAME_1 %in% c("Québec", "Ontario", "Nova Scotia", "New Brunswick", "Newfoundland and Labrador"), ]
It would take a while to plot because there is a lot of unnecessary details, so
we can simplify the shape of the polygons using st_simplify()
.
#R> Warning in st_simplify.sfc(st_geometry(x), preserveTopology, dTolerance):
#R> argument preserveTopology cannot be set to FALSE when working with ellipsoidal
#R> coordinates since the algorithm behind st_simplify always preserves topological
#R> relationships
#R> Warning in st_simplify.sfc(st_geometry(x), preserveTopology, dTolerance):
#R> argument preserveTopology cannot be set to FALSE when working with ellipsoidal
#R> coordinates since the algorithm behind st_simplify always preserves topological
#R> relationships
#R> Warning in st_simplify.sfc(st_geometry(x), preserveTopology, dTolerance):
#R> argument preserveTopology cannot be set to FALSE when working with ellipsoidal
#R> coordinates since the algorithm behind st_simplify always preserves topological
#R> relationships
The warning stating that st_simplify does not correctly simplify longitude/latitude data and it should be in decimal degrees. It’s important in some spatial operations involving distances, but we can ignore it for the purpose of mapping.
Let’s look at what we have:
plot(st_geometry(qc_simple), col = "grey85")
plot(st_geometry(qc_neigh_simple), col = "grey45", add = TRUE)
plot(st_geometry(usa0_simple), col = "grey25", add = TRUE)
Let’s say we sampled vegetation in 100 plots in Quebec; we now want to plot them with points proportional to their species richness. We will now create a data frame containing coordinates and random values from 5 to 50.