The raster data format is commonly used for environmental datasets such as elevation, climate, soil, and land cover. We commonly need to extract
the data from raster objects using simple features (vector objects). For example if you had a set of points you collected using a GPS and wanted to know the mean annual temperature at each point, you might extract
the data from each location in a raster-based map of temperature.
You could also be interested in some summary of the raster data across multiple pixels (such as the buffered points above, a transect, or within a polygon). For example, you might be interested in the mean elevation within the entire polygon in the above figure.
In this case study we’ll work with a timeseries of temperature data from WorldClim. These are near-global rasters of various climatic variables available at several resolutions. For convenience, we’ll work with the very coarse data (0.5 degree, which is about 50km), but much finer data are available (~1km).
Identify the hottest country on each continent (not counting Antarctica) by intersecting a set of polygons with a raster image and calculating the maximum raster value in each polygon.
world
datasetDownload starter R script (if desired). Save this directly to your course folder (repository) so you don’t lose track of it!
The details below describe one possible approach.
You will need to load the following packages
library(raster)
library(sf)
library(sp)
library(spData)
library(tidyverse)
Loading the spData()
package may return a warning: To access larger datasets...install spDataLarge...
. This is not required - you can use the standard lower resolution files and safely ignore this message.
data(world) #load 'world' data from spData package
tmax_monthly <- getData(name = "worldclim", var="tmax", res=10)
world
object).
filter()
because WorldClim does not have data there.world
object to sp
format (the ‘old’ format) because the raster
package doesn’t accept sf
objects. you can do this with as(world,"Spatial")
.tmax_monthly=getData(...)
).tmax_monthly
object (you can start by just typing it’s name tmax_monthly
, then perhaps making a plot()
). How many layers does it have? What do these represent? What are the units? You can read more about the data heregain()
to convert to Degrees C. You can figure this out using information found here and ?gain()
. What value do you need to multiply with the data to get degrees C?tmax_annual
that is the annual maximum temperature in each pixel of the raster stack using max()
. This will find the maximum temperature in each location across all months.names(tmax_annual) <- "tmax"
to change the name of the layer in the new tmax_annual
object to tmax
. This makes the later code more intuitive than keeping the default name layer
.raster::extract()
to identify the maximum temperature observed in each country (fun=max
). Also set na.rm=T, small=T, sp=T
to 1) handle missing data along coastlines, 2) account for small countries that may not have a full 0.5 degree pixel in them, and 3) return a spatial polygon object instead of just a vector of values.sf
format with st_as_sf()
. Now you have an updated polygon object with a new column of maximium temperature. Cool!ggplot()
and geom_sf()
to plot the maximum temperature in each country polygon (not the original raster layer). To recreate the image below, you will also need +scale_fill_viridis_c(name="Annual\nMaximum\nTemperature (C)")
. Note the use of \n
to insert a line break in the text string. You can move the legend around with +theme(legend.position = 'bottom')
.dplyr
tools to find the hottest country in each continent. You may need group_by()
and top_n
. To create a nice looking table, you may also want to use select()
to keep only the desired columns, arrange()
to sort them, st_set_geometry(NULL)
to drop the geometry column (if desired).Your final result should look something like this:
And the summary table will look like this:
name_long | continent | tmax |
---|---|---|
Algeria | Africa | 48.9 |
Iran | Asia | 46.7 |
United States | North America | 44.8 |
Australia | Oceania | 41.8 |
Argentina | South America | 36.5 |
Spain | Europe | 36.1 |
French Southern and Antarctic Lands | Seven seas (open ocean) | 11.8 |
Note that these data are based on 0.5 degree resolution data and thus will miss small hot places and cannot be directly compared with station-based data.
Build a leaflet map of the same dataset.