Skip to contents

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 the vignette of eurostat (vignette(package = "eurostat")) 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.

library(dplyr)
#> 
#> 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
library(eurostat)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(tmap)
#> Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
#> remotes::install_github('r-tmap/tmap')

# Download attribute data from Eurostat
sp_data <- eurostat::get_eurostat("tgs00026", time_format = "raw") %>%
  # subset to have only a single row per geo
  filter(TIME_PERIOD == 2016, nchar(geo) == 4) %>%
  # categorise
  mutate(income = cut_to_classes(values, n = 5))
#> Table tgs00026 cached at /tmp/RtmpfN6Ud8/eurostat/c56ebc43d61e71c047f45f97c09d96f5.rds

# Download geospatial data from GISCO
geodata <- get_eurostat_geospatial(nuts_level = 2, year = 2016)
#> Extracting data from eurostat::eurostat_geodata_60_2016

# merge with attribute data with geodata
map_data <- inner_join(geodata, sp_data, by = "geo")

Construct the map

# Create and plot the map
map1 <- tm_shape(geodata,
  projection = "EPSG:3035",
  xlim = c(2400000, 7800000),
  ylim = c(1320000, 5650000)
) +
  tm_fill("lightgrey") +
  tm_shape(map_data) +
  tm_polygons("income",
    title = "Disposable household\nincomes in 2016",
    palette = "Oranges"
  )

print(map1)

Interactive maps can be generated as well

# Interactive
tmap_mode("view")
#> tmap mode set to interactive viewing
map1
# Set the mode back to normal plotting
tmap_mode("plot")
#> tmap mode set to plotting
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)

# Downloading and manipulating the tabular data
print("Let us focus on year 2016 and NUTS-3 level")
#> [1] "Let us focus on year 2016 and NUTS-3 level"
euro_sf2 <- get_eurostat("tgs00026",
  time_format = "raw",
  filter = list(time = "2016")
) %>%
  # 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)
  )
#> Table tgs00026 cached at /tmp/RtmpfN6Ud8/eurostat/9b916562f6b233b75ce601e7d2a7ff8d.rds

print("Download geospatial data from GISCO")
#> [1] "Download geospatial data from GISCO"
geodata <- get_eurostat_geospatial(
  resolution = "01", nuts_level = 2,
  year = 2016, country = "PL"
)
#> Loading required namespace: giscoR
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

# Merge with attribute data with geodata
map_data <- inner_join(geodata, euro_sf2, by = "geo")

# plot map
library(tmap)

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() +
  tm_layout(legend.outside = TRUE)
map2

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

# Disposable income of private households by NUTS 2 regions at 1:1mln res
library(eurostat)
library(dplyr)
library(ggplot2)
data_eurostat <- get_eurostat("tgs00026", time_format = "raw") %>%
  filter(TIME_PERIOD == 2018, nchar(geo) == 4) %>%
  # classifying the values the variable
  dplyr::mutate(cat = cut_to_classes(values))
#> Dataset query already saved in cache_list.json...
#> Reading cache file /tmp/RtmpfN6Ud8/eurostat/c56ebc43d61e71c047f45f97c09d96f5.rds
#> Table  tgs00026  read from cache file:  /tmp/RtmpfN6Ud8/eurostat/c56ebc43d61e71c047f45f97c09d96f5.rds

# Download geospatial data from GISCO
data_geo <- get_eurostat_geospatial(
  resolution = "01", nuts_level = "2",
  year = 2016
)
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

# merge with attribute data with geodata
data <- left_join(data_geo, data_eurostat, by = "geo")

ggplot(data) +
  # Base layer
  geom_sf(fill = "lightgrey", color = "lightgrey") +
  # Choropleth layer
  geom_sf(aes(fill = cat), color = "lightgrey", linewidth = 0.1, na.rm = TRUE) +
  scale_fill_brewer(palette = "Oranges", na.translate = FALSE) +
  guides(fill = guide_legend(reverse = TRUE, title = "euro")) +
  labs(
    title = "Disposable household income in 2018",
    caption = "© EuroGeographics for the administrative boundaries
                Map produced in R with data from Eurostat-package http://ropengov.github.io/eurostat"
  ) +
  theme_light() +
  coord_sf(
    xlim = c(2377294, 7453440),
    ylim = c(1313597, 5628510),
    crs = 3035
  )

Citing the data sources

Eurostat data: cite Eurostat.

Administrative boundaries: cite EuroGeographics

Citing the eurostat R package

For main developers and contributors, see the package homepage.

This work can be freely used, modified and distributed under the BSD-2-clause (modified FreeBSD) license:

