library(sf) library(spData) library(terra)
E1. Use summary()
on the geometry column of the world
data object that is included in the spData package. What does the output tell us about:
summary(world) # - Its geometry type? # multipolygon # - The number of countries? # 177 # - Its coordinate reference system (CRS)? # epsg:4326
E2. Run the code that 'generated' the map of the world in Section 2.2.3 (Basic map-making). Find two similarities and two differences between the image on your computer and that in the book.
cex
argument do (see ?plot
)?cex
set to the sqrt(world$pop) / 10000
?plot(world["continent"], reset = FALSE) cex = sqrt(world$pop) / 10000 world_cents = st_centroid(world, of_largest = TRUE) plot(st_geometry(world_cents), add = TRUE, cex = cex) # - What does the `cex` argument do (see `?plot`)? # It specifies the size of the circles # - Why was `cex` set to the `sqrt(world$pop) / 10000`? # So the circles would be visible for small countries but not too large for large countries, also because area increases as a linear function of the square route of the diameter defined by `cex` # - Bonus: experiment with different ways to visualize the global population. plot(st_geometry(world_cents), cex = world$pop / 1e9) plot(st_geometry(world_cents), cex = world$pop / 1e8) plot(world["pop"]) plot(world["pop"], logz = TRUE) # Similarities: global extent, colorscheme, relative size of circles # # Differences: projection (Antarctica is much smaller for example), graticules, location of points in the countries. # # To understand these differences read-over, run, and experiment with different argument values in this script: https://github.com/geocompx/geocompr/raw/main/code/02-contpop.R # # `cex` refers to the diameter of symbols plotted, as explained by the help page `?graphics::points`. It is an acronym for 'Chacter symbol EXpansion'. # It was set to the square route of the population divided by 10,000 because a) otherwise the symbols would not fit on the map and b) to make circle area proportional to population.
E3. Use plot()
to create maps of Nigeria in context (see Section 2.2.3).
lwd
, col
and expandBB
arguments of plot()
. text()
and annotate the map.nigeria = world[world$name_long == "Nigeria", ] plot(st_geometry(nigeria), expandBB = c(0, 0.2, 0.1, 1), col = "gray", lwd = 3) plot(world[0], add = TRUE) world_coords = st_coordinates(world_cents) text(world_coords, world$iso_a2) # Alternative answer: nigeria = world[world$name_long == "Nigeria", ] africa = world[world$continent == "Africa", ] plot(st_geometry(nigeria), col = "white", lwd = 3, main = "Nigeria in context", border = "lightgray", expandBB = c(0.5, 0.2, 0.5, 0.2)) plot(st_geometry(world), lty = 3, add = TRUE, border = "gray") plot(st_geometry(nigeria), col = "yellow", add = TRUE, border = "darkgray") a = africa[grepl("Niger", africa$name_long), ] ncentre = st_centroid(a) ncentre_num = st_coordinates(ncentre) text(x = ncentre_num[, 1], y = ncentre_num[, 2], labels = a$name_long)
E4. Create an empty SpatRaster
object called my_raster
with 10 columns and 10 rows.
Assign random values between 0 and 10 to the new raster and plot it.
my_raster = rast(ncol = 10, nrow = 10, vals = sample(0:10, size = 10 * 10, replace = TRUE)) plot(my_raster)
E5. Read-in the raster/nlcd.tif
file from the spDataLarge package.
What kind of information can you get about the properties of this file?
nlcd = rast(system.file("raster/nlcd.tif", package = "spDataLarge")) dim(nlcd) # dimensions res(nlcd) # resolution ext(nlcd) # extent nlyr(nlcd) # number of layers cat(crs(nlcd)) # CRS
E6. Check the CRS of the raster/nlcd.tif
file from the spDataLarge package.
What kind of information you can learn from it?
cat(crs(nlcd))
{asis 02-ex-e62, message = FALSE}
The WKT above describes a two-dimensional projected coordinate reference system.
It is based on the GRS 1980 ellipsoid with North American Datum 1983 and the Greenwich prime meridian.
It used the Transverse Mercator projection to transform from geographic to projected CRS (UTM zone 12N).
Its first axis is related to eastness, while the second one is related to northness, and both axes have units in meters.
The SRID of the above CRS is "EPSG:26912".
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.