demo.R 2.07 KB
Newer Older
1

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
2
### Preparation ###
3
devtools::install_url("https://renkulab.io/gitlab/dscc/metadata-auto-r-library/-/archive/v002/metadata-auto-r-library-v002.tar.gz")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
4
5
6
library(fso.metadata)
library(ggplot2)
library(tidyverse)
7

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
8
### 1. Get a codelist ###
9
10
# All languages
codelist <- get_codelist(identifier='CL_NOGA_SECTION')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
11
head(codelist, 3)
12
13

# In french
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
14
15
codelist_fr <- get_codelist(identifier='CL_NOGA_SECTION', language='fr')
head(codelist_fr, 3)
16

17

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
18
### 2. Get a nomenclature of one level ###
19
20
# All language: Level 1
nomenclature <- get_nomenclature_one_level(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
21
22
  identifier='HCL_CH_ISCO_19_PROF', 
  level_number=2
23
)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
24
head(nomenclature, 3)
25
26

# French: Level 2
27
nomenclature_fr <- get_nomenclature_one_level(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
28
29
30
  identifier='HCL_CH_ISCO_19_PROF', 
  level_number=1, 
  language='fr'
31
)
32
head(nomenclature_fr, 5)
33
34


Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
35
36

### 3. Get a nomenclature of multiple levels ###
37
38
39
# French
multi_nomenclature_fr <- get_nomenclature_multiple_levels(
  identifier='HCL_CH_ISCO_19_PROF', 
40
  level_from=1, 
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
41
  level_to=6, 
42
43
  language='fr'
)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
44
head(multi_nomenclature_fr, 10)
45

46

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

### 4. Concrete example from Mr. van Nieuwkoop with Noga Data
load("data/pkagg.Rdata")
head(pk_agg$A88)

noga2 <- as_tibble(get_codelist(identifier='CL_NOGA_DIVISION', language='it'))
names(noga2) <- c('id', 'label', 'name')
head(noga2)

# Get the completely disaggregated production accounts
#  and join them with the noga2 descriptions
a88 <- pk_agg$A88 %>% 
  left_join(noga2, by = c("Code" = "id")) 

head(a88)

# Filter and prepare data
a88_filtered <- a88 %>% 
  select(-Beschreibung, -name) %>% 
  relocate(label, .after = Code) %>% 
  rename(Department = label, Year = Jahr, Component = Komponent) %>% 
  select(Code, Department, Component, Year, Nominal) %>% 
  filter( Nominal > 0) %>% 
  filter(!is.na(Department)) %>%        # keep 
  filter(Code %in% c("01","02", "03")) # keep first 3 department

# Plot the intermediate consumption (CI), the value added (VA), and the 
# production value (VP) for the section A (agriculture)
ggplot(a88_filtered, aes(Year, Nominal, color = Component)) + 
  geom_line() + 
  ylab("in Mio. CHF") +
  facet_wrap(~Department,  scales = "free")