/
11-advanced-mapping.Rmd
79 lines (66 loc) · 1.96 KB
/
11-advanced-mapping.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
# Advanced map making
## Prerequisites {-}
- ...
```{r, include=FALSE}
pkgs_c11 = c(
"cowplot",
"wbstats",
"rnaturalearth",
"viridis",
"sf",
"tidyverse",
"tmap",
"units"
)
pacman::p_load(char = pkgs_c11)
if(!exists("world")) {
source("code/01-introduction.R")
}
```
# Advanced map making {#adv-map}
<!--
- tmap
- ggplot2/ggmap (geom_sf, coord_sf)
- leaflet/mapview
- rasterVis ?
- shiny ?
- animations
- inset maps
- color palettes (viridis, etc.)
-->
## Animations
```{r ani_data_prep}
options(scipen = 999)
wb_data_create = function(indicator, our_name, year, ...){
df = wb(indicator = indicator, startdate = year, enddate = year, ...) %>%
as_data_frame() %>%
select(iso_a2=iso2c, value) %>%
mutate(indicator = our_name) %>%
spread(indicator, value)
return(df)
}
data_lifeExp = seq(1963, 2013, by=5) %>%
set_names(.) %>%
map_df(~wb_data_create(.x, indicator = "SP.DYN.LE00.IN",
our_name = "lifeExp",
country = "countries_only"), .id='year') %>%
spread(year, lifeExp)
world_sf_temporal = ne_countries(returnclass = 'sf') %>%
left_join(., data_lifeExp, by = c('iso_a2')) %>%
mutate(area_km2 = set_units(st_area(.), km^2)) %>%
select(iso_a2, name_long, continent, region_un, subregion, type, area_km2, `1963`:`2013`) %>%
gather(year, lifeExp, `1963`:`2013`)
```
```{r animation1, eval=FALSE}
m1 = tm_shape(world_sf_temporal) +
tm_polygons("lifeExp") +
tm_facets(by = "year", nrow = 1, ncol = 1, drop.units = TRUE)
animation_tmap(m1, filename = "figures/11-lifeExp_animation.gif", width = 2000, height = 600, delay = 40)
```
```{r animation2, eval=FALSE}
world_sf_temporal2 = filter(world_sf_temporal, continent == "South America")
m2 = tm_shape(world_sf_temporal2) +
tm_polygons("lifeExp", n = 12) +
tm_facets(by = "name_long", along = "year", drop.units = TRUE, free.coords = TRUE)
animation_tmap(m2, filename = "figures/11-lifeExp_sa_animation.gif", width = 1600, height = 1000, delay = 40)
```