-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-adding_points.Rmd
105 lines (79 loc) · 3.07 KB
/
03-adding_points.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
# Adding Points
## Introduction
In many cases we may have a data set of interest that has georeferenced data (lat, lon values).
Using the soybean rust observation data, add the points to the map of Brazil.
## Exercises
### Import Soybean Rust Observations
We will load the _tidyverse_ package for these next steps.
```{r import-sbr-data, message=FALSE}
library(tidyverse)
sbr <- read_csv("SBR-BR.csv")
glimpse(sbr)
```
### Add Soybean Rust Observations to Our ggplot2 Object
Using `geom_point()` we can add points from the CSV file of observation data to the map of Brazil that we have already created.
```{r add-points-map}
br +
geom_point(data = sbr, aes(x = lon, y = lat))
```
That is not too clear. Perhaps it would be better to just plot the two states, rather than the whole country.
Filter the `br_sf` object to create a new object of only these two states.
```{r plot-sbr-states1}
rgs_pa <-
filter(br_sf, name_pt == "Rio Grande do Sul" |
name_pt == "Paraná")
rgs_pa
```
Using the new object with only Rio Grande do Sul and Paraná, create a new map with the soybean rust observations.
Note the use of the data call in both `geom_sf()` and `geom_point()` so that it is possible to mix different data sources in the same map.
```{r plot-sbr-states2}
ggplot() +
geom_sf(data = rgs_pa, fill = "white") +
geom_point(data = sbr, aes(x = lon, y = lat)) +
xlab("Longitude") +
ylab("Latitude") +
theme_bw()
```
To show more information about the soybean rust outbreaks, we can use colours in the points.
```{r plot-sbr-states3}
ggplot() +
geom_sf(data = rgs_pa, fill = "white") +
geom_point(data = sbr, aes(x = lon, y = lat, colour = month)) +
scale_color_viridis_d("Report") +
xlab("Longitude") +
ylab("Latitude") +
theme_bw()
```
That is more useful, but we have multiple years and the months are not in order.
To correct this, we will modify the `sbr` object and make the "month" column a factor and reorder it to follow the soybean growing season.
```{r sbr-year-month}
sbr <-
sbr %>%
mutate(month = as_factor(month)) %>%
mutate(month = fct_relevel(month, c("Nov", "Dec", "Jan", "Feb", "Mar")))
```
With the new data format, now plot the data.
```{r plot-sbr-states4}
ggplot() +
geom_sf(data = rgs_pa, fill = "white") +
geom_point(data = sbr, aes(x = lon, y = lat, colour = month)) +
scale_color_viridis_d("Report") +
xlab("Longitude") +
ylab("Latitude") +
theme_bw()
```
Now plotting the data again shows that the months are in the proper order but there are multiple seasons.
We can use faceting to make the maps more clear and see patterns.
```{r plot-sbr-states5, message=FALSE}
ggplot() +
geom_sf(data = rgs_pa, fill = "white") +
geom_point(data = sbr, aes(x = lon, y = lat, colour = month)) +
scale_color_viridis_d("Report") +
xlab("Longitude") +
ylab("Latitude") +
theme_bw() +
facet_wrap(. ~ season)
```
## Your Turn {.exercise}
* Create a figure such that each state/year combination is represented in its own facet. Why might this not be idea?
* Create individual figures for each state faceted by year with month as the point colour.