Create Uniform Square Country Cartogram heatmaps
Heatmaps in a tile shape are an alternative to choropleth maps that don’t distort perception of results based on land mass size.
This package is a essentially a fork of Bob Rudis’s statebins package designed for use for data involving Africa.
This grid attempts to preserve the general position of countries on the continent, while providing an equal area by country.
The main function implemented is - geom_countrybins_africa
: which is a
countrybin Geom for africa
Use the following to install:
devtools::install_github("delabj/AfricaCountryBins")
# or
remotes::install_github("delabj/AfricaCountryBins")
To use these options you’ll have to install devtools or remotes respectively.
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))
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())