-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtemperature.Rmd
More file actions
298 lines (227 loc) · 10.2 KB
/
temperature.Rmd
File metadata and controls
298 lines (227 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
title: "Temperature"
output:
html_document:
toc: true
toc_float: true
number_sections: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
#### Data set details
|||
| ----------- | ----------- |
| **Data set description:** | Temperature data worldwide and in Nigeria |
| **Source:** | [Worldclim](https://www.worldclim.org/) 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](https://www.worldclim.org/) 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](https://www.worldclim.org/data/worldclim21.html) 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 km^2^) and 10 minutes (~340km^2^). A list of [19 bioclimatic variables](https://www.worldclim.org/data/bioclim.html) 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
```{r install-raster, eval=FALSE}
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 (~340km^2^).
```{r download-maxtemp-data}
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.
```{r print-maxtemp-data}
tmax_data
```
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.
```{r convert-maxtemp-to-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.
```{r maxtemp-may-data}
tmax_data$tmax5
```
## 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.
```{r visualise-may-maxtemp}
# 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.
```{r visualise-monthly-maxtemp-example}
# 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.
```{r visualise-mean-maxtemp-example}
# 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.
```{r}
# 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](https://www.worldclim.org/data/bioclim.html)
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".
```{r warmest-month-maxtemp-example}
# 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
- `WorldClim` website: https://worldclim.org/data/index.html
- `raster` package documentation: https://cran.r-project.org/web/packages/raster/raster.pdf
---
<p style ="color:grey; font-size: 13px;">
Last updated: `r Sys.Date()`
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/temperature.Rmd
<details>
<summary><span style ="color:grey; font-size: 13px;">Tutorial was complied using: (click to expand)</span></summary>
```{r echo=FALSE}
sessionInfo()
```
</details>
</p>