library(tidycensus)
::census_api_key(key="xxx",
tidycensusinstall = TRUE)
Overview
For this challenge I looked at an interesting resource: Analyzing US Census Data: Methods, Maps, and Models in R, also found the {idbr}
package R Interface to the US Census Bureau International Data Base API.
Source: https://walker-data.com/census-r/working-with-census-data-outside-the-united-states.html
API key from {tidycensus}
You might need a tidycensus
API key to use the {idbr} function get_idb()
to donwload census data, then go to https://api.census.gov/data/key_signup.html and add your details. It will send you an email with the API key.
Then use your key in the tidycensus::census_api_key()
function:
tidycensus::census_api_key(key="xxx",
install = TRUE)
To check whether the key is set do:
readRenviron("~/.Renviron")
Sys.getenv("CENSUS_API_KEY")
Census Data with {idbr}
Now you are all set to get ready downloading your favorite census data.
library(idbr)
library(tidyverse)
Get data for all countries
Here the tfr
variable is selected as interested in the differences in total fertility rate in 2021 in Africa.
Get the data for all countries and then select only Africa countries.
<- get_idb(
data country = "all",
year = 2021,
variables = "tfr",
geometry = TRUE,
)
%>% head() data
%>%
data names
Have a look at the global total fertility rate in 2021.
ggplot(data, aes(fill = code)) +
theme_bw() +
geom_sf() +
coord_sf(crs = 'ESRI:54030') +
scale_fill_viridis_d()+
guides(fill=guide_legend(nrow = 10,title = ""))+
theme(legend.key.size = unit(2,units = "pt"),
legend.text = element_text(size=2),
legend.position = "bottom")
Scrap the African countries from Wikipedia:
https://en.wikipedia.org/wiki/List_of_African_countries_by_population
library(rvest)
<- read_html("https://en.wikipedia.org/wiki/List_of_African_countries_by_population")
afri_data
<- afri_data %>%
afri_countries html_nodes("table") %>%
1]] %>%
.[[html_table(fill = TRUE)
%>% names() afri_countries
<- afri_countries%>%select(Country)%>%
afri_countries unlist()
Get the Africa tfr data
<- get_idb(
africa country = afri_countries,
year = 2021,
variables = "tfr",
geometry = TRUE,
)
%>% head() africa
Check the range of the tfr:
summary(africa$tfr)
Set a color range:
<- c(2,7) col.range
Make the Map
ggplot(africa, aes(fill = tfr)) +
geom_sf(color="white") +
geom_sf_text(aes(label=name),size=2,family="Roboto Condensed")+
scale_fill_continuous(low = "#fff7ec",
high = "#7F0000",
limits=col.range)+
::geom_richtext(x=0,y=0,
ggtexthjust = 0.8,
vjust=1.5,
label="Africa",
fill = NA,
label.color = NA,
size=25,
family = "Gill Sans")+
::geom_richtext(x=0,y=0,
ggtexthjust = 0.8,
vjust=6,
label="Total fertility rate by country in 2021",
fill = NA,
label.color = NA,
size=4,
family = "Gill Sans")+
coord_sf(crs = 'ESRI:54030',clip = "off") +
labs(caption="#30DayMapChallenge 2023 - Day 8 Africa\nDataSource: wikipedia & {idbr} package | Map @fgazzelloni\n\n'Scale on map varies by more than 10%, scale bar may be inaccurate' {ggspatial}",
fill="Total fertility rate (tfr)")+
::theme_map()+
ggthemestheme(text=element_text(family = "Gill Sans"),
plot.title = element_text(face="bold",size=14,hjust=0.5),
plot.subtitle = element_text(hjust=0.5),
plot.caption = element_text(hjust=0),
legend.position = c(0.75,-0.02),
legend.key.size = unit(9,units = "pt"),
legend.text = element_text(size=7.5),
legend.title = element_text(face="bold",size=8),
legend.background = element_rect(color="#edece6",fill="#edece6"))+
::annotation_scale() ggspatial
Save it!
ggsave("day8_africa.png",bg="#ebe7d8")