This is an R Markdown Notebook aimed at mapping World Development Indicators. We use data from the World Bank Development Indicators dataset. We will use Africa as our region of interest.
The first step is to load the required libraries:
library(rgdal)
library(sf)
library(sp)
library(cartography)
library(ggplot2)
library(ggiraph)
library(rnaturalearth)
library(readr)
library(RCurl)
We will use geospatial data from rnaturalearth. For interactive mapping we will use functionalities from ggplot2 and ggiraph R libraries.
The internet usage data will be retrieved online from a csv file stored at the bhaskarvk github repository. Note this file is not saved as a local file.
urlfile <- "https://raw.github.com/bhaskarvk/user2017.geodataviz/master/inst/extdata/africa-internet_usage-2015.csv"
internet_usage <- read.csv(urlfile)
head(internet_usage)
names(internet_usage) <- c("Country Name", "Country Code", "Series Name", "Series Code",
"2014 [YR2014]", "2015 [YR2015]", "2015 [YR20156]")
names(internet_usage)
## [1] "Country Name" "Country Code" "Series Name" "Series Code"
## [5] "2014 [YR2014]" "2015 [YR2015]" "2015 [YR20156]"
Note that a world countries dataset comes with the rnaturalearth library:
world <- sf::st_as_sf(rnaturalearth::countries110)
## str(world)
length(unique(world$iso_a3))
## [1] 175
africa <- dplyr::filter(world, region_un=='Africa') %>%
dplyr::left_join(internet_usage %>% dplyr::select(
`Country Code`, `2015 [YR2015]`
) %>% dplyr::rename(iso_a3=`Country Code`, internet.usage.2015=`2015 [YR2015]`),
by = 'iso_a3') %>%
st_transform(crs="+proj=laea +lon_0=18.984375")
africa.centers <- st_centroid(africa)
africa.spdf <- methods::as(africa, 'Spatial')
africa.spdf@data$id <- row.names(africa.spdf@data)
africa.tidy <- broom::tidy(africa.spdf)
Let’s create an interactive map:
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are *required* to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see http://bit.ly/arialnarrow
library(colormap)
library(widgetframe)
## Loading required package: htmlwidgets
africa.tidy <- dplyr::left_join(africa.tidy, africa.spdf@data, by='id')
g3 <- ggplot(africa.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=internet.usage.2015,
tooltip=sprintf("%s<br/>%s",iso_a3,internet.usage.2015))) +
hrbrthemes::theme_ipsum() +
colormap::scale_fill_colormap(
colormap=colormap::colormaps$copper, reverse = T) +
labs(title='Internet Usage in Africa in 2015', subtitle='As Percent of Population',
caption='Source: World Bank Open Data.')
#widgetframe::frameWidget(ggiraph(code=print(g3)))
ggiraph(code=print(g3))
First of all, we need to go to Our World in Data and download a CSV file containing all data used in The size of the poverty gap: some hints regarding the cost of ending extreme poverty. Then, we have to upload the file to our Rstudio Cloud Project. Then,we can proceed:
poverty_gap <- read.csv("./datos/poverty-gap-index.csv")
#unique(poverty_gap$Code)
length(unique(poverty_gap$Code))
## [1] 133
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
npov92 <- filter(poverty_gap, Year==1992)
npov05 <- filter(poverty_gap, Year==2005)
npov14 <- filter(poverty_gap, Year == 2014)
names(npov92) <- c("Entity", "Code", "Year", "Pov_1992")
names(npov05) <- c("Entity", "Code", "Year", "Pov_2005")
names(npov14) <- c("Entity", "Code", "Year", "Pov_2014")
class(npov14)
## [1] "data.frame"
library(sf)
nworld1 <- left_join(world, npov92, by = c('iso_a3' = 'Code'))
nworld2 <- left_join(nworld1, npov05, by = c('iso_a3' = 'Code'))
nworld <- left_join(nworld2, npov14, by = c('iso_a3' = 'Code'))
nworld %>% st_transform(crs="+proj=laea +lon_0=18.984375")
We need to replace missing values with a zero value:
(nworld$Pov_1992[is.na(nworld$Pov_1992)] <- 0)
## [1] 0
(nworld$Pov_2005[is.na(nworld$Pov_2005)] <- 0)
## [1] 0
(nworld$Pov_2014[is.na(nworld$Pov_2014)] <- 0)
## [1] 0
world.centers <- st_centroid(nworld)
world.spdf <- methods::as(nworld, 'Spatial')
world.spdf@data$id <- row.names(world.spdf@data)
world.tidy <- broom::tidy(world.spdf)
world.tidy <- dplyr::left_join(world.tidy, world.spdf@data, by='id')
africa.tidy <- dplyr::filter(world.tidy, region_un=='Africa')
library(ggplot2)
g92 <- ggplot(africa.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=(Pov_1992),
tooltip=sprintf("%s<br/>%s",iso_a3,Pov_1992))) +
hrbrthemes::theme_ipsum() +
colormap::scale_fill_colormap(
colormap=colormap::colormaps$freesurface_red, reverse = T) +
labs(title='Poverty Gap in Africa in 1992', subtitle='As Percent of Population',
caption='Source: World Bank Open Data.')
##widgetframe::frameWidget(ggiraph(code=print(g4)))
ggiraph(code=print(g92))
g02 <- ggplot(africa.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=(Pov_2005),
tooltip=sprintf("%s<br/>%s",iso_a3,Pov_2005))) +
hrbrthemes::theme_ipsum() +
colormap::scale_fill_colormap(
colormap=colormap::colormaps$freesurface_red, reverse = T) +
labs(title='Poverty Gap in Africa in 2005', subtitle='As Percent of Population',
caption='Source: World Bank Open Data.')
##widgetframe::frameWidget(ggiraph(code=print(g4)))
ggiraph(code=print(g02))
library(ggplot2)
g12 <- ggplot(africa.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=(Pov_2014),
tooltip=sprintf("%s<br/>%s",iso_a3,Pov_2014))) +
hrbrthemes::theme_ipsum() +
colormap::scale_fill_colormap(
colormap=colormap::colormaps$freesurface_red, reverse = T) +
labs(title='Poverty Gap in Africa in 2014', subtitle='As Percent of Population',
caption='Source: World Bank Open Data.')
##widgetframe::frameWidget(ggiraph(code=print(g4)))
ggiraph(code=print(g12))
A similar procedure can be used for mapping other World Development Indicators.