R Tools for Eurostat Open Data: maps

This rOpenGov R package provides tools to access Eurostat database, which you can also browse on-line for the data sets and documentation. For contact information and source code, see the package website.

See eurostat vignette for installation and basic use.

Maps

NOTE: we recommend to check also the giscoR package (https://dieghernan.github.io/giscoR/). This is another API package that provides R tools for Eurostat geographic data to support geospatial analysis and visualization.

Disposable income of private households by NUTS 2 regions at 1:60mln resolution using tmap

The mapping examples below use tmap package.

## 
## 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
## Linking to GEOS 3.8.1, GDAL 3.1.3, PROJ 7.1.0
library(tmap)

# Download attribute data from Eurostat
sp_data <- eurostat::get_eurostat("tgs00026",
                          time_format = "raw") %>% 
  # subset to have only a single row per geo
  dplyr::filter(time == 2010, nchar(geo) == 4) %>% 
  # categorise
  dplyr::mutate(income = cut_to_classes(values, n = 5))
## Table tgs00026 cached at /tmp/RtmpSJsNom/eurostat/tgs00026_raw_code_FF.rds
# Download geospatial data from GISCO
geodata <- get_eurostat_geospatial(output_class = "sf",
                                   resolution = "60",
                   nuts_level = 2,
                   year = 2013)
## No encoding supplied: defaulting to UTF-8.
## sf at resolution 1: 60  cached at:  /tmp/RtmpSJsNom/eurostat/sf60220134326.RData
# merge with attribute data with geodata
map_data <- inner_join(geodata, sp_data)
## Joining, by = "geo"

Construct the map

map1 <- tmap::tm_shape(geodata) +
  tmap::tm_fill("lightgrey") +
  tmap::tm_shape(map_data) +
  tmap::tm_grid() +
  tmap::tm_polygons("income", title = "Disposable household\nincomes in 2010",  
                    palette = "Oranges")
print(map1)  

Interactive maps can be generated as well

# Interactive
tmap_mode("view")
map1

# Set the mode back to normal plotting
tmap_mode("plot")
print(map1)

Disposable income of private households by NUTS 2 regions in Poland with labels at 1:1mln resolution using tmap

library(eurostat)
library(dplyr)
library(sf)
library(RColorBrewer)

# Downloading and manipulating the tabular data
print("Let us focus on year 2014 and NUTS-3 level")
## [1] "Let us focus on year 2014 and NUTS-3 level"
euro_sf2 <- get_eurostat("tgs00026", time_format = "raw",
             filter = list(time = "2014")) %>% 
 
  # Subset to NUTS-3 level
  dplyr::filter(grepl("PL",geo)) %>% 
  # label the single geo column
  mutate(label = paste0(label_eurostat(.)[["geo"]], "\n", values, "€"),
         income = cut_to_classes(values))

print("Download geospatial data from GISCO")
## [1] "Download geospatial data from GISCO"
geodata <- get_eurostat_geospatial(output_class = "sf", resolution = "60", nuts_level = 2, year = 2013)
## Reading cache file /tmp/RtmpSJsNom/eurostat/sf60220134326.RData
## sf at resolution 1: 60  from year  2013  read from cache file:  /tmp/RtmpSJsNom/eurostat/sf60220134326.RData
# Merge with attribute data with geodata
map_data <- inner_join(geodata, euro_sf2)
## Joining, by = "geo"
# plot map
map2 <- tm_shape(geodata) +
  tm_fill("lightgrey") +
  tm_shape(map_data, is.master = TRUE) +
  tm_polygons("income", title = "Disposable household incomes in 2014",
              palette = "Oranges", border.col = "white") + 
  tm_text("NUTS_NAME", just = "center") + 
  tm_scale_bar()
map2

Disposable income of private households by NUTS 2 regions at 1:10mln resolution using spplot

library(sp)
library(eurostat)
library(dplyr)
library(RColorBrewer)
dat <- get_eurostat("tgs00026", time_format = "raw") %>% 
  # subsetting to year 2014 and NUTS-2 level
  dplyr::filter(time == 2014, nchar(geo) == 4) %>% 
  # classifying the values the variable
  dplyr::mutate(cat = cut_to_classes(values))

# Download geospatial data from GISCO
geodata <- get_eurostat_geospatial(output_class = "spdf", resolution = "10", nuts_level = 2, year = 2013)

# merge with attribute data with geodata
geodata@data <- left_join(geodata@data, dat)

# plot map
sp::spplot(obj = geodata, "cat", main = "Disposable household income",
       xlim = c(-22,34), ylim = c(35,70), 
           col.regions = c("dim grey", brewer.pal(n = 5, name = "Oranges")),
       col = "white", usePolypath = FALSE)

Disposable income of private households by NUTS 2 regions at 1:60mln resolution using ggplot2

Meanwhile the CRAN version of ggplot2 is lacking support for simple features, you can plot maps with ggplot2 by downloading geospatial data as data.frame with output_class argument set as df.

library(eurostat)
library(dplyr)
library(ggplot2)
dat <- get_eurostat("tgs00026", time_format = "raw") %>% 
  # subsetting to year 2014 and NUTS-2 level
  dplyr::filter(time == 2014, nchar(geo) == 4) %>% 
  # classifying the values the variable
  dplyr::mutate(cat = cut_to_classes(values))
## Reading cache file /tmp/RtmpSJsNom/eurostat/tgs00026_raw_code_FF.rds
## Table  tgs00026  read from cache file:  /tmp/RtmpSJsNom/eurostat/tgs00026_raw_code_FF.rds
# Download geospatial data from GISCO
geodata <- get_eurostat_geospatial(resolution = "60", nuts_level = "2", year = 2013)
## Reading cache file /tmp/RtmpSJsNom/eurostat/sf60220134326.RData
## sf at resolution 1: 60  from year  2013  read from cache file:  /tmp/RtmpSJsNom/eurostat/sf60220134326.RData
# merge with attribute data with geodata
map_data <- inner_join(geodata, dat)
## Joining, by = "geo"
ggplot(data=map_data) + geom_sf(aes(fill=cat),color="dim grey", size=.1) + 
    scale_fill_brewer(palette = "Oranges") +
  guides(fill = guide_legend(reverse=T, title = "euro")) +
  labs(title="Disposable household income in 2014",
       caption="(C) EuroGeographics for the administrative boundaries 
                Map produced in R with a help from Eurostat-package <github.com/ropengov/eurostat/>") +
  theme_light() + theme(legend.position=c(.8,.8)) +
  coord_sf(xlim=c(-12,44), ylim=c(35,70))

Version info

This tutorial was created with

## R version 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.10
## 
## Matrix products: default
## BLAS:   /home/lemila/bin/R-4.0.3/lib/libRblas.so
## LAPACK: /home/lemila/bin/R-4.0.3/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_3.3.3      RColorBrewer_1.1-2 tmap_3.3           sf_0.9-7          
## [5] dplyr_1.0.3        eurostat_3.7.1    
## 
## loaded via a namespace (and not attached):
##  [1] httr_1.4.2         tidyr_1.1.2        jsonlite_1.7.2     viridisLite_0.3.0 
##  [5] assertthat_0.2.1   countrycode_1.2.0  highr_0.8          sp_1.4-5          
##  [9] yaml_2.2.1         pillar_1.4.7       backports_1.2.1    lattice_0.20-41   
## [13] glue_1.4.2         digest_0.6.27      RefManageR_1.3.0   colorspace_2.0-0  
## [17] htmltools_0.5.1.1  plyr_1.8.6         XML_3.99-0.5       pkgconfig_2.0.3   
## [21] broom_0.7.4        raster_3.4-5       stars_0.5-1        purrr_0.3.4       
## [25] scales_1.1.1       tibble_3.0.6       farver_2.0.3       generics_0.1.0    
## [29] ellipsis_0.3.1     withr_2.4.1        cachem_1.0.1       leafsync_0.1.0    
## [33] magrittr_2.0.1     crayon_1.3.4       mime_0.9           memoise_2.0.0     
## [37] evaluate_0.14      fs_1.5.0           xml2_1.3.2         lwgeom_0.2-5      
## [41] class_7.3-18       Cairo_1.5-12.2     textshaping_0.2.1  tools_4.0.3       
## [45] hms_1.0.0          lifecycle_0.2.0    stringr_1.4.0      munsell_0.5.0     
## [49] compiler_4.0.3     pkgdown_1.6.1      e1071_1.7-4        systemfonts_0.3.2 
## [53] rlang_0.4.10       classInt_0.4-3     units_0.6-7        grid_4.0.3        
## [57] tmaptools_3.1-1    dichromat_2.0-0    htmlwidgets_1.5.3  crosstalk_1.1.1   
## [61] leafem_0.1.3       base64enc_0.1-3    rmarkdown_2.6.4    gtable_0.3.0      
## [65] codetools_0.2-18   abind_1.4-5        DBI_1.1.1          curl_4.3          
## [69] R6_2.5.0           lubridate_1.7.9.2  knitr_1.31         fastmap_1.1.0     
## [73] rprojroot_2.0.2    ragg_0.4.1         KernSmooth_2.23-18 readr_1.4.0       
## [77] desc_1.2.0         stringi_1.5.3      parallel_4.0.3     Rcpp_1.0.6        
## [81] vctrs_0.3.6        png_0.1-7          leaflet_2.0.4.1    tidyselect_1.1.0  
## [85] xfun_0.20