citation("eurostat")
#> Kindly cite the eurostat R package as follows:
#> 
#>   Lahti L., Huovari J., Kainu M., and Biecek P. (2017). Retrieval and
#>   analysis of Eurostat open data with the eurostat package. The R
#>   Journal 9(1), pp. 385-392. doi: 10.32614/RJ-2017-019
#> 
#>   Lahti, L., Huovari J., Kainu M., Biecek P., Hernangomez D., Antal D.,
#>   and Kantanen P. (2023). eurostat: Tools for Eurostat Open Data
#>   [Computer software]. R package version 4.0.0.
#>   https://github.com/rOpenGov/eurostat
#> 
#> To see these entries in BibTeX format, use 'print(<citation>,
#> bibtex=TRUE)', 'toBibtex(.)', or set
#> 'options(citation.bibtex.max=999)'.

Contact

For contact information, see the package homepage.

Version info

This tutorial was created with

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31)
#>  os       Ubuntu 22.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en
#>  collate  C.UTF-8
#>  ctype    C.UTF-8
#>  tz       UTC
#>  date     2024-01-08
#>  pandoc   2.19.2 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package           * version   date (UTC) lib source
#>  abind               1.4-5     2016-07-21 [1] RSPM
#>  assertthat          0.2.1     2019-03-21 [1] RSPM
#>  backports           1.4.1     2021-12-13 [1] RSPM
#>  base64enc           0.1-3     2015-07-28 [1] RSPM
#>  bibtex              0.5.1     2023-01-26 [1] RSPM
#>  bit                 4.0.5     2022-11-15 [1] RSPM
#>  bit64               4.0.5     2020-08-30 [1] RSPM
#>  bslib               0.6.1     2023-11-28 [1] RSPM
#>  cachem              1.0.8     2023-05-01 [1] RSPM
#>  cellranger          1.1.0     2016-07-27 [1] RSPM
#>  class               7.3-22    2023-05-03 [3] CRAN (R 4.3.2)
#>  classInt            0.4-10    2023-09-05 [1] RSPM
#>  cli                 3.6.2     2023-12-11 [1] RSPM
#>  codetools           0.2-19    2023-02-01 [3] CRAN (R 4.3.2)
#>  colorspace          2.1-0     2023-01-23 [1] RSPM
#>  countrycode         1.5.0     2023-05-30 [1] RSPM
#>  crayon              1.5.2     2022-09-29 [1] RSPM
#>  crosstalk           1.2.1     2023-11-23 [1] RSPM
#>  curl                5.2.0     2023-12-08 [1] RSPM
#>  data.table          1.14.10   2023-12-08 [1] RSPM
#>  DBI                 1.2.0     2023-12-21 [1] RSPM
#>  desc                1.4.3     2023-12-10 [1] RSPM
#>  dichromat           2.0-0.1   2022-05-02 [1] RSPM
#>  digest              0.6.33    2023-07-07 [1] RSPM
#>  dplyr             * 1.1.4     2023-11-17 [1] RSPM
#>  e1071               1.7-14    2023-12-06 [1] RSPM
#>  ellipsis            0.3.2     2021-04-29 [1] RSPM
#>  eurostat          * 4.0.0     2024-01-08 [1] local
#>  evaluate            0.23      2023-11-01 [1] RSPM
#>  fansi               1.0.6     2023-12-08 [1] RSPM
#>  farver              2.1.1     2022-07-06 [1] RSPM
#>  fastmap             1.1.1     2023-02-24 [1] RSPM
#>  fs                  1.6.3     2023-07-20 [1] RSPM
#>  generics            0.1.3     2022-07-05 [1] RSPM
#>  geojsonsf           2.0.3     2022-05-30 [1] RSPM
#>  ggplot2           * 3.4.4     2023-10-12 [1] RSPM
#>  giscoR              0.4.0     2023-10-30 [1] RSPM
#>  glue                1.6.2     2022-02-24 [1] RSPM
#>  gtable              0.3.4     2023-08-21 [1] RSPM
#>  here                1.0.1     2020-12-13 [1] RSPM
#>  highr               0.10      2022-12-22 [1] RSPM
#>  hms                 1.1.3     2023-03-21 [1] RSPM
#>  htmltools           0.5.7     2023-11-03 [1] RSPM
#>  htmlwidgets         1.6.4     2023-12-06 [1] RSPM
#>  httr                1.4.7     2023-08-15 [1] RSPM
#>  httr2               1.0.0     2023-11-14 [1] RSPM
#>  ISOweek             0.6-2     2011-09-07 [1] RSPM
#>  jquerylib           0.1.4     2021-04-26 [1] RSPM
#>  jsonlite            1.8.8     2023-12-04 [1] RSPM
#>  KernSmooth          2.23-22   2023-07-10 [3] CRAN (R 4.3.2)
#>  knitr               1.45      2023-10-30 [1] RSPM
#>  lattice             0.21-9    2023-10-01 [3] CRAN (R 4.3.2)
#>  leafem              0.2.3     2023-09-17 [1] RSPM
#>  leaflet             2.2.1     2023-11-13 [1] RSPM
#>  leaflet.providers   2.0.0     2023-10-17 [1] RSPM
#>  leafsync            0.1.0     2019-03-05 [1] RSPM
#>  lifecycle           1.0.4     2023-11-07 [1] RSPM
#>  lubridate           1.9.3     2023-09-27 [1] RSPM
#>  lwgeom              0.2-13    2023-05-22 [1] RSPM
#>  magrittr            2.0.3     2022-03-30 [1] RSPM
#>  memoise             2.0.1     2021-11-26 [1] RSPM
#>  munsell             0.5.0     2018-06-12 [1] RSPM
#>  pillar              1.9.0     2023-03-22 [1] RSPM
#>  pkgconfig           2.0.3     2019-09-22 [1] RSPM
#>  pkgdown             2.0.7     2022-12-14 [1] any (@2.0.7)
#>  plyr                1.8.9     2023-10-02 [1] RSPM
#>  png                 0.1-8     2022-11-29 [1] RSPM
#>  proxy               0.4-27    2022-06-09 [1] RSPM
#>  purrr               1.0.2     2023-08-10 [1] RSPM
#>  R.cache             0.16.0    2022-07-21 [1] RSPM
#>  R.methodsS3         1.8.2     2022-06-13 [1] RSPM
#>  R.oo                1.25.0    2022-06-12 [1] RSPM
#>  R.utils             2.12.3    2023-11-18 [1] RSPM
#>  R6                  2.5.1     2021-08-19 [1] RSPM
#>  ragg                1.2.7     2023-12-11 [1] RSPM
#>  rappdirs            0.3.3     2021-01-31 [1] RSPM
#>  raster              3.6-26    2023-10-14 [1] RSPM
#>  RColorBrewer        1.1-3     2022-04-03 [1] RSPM
#>  Rcpp                1.0.11    2023-07-06 [1] RSPM
#>  readr               2.1.4     2023-02-10 [1] RSPM
#>  readxl              1.4.3     2023-07-06 [1] RSPM
#>  RefManageR          1.4.0     2022-09-30 [1] RSPM
#>  regions             0.1.8     2021-06-21 [1] RSPM
#>  rlang               1.1.2     2023-11-04 [1] RSPM
#>  rmarkdown           2.25      2023-09-18 [1] RSPM
#>  rprojroot           2.0.4     2023-11-05 [1] RSPM
#>  s2                  1.1.6     2023-12-19 [1] RSPM
#>  sass                0.4.8     2023-12-06 [1] RSPM
#>  scales              1.3.0     2023-11-28 [1] RSPM
#>  sessioninfo         1.2.2     2021-12-06 [1] any (@1.2.2)
#>  sf                * 1.0-15    2023-12-18 [1] RSPM
#>  sp                  2.1-2     2023-11-26 [1] RSPM
#>  stars               0.6-4     2023-09-11 [1] RSPM
#>  stringi             1.8.3     2023-12-11 [1] RSPM
#>  stringr             1.5.1     2023-11-14 [1] RSPM
#>  styler              1.10.2    2023-08-29 [1] RSPM
#>  systemfonts         1.0.5     2023-10-09 [1] RSPM
#>  terra               1.7-65    2023-12-15 [1] RSPM
#>  textshaping         0.3.7     2023-10-09 [1] RSPM
#>  tibble              3.2.1     2023-03-20 [1] RSPM
#>  tidyr               1.3.0     2023-01-24 [1] RSPM
#>  tidyselect          1.2.0     2022-10-10 [1] RSPM
#>  timechange          0.2.0     2023-01-11 [1] RSPM
#>  tmap              * 3.3-4     2023-09-12 [1] RSPM
#>  tmaptools           3.1-1     2021-01-19 [1] RSPM
#>  tzdb                0.4.0     2023-05-12 [1] RSPM
#>  units               0.8-5     2023-11-28 [1] RSPM
#>  utf8                1.2.4     2023-10-22 [1] RSPM
#>  vctrs               0.6.5     2023-12-01 [1] RSPM
#>  viridisLite         0.4.2     2023-05-02 [1] RSPM
#>  vroom               1.6.5     2023-12-05 [1] RSPM
#>  withr               2.5.2     2023-10-30 [1] RSPM
#>  wk                  0.9.1     2023-11-29 [1] RSPM
#>  xfun                0.41      2023-11-01 [1] RSPM
#>  XML                 3.99-0.16 2023-11-29 [1] RSPM
#>  xml2                1.3.6     2023-12-04 [1] RSPM
#>  yaml                2.3.8     2023-12-11 [1] RSPM
#> 
#>  [1] /home/runner/work/_temp/Library
#>  [2] /opt/R/4.3.2/lib/R/site-library
#>  [3] /opt/R/4.3.2/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────