-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
120 lines (103 loc) · 3.15 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
---
title: "AfricaCountryBins"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE,
fig.retina = 2,
fig.path = "man/figures/README-",
collapse = TRUE
)
```
Create Uniform Square Country Cartogram heatmaps
## Description
Heatmaps in a tile shape are an alternative to choropleth maps that don't distort based on land mass size.
This package is a essentially a fork of [Bob Rudis's statebins package](https://github.com/hrbrmstr/statebin) designed for use for data involving Africa.
This grid attempts to preserve the general position of countries on the contienet, while providing an equal area by country.
## Whatcha Get?
The main function implemented is
- `geom_countrybins_africa`: which is a countrybin Geom for africa
Sample dataset `africa_mines` (see `?africa_mines` for details)
## Installation
Use the following to install:
```{r eval=FALSE}
devtools::install_github("delabj/AfricaCountryBins")
# or
remotes::install_github("delabj/AfricaCountryBins")
```
To use these options you'll have to install devtools or remotes respectively.
## Examples
```{r ex-01, fig.width=600/96, fig.height=600/96}
library(AfricaCountryBins)
library(hrbrthemes)
library(tidyverse)
data("africa_mines")
count(africa_mines, countrycode, wt=count) %>%
mutate(n = ifelse(n == 0, NA, n)) %>%
ggplot(aes(country = countrycode, fill = n)) +
geom_countrybins_africa() +
coord_fixed() +
scale_fill_viridis_c(
name = "# mines (log2)",
option = "magma",
direction = -1,
na.value = "#DEE5E8",
trans = "log2",
label = scales::comma_format(1)
) +
guides(
fill = guide_colourbar(
title.position = "top"
)
) +
labs(
title = "Mines in Africa",
caption = "Source: data.world <hdata.world/datainspace/mines-in-africa>"
) +
theme_ipsum_es(grid="") +
theme(legend.position = c(0.2, 0.4)) +
theme(legend.direction = "horizontal") +
theme(legend.key.width = unit(1.5, "lines")) +
theme(axis.text.x = element_blank()) +
theme(axis.text.y = element_blank()) +
theme(plot.title = element_text(hjust = 0.5))
```
```{r ex-02, fig.width=1200/96, fig.height=1200/96}
africa_mines %>%
count(countrycode, mine_type, wt = count) %>%
mutate(mine_type = fct_reorder(mine_type, n, sum)) %>%
filter(mine_type %in% rev(levels(mine_type))[1:8]) %>%
mutate(n = ifelse(n == 0, NA, n)) %>%
ggplot(aes(country = countrycode, fill = n)) +
geom_countrybins_africa(
radius = unit(0, "pt"),
family = font_an, size = 2
) +
scale_fill_viridis_c(
name = "# mines (log2)",
option = "magma",
direction = -1,
na.value = "#DEE5E8",
trans = "log2",
label = scales::comma_format(1)
) +
coord_equal() +
facet_wrap(~mine_type) +
guides(
fill = guide_colourbar(
title.position = "top"
)
) +
labs(
title = "Mines in Africa",
caption = "Source: data.world <hdata.world/datainspace/mines-in-africa>"
) +
theme_ipsum(grid="", strip_text_face = "bold") +
theme(legend.position = c(0.85, 0.2)) +
theme(legend.direction = "vertical") +
theme(axis.text.x = element_blank()) +
theme(axis.text.y = element_blank())
```