Background:

We are interested in finding the hottest country on each continent. To approach this task we will use the World Climate data from: WorldClim (http://worldclim.org) which gives us a timeseries of temperature data available at various resolutions. This raster data format is commonly used for datasets such as the timeseries of temperature data. We will extract maximum temperature data for each country by intersecting a set of polygons with a raster image. Maximum temperatures will be calculated from the maximum raster value in each polygon.

Objective:

Identify the hottest country on each continent (not counting Antarctica or the seven seas) by intersecting a set of polygons with a raster image and calculating the maximum raster value in each polygon.

Data/Methods:

Load Packages:

library(raster)
library(sf)
library(sp)
library(spData)
library(tidyverse)

Prepare Country Polygon Data:

data(world)
plot(world)

1a. Remove “Antarctica”

WorldClim does not have temperature data for Antarctica, so we need to remove it from the world data
I also removed the seven seas from the world dataset as we are only interested in land temperatures for this analysis.
worldClim<- world %>% 
  dplyr::filter(continent!="Antarctica") %>% 
  dplyr::filter(continent!="Seven seas (open ocean)") 

1b. Convert the world object to sp format

The raster package does not accept sf objects, so we need to change it to the sp format using the following line of code:
world1<-as(worldClim,"Spatial") #allows us to do raster-vector interactions, creating sp object

Prepare Climate Data:

2a. Download the WorldClim maximum temperature data

var=“tmax” gives us the maximum temperature, and res=10 gives us the lowest resolution (10 degrees Celsius).
tmax_monthly<-getData(name="worldclim", var="tmax", res=10)

2b. Inspect the new tmax_monthly object

The tmax_monthly object has 12 layers representing the average maximum temperature for each month of the year. They are in degrees celsius, however, we will need to divide the number by 10 to convert it to degrees celsius according to the data website metadata.
tmax_monthly
## class      : RasterStack 
## dimensions : 900, 2160, 1944000, 12  (nrow, ncol, ncell, nlayers)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## names      : tmax1, tmax2, tmax3, tmax4, tmax5, tmax6, tmax7, tmax8, tmax9, tmax10, tmax11, tmax12 
## min values :  -478,  -421,  -422,  -335,  -190,   -94,   -59,   -76,  -153,   -265,   -373,   -452 
## max values :   418,   414,   420,   429,   441,   467,   489,   474,   441,    401,    401,    413

2c. Convert to degrees Celsius

(See explaination above) The gain ( ) function is used to multiply the tmax_monthly object by 0.1 to convert to degrees celsius (as per the metadata instruction).
gain(tmax_monthly)=0.1
plot(tmax_monthly)

2d. Create a new object that is the annual maximum temperature.

tmax_annual<-max(tmax_monthly)

2e. Change the name of the layer in the new “tmax_annual” object to “tmax”

The default name is layer, so we want something more intuitive, hence “tmax”, this line of code changes the name of the layer.
names(tmax_annual)<-"tmax"

Maximum temperature observed in each country:

3a. This next line of code

(i) handles missing data along coastlines (na.rm=)
(ii) accounts for small countries that may not have a fully 0.5 degree pixel (small=) and
(iii) returns a spatial polygon object instead of just a vector of values (sp=).
monthly_tmax<-raster::extract(x=tmax_annual, y=world1, fun=max, na.rm=TRUE, small=TRUE, sp=TRUE)

3b.Convert the format of monthly_tmax to sf format

This code will give us an updated polygon object with a new column with the maximum temperature values.
worldSF<-st_as_sf(monthly_tmax)

Results:

4a. The following ggplot code uses the data from the object created in the last step to plot the maximum annual temperature for each country.

ggplot(data=worldSF)+
  geom_sf(aes(fill=tmax))+
  scale_fill_viridis_c(name="Annual\nMaximum\nTemperature (C)")+
  theme_classic()+
  theme(legend.position="bottom")+
  labs(title="The Annual Maximum Temperature\nfor Each Country")+ 
  xlab("Latitude")+
  ylab("Longitude")+
  theme(plot.title = element_text(hjust=0.5))

4b. These lines of code create an object that groups the worldSF data by continent and then arranges the data by the annnual maximum temperature values. For the final table I selected only the relevant columns and used the slice function to filter the top country on each continent.

hottest<-worldSF %>% 
  dplyr::group_by(continent) %>% 
  dplyr::arrange(desc(tmax)) %>% 
  dplyr::select(continent, name_long, tmax) %>% 
  dplyr::slice(1) %>% 
  st_set_geometry(NULL)

4b. Generate a table from the tibble created above:

knitr::kable(hottest)
continent name_long tmax
Africa Algeria 48.9
Asia Iran 46.7
Europe Spain 36.1
North America United States 44.8
Oceania Australia 41.8
South America Argentina 36.5

Conclusions:

The hottest countries on each continent are listed in the table above. Africa contains the hottest country in the world, with a annual temperature maximum of 48.9 degrees Cesius. Whereas Argentina had the coolest “hottest” country with an annual temperature maximum of 36.5 degrees Cesius. Over a 10 degree difference between the two!