14.2 Build an awesome flexdadhboard
Here we build our awesome flexdadhboard with shiny using the data from the World Bank: https://datacatalog.worldbank.org/
14.2.1 Download the data
To download the data from the World Bank we need {wbstats} package.
library(wbstats)
# wbdatacatalog()
# https://datacatalog.worldbank.org/
This function helps locating the data from the database, we need to specify the language of the data.
?wb_cache
<- wb_cache("en") list
<- list$indicators indicators
Selecting PM25 we can retrieve the data used in the book.
library(tidyverse)
%>%
indicatorsfilter(str_detect(indicator_id,"PM25"))
## # A tibble: 5 × 8
## indicator_id indicator unit indic…¹ sourc…² topics sourc…³ source
## <chr> <chr> <lgl> <chr> <chr> <list> <dbl> <chr>
## 1 EN.ATM.PM25.MC.M3 PM2.5 air po… NA Popula… Brauer… <df> 2 World…
## 2 EN.ATM.PM25.MC.T1.ZS PM2.5 pollut… NA Percen… Brauer… <df> 2 World…
## 3 EN.ATM.PM25.MC.T2.ZS PM2.5 pollut… NA Percen… Brauer… <df> 2 World…
## 4 EN.ATM.PM25.MC.T3.ZS PM2.5 pollut… NA Percen… Brauer… <df> 2 World…
## 5 EN.ATM.PM25.MC.ZS PM2.5 air po… NA Percen… Brauer… <df> 2 World…
## # … with abbreviated variable names ¹indicator_desc, ²source_org, ³source_id
But, for making our flexdashboard we select other data, this data contains information about Poverty Severity Values, the amount of low wages which range from 0.00usd to 4.00usd a day, in specific countries of the South America.
# select this indicator id:
# 1.1.PSev.1.90usd
# Poverty Severity ($1.90 a day)-Rural
%>%
indicatorsfilter(str_detect(indicator,"Severity"))%>%
head(3)
## # A tibble: 3 × 8
## indicator_id indicator unit indic…¹ sourc…² topics sourc…³ source
## <chr> <chr> <lgl> <chr> <chr> <list> <dbl> <chr>
## 1 1.0.PSev.1.90usd Poverty Severit… NA The po… LAC Eq… <df> 37 LAC E…
## 2 1.0.PSev.2.5usd Poverty Severit… NA The po… LAC Eq… <df> 37 LAC E…
## 3 1.0.PSev.Poor4uds Poverty Severit… NA The po… LAC Eq… <df> 37 LAC E…
## # … with abbreviated variable names ¹indicator_desc, ²source_org, ³source_id
We can select a specific year with wb_data()
<- wb_data(indicator = "1.0.PSev.1.90usd")
p
%>%count(date) p
Then we can download the data for chosen year:
<- wb(indicator = "1.0.PSev.1.90usd",
p_14_190 startdate = 2014,
enddate = 2014)
<- wb(indicator = "1.0.PSev.2.5usd",
p_14_250 startdate = 2014,
enddate = 2014)
<- wb(indicator = "1.0.PSev.Poor4uds",
p_14_4uds startdate = 2014,
enddate = 2014)
<- dplyr::bind_rows(p_14_190,p_14_250,p_14_4uds)
p_14 %>%head p_14
## iso3c date value indicatorID indicator iso2c
## 1 ANR 2014 1.0656694 1.0.PSev.1.90usd Poverty Severity ($1.90 a day) L5
## 2 ARG 2014 0.7833897 1.0.PSev.1.90usd Poverty Severity ($1.90 a day) AR
## 3 BOL 2014 2.0466247 1.0.PSev.1.90usd Poverty Severity ($1.90 a day) BO
## 4 BRA 2014 1.1711453 1.0.PSev.1.90usd Poverty Severity ($1.90 a day) BR
## 5 MCA 2014 1.0658485 1.0.PSev.1.90usd Poverty Severity ($1.90 a day) L6
## 6 COL 2014 1.4215912 1.0.PSev.1.90usd Poverty Severity ($1.90 a day) CO
## country
## 1 Andean Region
## 2 Argentina
## 3 Bolivia
## 4 Brazil
## 5 Central America
## 6 Colombia
14.2.2 Build the map
We can use this data to build our map:
library(rnaturalearth)
<- ne_countries()
map names(map)[names(map) == "iso_a3"] <- "ISO3"
names(map)[names(map) == "name"] <- "NAME"
$PSev <- p_14[match(map$ISO3, p_14$iso3c), "value"]
map
$PSev <- round(map$PSev,4) map
library(leaflet)
<- colorBin(
pal palette = "viridis",
domain = map$PSev,
bins = seq(0, 3.5, by = 0.5)
)
leaflet(map) %>%
addTiles() %>%
setView(lng = -70, lat = -10, zoom = 3) %>%
addPolygons(stroke = TRUE,
weight = 1,
fillColor = ~ pal(PSev),
color = ~ pal(PSev),
fillOpacity = 0.2,
# label = ~labels,
highlight = highlightOptions(
color = "black",
bringToFront = TRUE
)%>%
) ::addLegend(na.label = "Na",
leafletpal = pal, values = ~PSev,
opacity = 0.5, title = "PSev"
)