-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-climate-data.Rmd
125 lines (87 loc) · 3.46 KB
/
04-climate-data.Rmd
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
# Working with Raster Data
## Introduction
## Exercises
### Download Climate Data from Worldclim
_raster_ has functionality built-in to download files and import them automatically to your R session for you.
We will use this functionality to get Worldclim data [@worldclim] for minimum temperature.
```{r wc-download, eval=TRUE}
library(raster)
tmin <- getData("worldclim", var = "tmin", res = 10)
summary(tmin)
```
We can see that the `tmin` object is a _raster_ stack.
In other words, this stack has several layers of data, one for each month plus the annual average minimum temperature.
### Convert Temperature Data
Worldclim temperature data need to be divided by 10 to convert back to degrees Celsius.
This will convert all of the layers.
```{r convert-to-C}
tmin <- tmin/10
```
### Create a Data Set for January
We can create a new object for just the minimum temperature in January.
Using the `[[]]` is called indexing, it tells R to take the first object in the stack.
```{r create-jan-raster}
tmin1 <- tmin[[1]]
class(tmin1)
```
### Create Data Frame for ggplot2
However, the data are in a format that _ggplot2_ still will not understand, a `raster` object.
We need to convert it to a data frame.
_raster_ has a built-in function to do this for us.
```{r create-jan-df}
tmin1_df <- as.data.frame(tmin1, xy = TRUE)
class(tmin1_df)
```
Now we we are ready to plot the data in _ggplot2_.
### Plot Global Minimum Temperatures
```{r plot-tmin1.1, message=FALSE}
ggplot() +
geom_raster(data = tmin1_df, aes(x = x, y = y, fill = tmin1)) +
scale_fill_viridis_c("Min\nT", na.value = NA) +
coord_quickmap()
```
### Brazil Minimum Temperature
If we wish to plot only temperatures for Brazil, we need to perform some GIS operations with the _raster_ object, a bit different than the _sf_ in previous examples.
Work is being done to make raster files in R work as seamlessly, but for right now it is not possible yet.
The first step is to crop the raster file, removing all data that is not Brazil.
We will use the `br_sf` object to instruct _raster_ where our data begins and ends.
Using the base `plot()` method, quickly explore the new object, `brazil` that is created.
#### Crop Operation
```{r crop}
brazil <- crop(tmin1, br_sf)
plot(brazil)
```
#### Mask Operation
The `brazil` object is not only Brazil but also bits of other countries nearby.
To fix this we will now use `mask()` from the _raster_ package to remove data that are outside the outline of Brazil.
```{r mask}
brazil <- mask(brazil, br_sf)
plot(brazil)
```
Now that we have the data we desire, we can convert it into a `data.frame` suitable for use in _ggplot2_.
```{r brazil-tmin1-df}
brazil <- as.data.frame(brazil, xy = TRUE)
```
### Plot Brazil Minimum Temperature
```{r ggplot-brazil-tmin1-df}
br_tmin <-
ggplot() +
geom_raster(data = brazil, aes(x = x, y = y, fill = tmin1)) +
scale_fill_viridis_c("Min\nT (°C)", na.value = NA) +
xlab("Longitude") +
ylab("Latitude") +
coord_quickmap()
```
### Adding State Outlines
We can add the state outlines on top of the minimum temperatures as a new layer.
Using the `br_sf` object and `geom_sf()` will add state outlines. However, `fill` must be set to `NA` or the polygons will be filled with default grey.
```{r tmin-states, message=FALSE, warning=FALSE}
br_tmin <-
br_tmin +
geom_sf(data = br_sf,
fill = NA,
colour = "black") +
coord_sf()
```
## Your Turn {.exercise}
* Create a map of Minas Gerais' minimum temperature for December.