以下では、公式tutorialを参考に進めていきます。

Package・ディレクトリの準備

library(sf)
library(tidyverse)
#install.packages("eurostat")
library(eurostat)
library(knitr)
library(stringr)
#install.packages("giscoR")
library(giscoR)
library(RColorBrewer)

setwd("~/R/ForTeaching")

Eurostatのデータを俯瞰する

いわゆる目次(Table of Contents: TOC)を抜き出してみる。

# Get Eurostat data listing
toc <- get_eurostat_toc()

# Check the first items
kable(tail(toc))
title code type last update of data last table structure change data start data end values
Persons living in households with very low work intensity by age and sex (population aged 0 to 64 years) ilc_lvhl11n dataset 12.10.2022 12.10.2022 2015 2021 NA
People living in households with very low work intensity by group of country of birth (population aged 18 to 64 years) ilc_lvhl16n dataset 12.09.2022 08.06.2022 2015 2021 NA
In-work at-risk-of-poverty rate by age and sex - EU-SILC survey ilc_iw01 dataset 06.10.2022 08.06.2022 2003 2021 NA
Severe housing deprivation rate by age, sex and poverty status - EU-SILC survey ilc_mdho06a dataset 09.09.2022 19.05.2021 2003 2020 NA
Overcrowding rate by age, sex and poverty status - total population - EU-SILC survey ilc_lvho05a dataset 06.10.2022 08.06.2022 2003 2021 NA
Housing cost overburden rate by age, sex and poverty status - EU-SILC survey ilc_lvho07a dataset 06.10.2022 08.06.2022 2003 2021 NA
# info about passengers
kable(search_eurostat("waste"))
title code type last update of data last table structure change data start data end values
Generation of waste by households enps_env_wasgenh dataset 10.02.2022 10.02.2022 2005 2020 NA
Municipal waste generated enps_env_wasgenm dataset 10.02.2022 10.02.2022 2005 2020 NA
Population served by municipal waste enps_env_wasacc dataset 10.02.2022 10.02.2022 2005 2019 NA
Municipal waste per inhabitant enps_env_wasgenp dataset 10.02.2022 10.02.2022 2005 2020 NA
Treatment of municipal waste enps_env_wastrt dataset 10.02.2022 10.02.2022 2005 2020 NA
Population connected to wastewater collection and treatment systems enps_env_ww_con dataset 10.02.2022 10.02.2022 2005 2020 NA
Municipal waste med_en21 dataset 28.05.2020 08.02.2021 2005 2018 NA
Generation of waste by sector med_en22 dataset 28.05.2020 01.06.2021 2005 2018 NA
Composition of municipal waste med_en23 dataset 10.12.2015 01.06.2021 2000 2013 NA
Management of waste med_en25 dataset 28.05.2020 01.06.2021 2005 2018 NA
Urban wastewater treatment med_en46 dataset 10.12.2015 01.06.2021 2000 2012 NA
Candidate countries and potential candidates: climate change and waste cpc_enclimwa dataset 17.03.2020 08.02.2021 2005 2017 NA
Generation of waste by waste category, hazardousness and NACE Rev. 2 activity env_wasgen dataset 13.09.2022 13.09.2022 2004 2020 NA
Treatment of waste by waste category, hazardousness and waste management operations env_wastrt dataset 15.09.2022 13.09.2022 2004 2020 NA
Management of waste excluding major mineral waste, by waste management operations env_wasoper dataset 11.06.2021 05.05.2021 2010 2018 NA
Management of waste excluding major mineral waste, by waste management operations and waste flow env_wasflow dataset 11.06.2021 05.05.2021 2010 2018 NA
Management of waste by waste management operations and type of material - Sankey diagram data env_wassd dataset 25.11.2021 25.11.2021 2010 2020 NA
Trade in waste by type of material and partner env_wastrdmp dataset 24.05.2022 NA 2004 2021 NA
Packaging waste by waste management operations env_waspac dataset 20.10.2022 20.10.2022 1997 2020 NA
Recycling rates of packaging waste for monitoring compliance with policy targets, by type of packaging env_waspacr dataset 20.10.2022 20.10.2022 1997 2020 NA
Waste electrical and electronic equipment (WEEE) by waste management operations - open scope, 6 product categories (from 2018 onwards) env_waseleeos dataset 28.02.2022 NA 2018 2019 NA
Waste electrical and electronic equipment (WEEE) by waste management operations env_waselee dataset 01.03.2022 01.03.2022 2005 2018 NA
End-of-life vehicles by waste management operations - detailed data env_waselv dataset 15.11.2021 15.11.2021 2005 2019 NA
Transboundary shipments of notified waste by partner, hazardousness and waste management operations env_wasship dataset 15.09.2022 18.07.2022 2010 2020 NA
Municipal waste by waste management operations env_wasmun dataset 26.09.2022 26.09.2022 1995 2020 NA
Population connected to wastewater treatment plants env_ww_con dataset 10.08.2022 10.08.2022 1970 2020 NA
Generation and discharge of wastewater in volume env_ww_genv dataset 10.08.2022 10.08.2022 1975 2020 NA
Generation and discharge of wastewater by pollutant env_ww_genp dataset 10.08.2022 10.08.2022 2000 2020 NA
Supply, transformation and consumption of renewables and wastes nrg_cb_rw dataset 12.07.2022 01.06.2022 1990 2021 NA
Electricity production capacities for renewables and wastes nrg_inf_epcrw dataset 20.04.2022 27.01.2022 1990 2020 NA
Management of waste excluding major mineral waste, by waste management operations env_wasoper dataset 11.06.2021 05.05.2021 2010 2018 NA
Municipal waste by waste management operations env_wasmun dataset 26.09.2022 26.09.2022 1995 2020 NA
Supply, transformation and consumption of renewables and wastes nrg_cb_rw dataset 12.07.2022 01.06.2022 1990 2021 NA
Electricity production capacities for renewables and wastes nrg_inf_epcrw dataset 20.04.2022 27.01.2022 1990 2020 NA
# Recycling rate of municipal waste
id<-"cei_wm011"

