-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathREADME.Rmd
208 lines (129 loc) · 6.85 KB
/
README.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
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
<!-- used devtools::build_readme() to update the md -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# afrilearndata <a href='https://github.com/afrimapr/afrilearndata'><img src='man/figures/hex.png' align="right" height="139" /></a>
<!-- badges: start -->
<!-- badges: end -->
afrilearndata provides small African spatial datasets to help with learning and teaching of spatial techniques and mapping.
The motivation is to provide analysts based in Africa with more easily relateable example datasets. More generally we aim to support the growth of R and mapping in the continent. Part of the [afrimapr](https://afrimapr.github.io/afrimapr.website/) project providing R building blocks, training and community.
## Installation
Install the development version of afrilearndata with:
```{r setup, eval=FALSE, message=FALSE, warning=FALSE}
# install.packages("remotes") # if not already installed
remotes::install_github("afrimapr/afrilearndata")
library(afrilearndata)
```
## Datasets
The package contains the following objects
1. `africontinent` polygons, continent outline including madagascar
1. `africountries` polygons, 51 country boundaries
1. `afrihighway` lines, trans African highway network (100 lines)
1. `africapitals` points, 51 capital cities
1. `afriairports` points, >3000 African airports
1. `afripop2020` raster grid, population density 2020 from [WorldPop](https://www.worldpop.org/) aggregated to 20km squares
1. `afripop2000` raster grid, population density 2000 from [WorldPop](https://www.worldpop.org/) aggregated to 20km squares
1. `afrilandcover` raster grid, landcover in 2019, categorical, 20km from [MODIS](https://lpdaac.usgs.gov/products/mcd12c1v006/)
Lazy loading means that the objects should be accessible once `library(afrilearndata)` is used.
If they are not recognised you can use e.g. `data(africountries)` to make sure the objects are loaded.
As well as providing the data as R objects the package provides them as files that can be used to demonstrate the process of reading spatial data into R and the read code is provided in the documentation of each dataset. The different datasets cover the following formats commonly used to store sptial data : geopackage, shapefile, kml, tiff, csv and grd.
Firstly, here are most of the data shown together. The `tmap` code to create this plot is shown later in the readme.
```{r tmap-all-the-data, echo=FALSE, message=FALSE, warning=FALSE}
#echo=FALSE to not show code in first version at top of readme
#do show the code later
library(afrilearndata)
# install.packages("tmap") # if not already installed
library(tmap)
# tmap_mode("view") to set to tmap interactive viewing mode
tm_shape(afripop2020) +
tm_raster(palette = rev(viridisLite::magma(5)), breaks=c(0,2,20,200,2000,25000)) +
tm_shape(africountries) +
tm_borders("white", lwd = .5) +
#tm_text("iso_a3", size = "AREA") +
tm_shape(afrihighway) +
tm_lines(col = "black") +
tm_shape(africapitals) +
tm_symbols(col = "blue", alpha=0.4, scale = .6 )+
tm_legend(show = FALSE)
```
Now looking at the data layers individually plotted with packages `sf` or `raster`
```{r countries, message=FALSE, warning=FALSE}
library(afrilearndata)
library(sf)
# polygons
plot(sf::st_geometry(africountries))
```
```{r highway, message=FALSE, warning=FALSE}
# lines
plot(sf::st_geometry(afrihighway))
```
```{r capitals, message=FALSE, warning=FALSE}
# points
plot(sf::st_geometry(africapitals))
```
Population density data are from WorldPop clipped to Africa and aggregated to 20km resolution to make them more manageable. [WorldPop](https://www.worldpop.org/) datasets are licensed under [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0/).
```{r population-grid, message=FALSE, warning=FALSE}
# raster grid
# install.packages("raster") # if not already installed
library(raster)
plot(afripop2020)
```
The `africountries` data has country names in French, Portuguese, Swahili, Afrikaans and English, that can be used to label maps as follows.
```{r french-country-names, warnings=FALSE, eval=TRUE, echo=TRUE}
library(afrilearndata)
# install.packages("tmap") # if not already installed
library(tmap)
tm_shape(africountries) +
tm_borders("grey", lwd = .5) +
tm_text("name_fr", auto.placement=FALSE, remove.overlap=FALSE, just='centre', col='red4', size=0.7 )
```
Interactive maps can be created using the `mapview` package.
```{r interactive, eval=FALSE}
# install.packages("mapview") # if not already installed
library(mapview)
mapview::mapview(africountries, zcol="name")
#here to show all airports on the continent
mapview(afriairports, zcol='type', label='name', cex=2)
```
Landcover data for the continent is provided as the majority landcover in 2019 at 20km resolution obtained from [MODIS](https://lpdaac.usgs.gov/products/mcd12c1v006/). An interactive landcover map can be displayed with `mapview`.
```{r interactive-landcover, eval=FALSE}
# install.packages("mapview") # if not already installed
library(mapview)
mapview(afrilandcover,
att="landcover",
col.regions=levels(afrilandcover)[[1]]$colour)
```
Here is a repeat of the map shown at the start of the readme, together with the code used to create it.
```{r tmap-code, warnings=FALSE, eval=TRUE, echo=TRUE}
library(afrilearndata)
# install.packages("tmap") # if not already installed
library(tmap)
# tmap_mode("view") to set to tmap interactive viewing mode
tm_shape(afripop2020) +
tm_raster(palette = rev(viridisLite::magma(5)), breaks=c(0,2,20,200,2000,25000)) +
tm_shape(africountries) +
tm_borders("white", lwd = .5) +
tm_shape(afrihighway) +
tm_lines(col = "red") +
tm_shape(africapitals) +
tm_symbols(col = "blue", alpha=0.4, scale = .6 )+
tm_legend(show = FALSE)
```
## Learning Resources
For learning resources using these data see our [afrilearnr interactive tutorials](https://github.com/afrimapr/afrilearnr), resources in English & French for a [4 hour entry level tutorial](https://github.com/afrimapr/r-maps-tutorial-fr-eng) and the in-progress [afrimapr book](https://github.com/afrimapr/afrimapr-book).
## How were the datasets created ?
Here is the [code (somewhat untidy)](https://github.com/afrimapr/afrilearndata/blob/master/data-raw/afrilearndata-data-creation.R) to create the datasets in the package.
## Related
For other and larger spatial datasets see the [spData package](https://github.com/Nowosad/spData) which was part of the inspiration for afrilearndata.
## Contributions
afrilearndata is part of [afrimapr](https://afrimapr.github.io/afrimapr.website/) we welcome
[issues and enhancement requests](https://github.com/afrimapr/afrilearndata/issues).