-
Notifications
You must be signed in to change notification settings - Fork 26
/
municipios.Rmd
87 lines (75 loc) · 2.92 KB
/
municipios.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
---
title: "Municipios"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.dim = c(6, 4.6),
dpi = 200,
out.width = '100%')
```
```{r message=FALSE,warning=FALSE}
library(mxmaps)
library(viridis)
library(scales)
df_mxmunicipio_2020$value <- df_mxmunicipio_2020$indigenous_language /
df_mxmunicipio_2020$pop
gg = MXMunicipioChoropleth$new(df_mxmunicipio_2020)
gg$title <- "Percentage of the population that speaks\nan indigenous language"
gg$set_num_colors(1)
gg$ggplot_scale <- scale_fill_viridis("percent",
labels = percent)
gg$render()
```
Another example with categorical data
```{r message=FALSE,warning=FALSE}
library("ggplot2")
df_mxmunicipio_2020$value <- as.factor(sample(c(NA, letters[1:6]),
nrow(df_mxmunicipio_2020),
replace = TRUE) )
gg = MXMunicipioChoropleth$new(df_mxmunicipio_2020)
gg$title <- "Municipios a-f"
gg$set_num_colors(6)
gg$set_zoom(subset(df_mxmunicipio_2020, state_name %in% c("Ciudad de México",
"México"))$region)
gg$ggplot_scale <- scale_fill_brewer("type", type = "qual", palette = 2,
na.value = "gray")
p <- gg$render()
p + theme_void()
```
You can also edit the ggplot object directly, for example, if you wanted to remove the municipio borders.
```{r message=FALSE,warning=FALSE}
library("scales")
df_mxmunicipio_2020$value <- df_mxmunicipio_2020$afromexican /
df_mxmunicipio_2020$pop
p <- mxmunicipio_choropleth(df_mxmunicipio_2020,
title = "Percentage of population that identifies as Afromexican",
legend = "percent\nAfromexican",
num_colors = 1)
p[["layers"]][[1]][["aes_params"]][["colour"]] <- "transparent"
p
```
We can also label the map with the names of important municipios (in this case those municipios in Chihuahua with more than 100,000 persons)
```{r}
library("ggrepel")
df_mxmunicipio_2020$value <- df_mxmunicipio_2020$indigenous_language /
df_mxmunicipio_2020$pop * 100
chih <- subset(df_mxmunicipio_2020, state_name %in% c("Chihuahua"))
p <- mxmunicipio_choropleth(df_mxmunicipio_2020, num_colors = 1,
zoom = chih$region,
title = "Percentage of the population that self-identifies\nas indigenous in Chihuahua",
show_states = FALSE,
legend = "%")
labels <- chih
labels$group <- NA
labels <- subset(labels, pop > 1e05)
p +
geom_text_repel(data = labels,
aes(long, lat, label = municipio_name),
nudge_x = .1,
nudge_y = .7) +
geom_point(data = labels,
aes(long, lat),
color = "#d6604d",
size = 1)
```