The API can return geojson objects, which can be very useful for quickly creating maps of the DHS indicator data. These are large objects though, and can cause the API to return slowly if too much data is returned.
In this demonstration we will be using 2 other packages, leaflet
and geojson
.
# load our package
library(rdhs)
# install other packages
# install.packages("geojson")
# install.packages("leaflet")
# make request
<- dhs_data(countryIds = "SN",
d indicatorIds = "FE_FRTR_W_A15",
surveyYearStart = 2014,
breakdown = "subnational",
returnGeometry = TRUE,
f = "geojson")
# convert to sp
<- geojsonio::as.json(d)
m <- geojsonio::geojson_sp(m)
nc
# plot using leaflet
<- leaflet::colorNumeric("viridis", NULL)
pal
::leaflet(nc[nc$IndicatorId=="FE_FRTR_W_A15",]) %>%
leaflet::addTiles() %>%
leaflet::addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
leafletfillColor = ~pal(log10(Value)),
label = ~paste0(CharacteristicLabel, ": ", formatC(Value, big.mark = ","))) %>%
::addLegend(pal = pal, values = ~log10(Value), opacity = 1.0,
leafletlabFormat = leaflet::labelFormat(transform = function(x) round(10^x)),title = ~Indicator[1])