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/RtmpRAawm5/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/RtmpRAawm5/eurostat/sf60220134326.RData
## Warning in get_eurostat_geospatial(output_class = "sf", resolution = "60", :
## Default of 'make_valid' for 'output_class="sf"' will be changed in the future
## (see function details).
# 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/RtmpRAawm5/eurostat/sf60220134326.RData
## sf at resolution 1: 60  from year  2013  read from cache file:  /tmp/RtmpRAawm5/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/RtmpRAawm5/eurostat/tgs00026_raw_code_FF.rds
## Table  tgs00026  read from cache file:  /tmp/RtmpRAawm5/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/RtmpRAawm5/eurostat/sf60220134326.RData
## sf at resolution 1: 60  from year  2013  read from cache file:  /tmp/RtmpRAawm5/eurostat/sf60220134326.RData
## Warning in get_eurostat_geospatial(resolution = "60", nuts_level = "2", :
## Default of 'make_valid' for 'output_class="sf"' will be changed in the future
## (see function details).
# 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.4 (2021-02-15)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.10
## 
## Matrix products: default
## BLAS:   /home/lemila/bin/R-4.0.4/lib/libRblas.so
## LAPACK: /home/lemila/bin/R-4.0.4/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-1         sf_0.9-8          
## [5] dplyr_1.0.5        eurostat_3.7.3    
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.0           lubridate_1.7.10   httr_1.4.2         rprojroot_2.0.2   
##  [5] tools_4.0.4        backports_1.2.1    bslib_0.2.4        utf8_1.2.1        
##  [9] R6_2.5.0           KernSmooth_2.23-18 DBI_1.1.1          colorspace_2.0-0  
## [13] raster_3.4-5       withr_2.4.2        sp_1.4-5           tidyselect_1.1.0  
## [17] leaflet_2.0.4.1    curl_4.3           compiler_4.0.4     leafem_0.1.3      
## [21] textshaping_0.3.3  Cairo_1.5-12.2     xml2_1.3.2         desc_1.3.0        
## [25] sass_0.3.1         scales_1.1.1       classInt_0.4-3     readr_1.4.0       
## [29] proxy_0.4-25       pkgdown_1.6.1      systemfonts_1.0.1  stringr_1.4.0     
## [33] digest_0.6.27      rmarkdown_2.7.12   base64enc_0.1-3    dichromat_2.0-0   
## [37] pkgconfig_2.0.3    htmltools_0.5.1.1  fastmap_1.1.0      highr_0.9         
## [41] htmlwidgets_1.5.3  rlang_0.4.10       jquerylib_0.1.4    generics_0.1.0    
## [45] farver_2.1.0       jsonlite_1.7.2     crosstalk_1.1.1    magrittr_2.0.1    
## [49] Rcpp_1.0.6         munsell_0.5.0      fansi_0.4.2        abind_1.4-5       
## [53] RefManageR_1.3.0   lifecycle_1.0.0    stringi_1.5.3      leafsync_0.1.0    
## [57] yaml_2.2.1         tmaptools_3.1-1    plyr_1.8.6         grid_4.0.4        
## [61] parallel_4.0.4     crayon_1.4.1       lattice_0.20-41    stars_0.5-2       
## [65] hms_1.0.0          knitr_1.33         pillar_1.6.0       codetools_0.2-18  
## [69] XML_3.99-0.6       glue_1.4.2         evaluate_0.14      vctrs_0.3.7       
## [73] png_0.1-7          gtable_0.3.0       purrr_0.3.4        tidyr_1.1.3       
## [77] assertthat_0.2.1   cachem_1.0.4       xfun_0.22          mime_0.10         
## [81] lwgeom_0.2-6       broom_0.7.6        countrycode_1.2.0  e1071_1.7-6       
## [85] ragg_1.1.2         class_7.3-18       viridisLite_0.4.0  tibble_3.1.1      
## [89] memoise_2.0.0      units_0.7-1        ellipsis_0.3.1