Data set details

Data set description: Temperature data worldwide and in Nigeria
Source: Worldclim database
Details on the retrieved data: Mean monthly maximum temperatures worldwide and in Nigeria for the years 1970-2000.
Spatial and temporal resolution: Temperature data at spatial resolutions between 30 seconds (~1 km2) and 10 minutes (~340km2).

The WorldClim database

Worldclim is a database of global interpolated climate data for global land areas, and provides aggregated historical climate data for the years 1970-2000.

WorldClim provides monthly climate data for minimum, maximum and average temperatures, as well as precipitation, solar radiation, wind speed and water vapor pressure, at four spatial resolutions between 30 seconds (~1 km2) and 10 minutes (~340km2). A list of 19 bioclimatic variables derived as an average of the monthly values for the years 1970-2000 are also provided as more biologically meaningful variables.

The raster R package provides access to the WorldClim database, and allows us to download data sets on the many different climatic conditions provided by WorldClim.

Installing raster

The raster package can be directly installed from CRAN as follows

install.packages("raster")

Downloading maximum temperature data

We can then use getData() function of the raster package to download different climatic conditions from the Worldclim database.

The following example downloads the monthly maximum temperatures at a spatial resolution of 10 minutes (~340km2).

library(raster)
tmax_data <- getData(name = "worldclim", var = "tmax", res = 10)

Understanding the downloaded data

The downloaded monthly maximum temperature data, will be a RasterStack object - which is a collection of RasterLayer objects with the same spatial extent and resolution. This makes sense, because the monthly maximum temperature data should contain 12 RasterLayers for 12 months.

tmax_data
## 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 +no_defs 
## 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

Note how each RasterLayer corresponding to the 12 months of the year are named from tmax1 - tmax12 respectively.

The minimum and maximum values for each month however don’t seem practical. This is because the Worldclim temperature data set is stated to have a gain of 0.1, which means that it must be multiplied by 0.1 to convert back to degrees Celsius.

gain(tmax_data) <- 0.1

Thereafter, to extract the maximum temperature of a single month we can use the following approach. This example prints out the RasterLayer object corresponding to the maximum temperature in May - the fifth month of year.

tmax_data$tmax5
## class      : RasterLayer 
## dimensions : 900, 2160, 1944000  (nrow, ncol, ncell)
## resolution : 0.1666667, 0.1666667  (x, y)
## extent     : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : tmax5.bil 
## names      : tmax5 
## values     : -19, 44.1  (min, max)

Visualising the data with ggplot2

The ggplot2 package provides the geom_raster() function, which can be used to easily visualise the downloaded temperature.

The following example visualises the maximum temperature in May. Before visualising the data, we first convert the RasterLayer object to a dataframe, as follows.

# install.packages("ggplot2")
library(ggplot2)

# Converting the raster object into a dataframe
tmax_data_may_df <- as.data.frame(tmax_data$tmax5, xy = TRUE, na.rm = TRUE)
rownames(tmax_data_may_df) <- c()

ggplot(
  data = tmax_data_may_df,
  aes(x = x, y = y)
) +
  geom_raster(aes(fill = tmax5)) +
  labs(
    title = "Maximum temperature in May",
    subtitle = "For the years 1970-2000"
  ) +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_gradientn(
    name = "Temperature (°C)",
    colours = c("#0094D1", "#68C1E6", "#FEED99", "#AF3301"),
    breaks = c(-20, 0, 20, 40)
  )

Examples

Visualising monthly maximum temperatures

The following example visualises the maximum monthly temperatures for each month.

# install.packages(c("raster", "ggplot2", "tidyverse"))
library(raster)
library(ggplot2)
library(tidyverse)

# Downloading monthly maximum temperature data
tmax_data <- getData(name = "worldclim", var = "tmax", res = 10)

# Converting temperature values to Celcius
gain(tmax_data) <- 0.1

# Converting the raster object into a dataframe
tmax_data_df <- as.data.frame(tmax_data, xy = TRUE, na.rm = TRUE)
rownames(tmax_data_df) <- c()

# Renaming the month columns, Converting the dataframe into long format and converting month column into a factor
tmax_data_df_long <- tmax_data_df %>%
  rename(
    "January" = "tmax1", "February" = "tmax2", "March" = "tmax3", "April" = "tmax4",
    "May" = "tmax5", "June" = "tmax6", "July" = "tmax7", "August" = "tmax8",
    "September" = "tmax9", "October" = "tmax10", "November" = "tmax11", "December" = "tmax12"
  ) %>%
  pivot_longer(c(-x, -y), names_to = "month", values_to = "temp")

tmax_data_df_long$month <- factor(tmax_data_df_long$month, levels = month.name)

tmax_data_df_long %>%
  ggplot(aes(x = x, y = y)) +
  geom_raster(aes(fill = temp)) +
  facet_wrap(~month) +
  labs(
    title = "Monthly maximum temperatures",
    subtitle = "For the years 1970-2000"
  ) +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_gradientn(
    name = "Temperature (°C)",
    colours = c("#006D9B", "#0094D1", "#68C1E6", "#FEED99", "#AF3301"),
    breaks = c(-40, -20, 0, 20, 40)
  )

