Synopsis

A Choroplet map is printed using data from the eurostat throgh the package of the same name. In this case the “General government gross debt - annual data” with code “teina225” was chosen.

Libraries

Built under R version 3.3.2

library(knitr)
library(dplyr)
library(leaflet)
library(eurostat)
opts_chunk$set(echo = TRUE, warning = FALSE,message=FALSE)

Data

The data is download from the eurostat using the function get_eurostat. Source of Data: Eurostat General government gross debt - annual data

#First set the working directory 
setwd('~/Data_Science/R/Tasks/LeafletMap')
gross_debt <- get_eurostat("teina225", time_format = "num")

Map

To print the map the data with year 2015 and the unit “PC_GDP: Porcentage of gross domestic product (GDP)” was filter.

year= 2015
uni = "PC_GDP"
gross_debt_year <- gross_debt %>%
                  filter(time == year & unit == uni)
# merge the filter data with wgeospatial data 
gross_debt_geo <- merge_eurostat_geodata(gross_debt_year, geocolumn="geo", resolution=60,
                            output_class="spdf", all_regions=FALSE)
#define a pallete acording to the data
pal <- colorQuantile(palette = "Greens",domain = gross_debt_geo$values)
country_popup <- paste0(as.character(gross_debt_geo[["NUTS_ID"]])," ", gross_debt_geo[["values"]])
map = leaflet(data = gross_debt_geo) %>% addTiles() %>% setView(10,55,zoom = 3) %>%
  addPolygons(fillColor = ~pal(values), stroke = FALSE,
              popup = country_popup)%>%
  addLegend("bottomright", pal = pal, values = ~values,
            title = paste0(uni," ",year))
map