-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-country_state_maps.Rmd
136 lines (94 loc) · 3.22 KB
/
02-country_state_maps.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
126
127
128
129
130
131
132
133
134
135
136
# Creating Country and State Level Maps
## Introduction
In this first exercise we will use [_rnaturalearth_](https://github.com/ropenscilabs/rnaturalearth), [_simple features_](https://cran.r-project.org/web/packages/sf/vignettes/sf1.html) and
_ggplot2_ to create a map of Brazil and states within Brazil to use as the base of the maps we will create in these exercises.
## Exercises
### Create Country Level Map of Brazil
This is our base layer, Brazil, of the map from (Naturalearth.com)[https://naturalearth.com/] using the base `plot()` to display these data.
Using `ne_states()` will return an `sf` object with state level information. There is also a `ne_countries()` to download country level outlines only as well.
```{r brazil-base}
library(rnaturalearth)
br_sf <- ne_states(geounit = "brazil",
returnclass = "sf")
plot(br_sf)
```
#### Plot using `ggplot2`
Using _ggplot2_, we have more control over how the data are displayed.
First plot the whole country using `ggplot()`.
```{r plot_level1, eval=TRUE}
library(ggplot2)
br <- ggplot(br_sf) +
geom_sf(fill = "white")
br
```
#### Add Labels to the States
The naturalearth data have several columns to work with. One of them is the abbreviation for the states. Using `geom_text()`, we can add this information to the map.
```{r plot_level2}
br <-
br +
geom_text(
data = br_sf,
aes(x = longitude,
y = latitude,
label = abbrev),
size = 2.5,
hjust = 1
)
br
```
#### Final Touches
Properly label the x and y-axis and set the theme. The _ggplot2_ theme, `theme_bw()` is nice to use with maps, so we will apply that here as well.
```{r plot_level3}
br <-
br +
xlab("Longitude") +
ylab("Latitude") +
theme_bw()
br
```
### Create Maps of Individual States
To create a map of only Minas Gerais, subset the _sf_ object, `br` such that it contains only the data for Minas Gerais.
#### Inspect Column Names in `br_sf` Object
To find out what the column names are, we can use, `colnames()`.
```{r colnames}
names(br_sf)
```
We see that a column named "name_pt" exists, with the Portuguese name spelling for each state in this data set.
We can use this to `filter()` the data so that it only represents Minas Gerais and we can use that to make a map.
For more on subsetting _sf_ objects, see: <https://cran.rstudio.com/web/packages/sf/vignettes/sf4.html>.
#### Filter Minas Gerais
```{r subset-mg}
library(dplyr)
mg <- filter(br_sf, name_pt == "Minas Gerais")
mg
```
#### Plot Minas Gerais
```{r ggplot-mg}
ggplot(data = mg) +
geom_sf() +
xlab("Longitude") +
ylab("Latitude") +
theme_bw()
```
### Highlighting States Within the Country
Using the `br_sf` and `mg` objects that we have created, it is possible to create a map that highlights one state and labels it using the abbreviation, in this case Minas Gerais.
```{r hightlight-mg}
ggplot(data = br_sf) +
geom_sf(fill = "white") +
geom_sf(
data = mg,
fill = "red") +
geom_text(
data = mg,
aes(x = longitude,
y = latitude,
label = abbrev),
size = 2.5,
hjust = 1) +
xlab("Longitude") +
ylab("Latitude") +
theme_bw()
```
## Your Turn {.exercise}
Using the examples above, download and create a map of Australia.
Then highlight Bahia.