Visualising mean monthly temperature

The WorldClim database provides the monthly maximum temperatures across the world, but it might also be important to visualise an annual average of these values. This can be done by averaging the monthly maximum temperature values.

The following is a complete example of downloading monthly maximum temperature values, calculating their mean and visualising it.

# install.packages(c("raster","ggplot2", "magrittr"))
library(raster)
library(ggplot2)
library(magrittr)

# Downloading monthly maximum temperature data
tmax_data <- getData(name = "worldclim", var = "tmax", res = 10)

# Converting temperature values to Celcius
gain(tmax_data) <- 0.1

# Calculating mean of the monthly maximum temperatures
tmax_mean <- mean(tmax_data)

# Converting the raster object into a dataframe
tmax_mean_df <- as.data.frame(tmax_mean, xy = TRUE, na.rm = TRUE)

tmax_mean_df %>%
  ggplot(aes(x = x, y = y)) +
  geom_raster(aes(fill = layer)) +
  labs(
    title = "Mean monthly maximum temperatures",
    subtitle = "For the years 1970-2000"
  ) +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_gradientn(
    name = "Temperature (°C)",
    colours = c("#0094D1", "#68C1E6", "#FEED99", "#AF3301"),
    breaks = c(-20, 0, 20, 40)
  )

Visualising mean monthly temperature of Nigeria

This example uses the same approach as the previous example but only extracts and visualises the mean monthly temperature in Nigeria.

# install.packages(c("raster","ggplot2", "magrittr", "sf"))
# remotes::install_github("wmgeolab/rgeoboundaries")
library(raster)
library(ggplot2)
library(magrittr)
library(sf)
library(rgeoboundaries)


# Downloading monthly maximum temperature data
tmax_data <- getData(name = "worldclim", var = "tmax", res = 10)

# Converting temperature values to Celcius
gain(tmax_data) <- 0.1

# Calculating mean of the monthly maximum temperatures
tmax_mean <- mean(tmax_data)

# Downloading the boundary of Nigeria
nigeria_sf <- geoboundaries("Nigeria")

# Extracting temperature data of Nigeria
tmax_mean_ngeria <- raster::mask(tmax_mean, as_Spatial(nigeria_sf))

# Converting the raster object into a dataframe
tmax_mean_nigeria_df <- as.data.frame(tmax_mean_ngeria, xy = TRUE, na.rm = TRUE)

tmax_mean_nigeria_df %>%
  ggplot(aes(x = x, y = y)) +
  geom_raster(aes(fill = layer)) +
  geom_sf(data = nigeria_sf, inherit.aes = FALSE, fill = NA) +
  labs(
    title = "Mean monthly maximum temperatures in Nigeria",
    subtitle = "For the years 1970-2000"
  ) +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_gradient(
    name = "Temperature (°C)",
    low = "#FEED99",
    high = "#AF3301"
  )

Visualising bioclimatic variables

The list of 19 bioclimatic variables provided by the Worldclim database are derived from the monthly values, in order to generate more biologically meaningful variables.

The complete list of variables and their codes can be found here

The process of downloading and visualising these variables follows a similar approach to the tutorial. The following is a complete example of downloading and visualising the “Maximum Temperature of the Warmest Month”.

# install.packages(c("raster", "ggplot2", "magrittr"))
library(raster)
library(ggplot2)
library(magrittr)

# Downloading bioclimatic data
bio <- getData(name = "worldclim", var = "bio", res = 10)

# Converting temperature values to Celcius
gain(bio) <- 0.1

# Extracting only the 5th bioclimatic variable and onverting the raster object into a dataframe
bio5_df <- as.data.frame(bio$bio5, xy = TRUE, na.rm = TRUE)

bio5_df %>%
  ggplot(aes(x = x, y = y)) +
  geom_raster(aes(fill = bio5)) +
  labs(
    title = "Maximum temperature of the warmest month",
    subtitle = "In the time period 1970-2000"
  ) +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_gradientn(
    name = "Temperature (°C)",
    colours = c("#0094D1", "#68C1E6", "#FEED99", "#AF3301"),
    breaks = c(-20, 0, 20, 40)
  )

References


Last updated: 2023-01-07
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/temperature.Rmd

