-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcustom-map.Rmd
76 lines (63 loc) · 1.72 KB
/
custom-map.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
---
title: "Making Custom Maps with tmap"
author: "Angela Li"
date: "5/22/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Animated Map Example
This is our ultimate goal!
```{r eval=FALSE}
library(sf)
library(tidyverse)
library(spData)
library(tmap)
m_save = world %>% filter(continent != "Antarctica") %>%
tm_shape() +
tm_polygons() +
tm_shape(urban_agglomerations) +
tm_dots(size = "population_millions", title.size = "Population (m)", alpha = 0.5, col = "red") +
tm_facets(along = "year", free.coords = FALSE)
tmap::tmap_animation(tm = m_save, filename = "/tmp/urban-animated.gif", width = 1200, height = 800)
magick::image_read("/tmp/urban-animated.gif")
```
# Load packages
```{r message=FALSE}
library(sf) # for spatial data
library(spData) # sample data I'm working with
library(tidyverse) # for data wrangling
library(tmap) # for custom maps
```
# Explore my data
```{r}
head(urban_agglomerations)
head(world)
summary(urban_agglomerations)
str(urban_agglomerations) # base R version
glimpse(urban_agglomerations) # tidyverse version - I prefer this one
class(urban_agglomerations) # tells you what type of object this is
dim(urban_agglomerations) # dimensions of data
```
# Make a ggplot2 map
```{r}
ggplot() +
geom_sf(data = urban_agglomerations)
```
```{r}
ggplot() +
geom_sf(data = world) +
geom_sf(data = urban_agglomerations)
```
# Try making the same map in tmap
```{r}
tm_shape(world) +
tm_polygons() +
tm_shape(urban_agglomerations) +
tm_dots(size = "population_millions") +
tm_compass() +
tm_scale_bar(position = c("left", "bottom")) +
tm_legend(legend.outside = TRUE) +
tm_layout(panel.show = TRUE, panel.labels = c("World population in 1950"))
```