dat <- get_eurostat(id, time_format = "num",type = "label")

DLしたデータの可視化

dat_smallgeo<-dat |>
  filter(geo %in% c("Denmark","France",
                    "Germany (until 1990 former territory of the FRG)",
                   "Ireland",
                   "Netherlands","Norway",
                   "Spain", "Sweden",
                   "United Kingdom")) |>
  mutate(geo2=geo)

dat_smallgeo$geo2<-str_replace_all(dat_smallgeo$geo2,
                   "until 1990 former territory of the FRG",
                   " ")


dat_smallgeo |>
  ggplot(aes(x = time, y = values))+
  geom_point(aes(shape = geo2))+ 
  geom_line(aes(lty=geo2))+             
  labs(x="year", y="Recycling rate(%)",
       subtitle = "Recycling rates of selected EU countries")+ 
  theme_bw()

dat_smallgeo |>
  ggplot(aes(x = time, y = values))+
  geom_point(aes(color = geo2),size=3)+ 
  geom_line(aes(color=geo2),size=2)+             
  labs(x="year", y="Recycling rate(%)", color="countries",
       subtitle = "Recycling rates of selected EU countries")+
  theme(legend.position="bottom",
        title=element_text(size=22),
        legend.text = element_text(size=16),
        legend.title = element_text(size=16))

地図の可視化

以下は、gsicoR公式サイトを参考にして作成しています。

# Get shapes
nuts0 <- gisco_get_nuts(
  year = "2021",
  epsg = "3035",
  resolution = "1",
  nuts_level = "0"
)

# データを1年だけにする
dat2018<-dat |>
  filter(time==2018)

# データと地図をつなげるために、ISOコードを付与
dat2018$NUTS_ID<-c( "AL","AT","BE","BG","CH","CY",
                     "CZ","DE","DK","EE", "EL","ES",
                     NA, "FI","FR","HR","HU", "IE",
                     "IS","IT","LT","LU","LV","ME",
                     "MT","NL","NO","PL","PT","RO",
                     "RS","SE","SI","SK","TR","UK")

# Merge data
nuts0.sf <- left_join(nuts0,dat2018,by="NUTS_ID")

# Group by NUTS by country and convert to lines
country_lines <- nuts0 %>%
  st_cast("MULTILINESTRING")


# Plot

ggplot(nuts0.sf) +
  geom_sf(aes(fill = values), 
          size = 0, 
          alpha = 0.9) +
  geom_sf(data = country_lines, col = "black", size = 0.1) +
  scale_fill_gradientn(colors=brewer.pal(9,"GnBu"))+
  # Center in Europe: EPSG 3035
  coord_sf(
    xlim = c(2377294, 7453440),
    ylim = c(1313597, 5628510)
  ) +
  labs(
    title = "Recycling rates in 2018",
    subtitle = "Country level",
    fill="Recycling rate (%)",
    caption = paste0(
      "Source: Eurostat, ", gisco_attributions(),
      "\nBased on Milos Popovic: https://milospopovic.net/how-to-make-choropleth-map-in-r/"
    )
  ) +
  theme(legend.position="bottom",
        title=element_text(size=18),
        legend.text = element_text(size=20),
        legend.title = element_text(size=20))