Tutorial was complied using: (click to expand)
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] magrittr_2.0.1            spocc_1.2.0              
##  [3] cartogram_0.2.2           wopr_0.4.6               
##  [5] ggmap_3.0.0               osmdata_0.1.5            
##  [7] malariaAtlas_1.0.1        ggthemes_4.2.4           
##  [9] here_1.0.1                MODIStsp_2.0.9           
## [11] nasapower_4.0.7           terra_1.5-17             
## [13] rnaturalearthhires_0.2.0  rnaturalearth_0.1.0      
## [15] viridis_0.5.1             viridisLite_0.3.0        
## [17] raster_3.5-15             sp_1.4-5                 
## [19] sf_1.0-7                  elevatr_0.4.2            
## [21] kableExtra_1.3.4          rdhs_0.7.2               
## [23] DT_0.17                   forcats_0.5.1            
## [25] stringr_1.4.0             dplyr_1.0.4              
## [27] purrr_0.3.4               readr_2.1.2              
## [29] tidyr_1.1.4               tibble_3.1.6             
## [31] tidyverse_1.3.1           openair_2.9-1            
## [33] leaflet_2.1.1             ggplot2_3.3.5            
## [35] rgeoboundaries_0.0.0.9000
## 
## loaded via a namespace (and not attached):
##   [1] uuid_0.1-4              readxl_1.3.1            backports_1.4.1        
##   [4] systemfonts_1.0.4       plyr_1.8.7              selectr_0.4-2          
##   [7] lazyeval_0.2.2          splines_4.0.3           storr_1.2.5            
##  [10] crosstalk_1.2.0         urltools_1.7.3          digest_0.6.27          
##  [13] htmltools_0.5.2         fansi_0.4.2             memoise_2.0.1          
##  [16] cluster_2.1.0           gdalUtilities_1.2.1     tzdb_0.3.0             
##  [19] modelr_0.1.8            vroom_1.5.7             xts_0.12.1             
##  [22] svglite_1.2.3.2         prettyunits_1.1.1       jpeg_0.1-9             
##  [25] colorspace_2.0-3        rvest_1.0.2             rappdirs_0.3.3         
##  [28] hoardr_0.5.2            haven_2.5.0             xfun_0.30              
##  [31] rgdal_1.5-23            crayon_1.5.1            jsonlite_1.8.0         
##  [34] hexbin_1.28.2           progressr_0.10.1        zoo_1.8-8              
##  [37] countrycode_1.2.0       glue_1.6.2              gtable_0.3.0           
##  [40] webshot_0.5.2           maps_3.4.0              scales_1.1.1           
##  [43] oai_0.3.2               DBI_1.1.2               Rcpp_1.0.7             
##  [46] progress_1.2.2          units_0.8-0             bit_4.0.4              
##  [49] mapproj_1.2.8           rgbif_3.5.2             htmlwidgets_1.5.4      
##  [52] httr_1.4.2              RColorBrewer_1.1-2      wk_0.5.0               
##  [55] ellipsis_0.3.2          pkgconfig_2.0.3         farver_2.1.0           
##  [58] sass_0.4.0              dbplyr_2.1.1            conditionz_0.1.0       
##  [61] utf8_1.1.4              crul_1.2.0              tidyselect_1.1.0       
##  [64] labeling_0.4.2          rlang_1.0.2             munsell_0.5.0          
##  [67] cellranger_1.1.0        tools_4.0.3             cachem_1.0.6           
##  [70] cli_3.2.0               generics_0.1.2          broom_0.8.0            
##  [73] evaluate_0.15           fastmap_1.1.0           yaml_2.2.1             
##  [76] knitr_1.33              bit64_4.0.5             fs_1.5.2               
##  [79] s2_1.0.7                RgoogleMaps_1.4.5.3     nlme_3.1-149           
##  [82] whisker_0.4             wellknown_0.7.2         xml2_1.3.2             
##  [85] compiler_4.0.3          rstudioapi_0.13         curl_4.3.2             
##  [88] png_0.1-7               e1071_1.7-4             reprex_2.0.1           
##  [91] bslib_0.3.1             stringi_1.5.3           highr_0.9              
##  [94] gdtools_0.2.4           lattice_0.20-41         Matrix_1.2-18          
##  [97] classInt_0.4-3          vctrs_0.3.8             slippymath_0.3.1       
## [100] pillar_1.7.0            lifecycle_1.0.1         triebeard_0.3.0        
## [103] jquerylib_0.1.4         data.table_1.14.2       bitops_1.0-7           
## [106] R6_2.5.0                latticeExtra_0.6-29     KernSmooth_2.23-17     
## [109] gridExtra_2.3           codetools_0.2-16        MASS_7.3-53            
## [112] assertthat_0.2.1        rjson_0.2.20            rprojroot_2.0.2        
## [115] withr_2.5.0             httpcode_0.3.0          rbison_1.0.0           
## [118] rvertnet_0.8.0          ridigbio_0.3.5          mgcv_1.8-33            
## [121] parallel_4.0.3          hms_1.1.1               rebird_1.2.0           
## [124] grid_4.0.3              rnaturalearthdata_0.1.0 class_7.3-17           
## [127] rmarkdown_2.11          packcircles_0.3.4       lubridate_1.8.0

Corrections: If you see mistakes or want to suggest additions or modifications, please create an issue on the source repository or submit a pull request
Reuse: Text and figures are licensed under Creative Commons Attribution CC BY 4.0.