demo.R 2.8 KB
Newer Older
1
2
install.packages("remotes")
remotes::install_gitlab("DSCC/fso-metadata-r")
3

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
4
library(fso.metadata)
5

6

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

12
### 1. Get a codelist ###
13
# In french
14
codelist_fr <- get_codelist(identifier = "CL_NOGA_SECTION", language = "fr")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
15
head(codelist_fr, 3)
16

17
18
19
# In german
codelist_de <- get_codelist(identifier = "CL_NOGA_SECTION", language = "de")
head(codelist_de, 3)
20

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
21

22
23
24
25
26
27
28
### 2. Get a nomenclature of multiple levels ###
# In italian
multi_nomenclature_it <- get_nomenclature_multiple_levels(
  identifier = "HCL_CH_ISCO_19_PROF",
  level_from = 1,
  level_to = 6,
  language = "it"
29
)
30
head(multi_nomenclature_it, 8)
31

32

33
34
35
### 3. Concrete example from Mr. van Nieuwkoop with Noga Data
library(ggplot2)
library(tidyverse)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
36

37
38
39
40
# Load the production account data for the agriculture divisions
load("data/pk_agr.Rdata")
pk_agr <- rename(pk_agr, Component = Komponent, Year = Jahr)
head(pk_agr)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
41

42
43
44
45
46
# Load the descriptions of the NOGA divisions
noga2 <- as_tibble(
  get_codelist(identifier='CL_NOGA_DIVISION', language='fr')
)
names(noga2) <- c("id", "label", "name")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
47
48
head(noga2)

49
50
51
52
53
54
55
# Join the production account data with the noga2 descriptions
pk <- pk_agr %>%
  left_join(noga2, by = c("Code" = "id")) %>%
  select(-name) %>%
  relocate(label, .after = Code) %>%
  rename(Department = label)
head(pk)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
56

57
# Plot the intermediate consumption (CI), the value added (VA), and the
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
58
# production value (VP) for the section A (agriculture)
59
60
61
62
63
pk %>%
  select(Code, Department, Component, Year, Nominal) %>%
  filter(Nominal > 0 & !is.na(Department)) %>%
  ggplot(aes(Year, Nominal, color = Component)) +
  geom_line() +
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
64
65
  ylab("in Mio. CHF") +
  facet_wrap(~Department,  scales = "free")
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101




plot_agriculture <- function(pk_agr, language) {
  # Load the descriptions of the NOGA divisions
  noga2 <- as_tibble(
    get_codelist(
      identifier='CL_NOGA_DIVISION', 
      language=language,
      environment='ABN') # for the demo, only available within network
  )
  names(noga2) <- c("id", "label", "name")
  
  # Join the production account data with the noga2 descriptions
  pk <- pk_agr %>%
    left_join(noga2, by = c("Code" = "id")) %>%
    select(-name) %>%
    relocate(label, .after = Code) %>%
    rename(Department = label)
  
  # Plot the intermediate consumption (CI), the value added (VA), and the
  # production value (VP) for the section A (agriculture)
  pk %>%
    select(Code, Department, Component, Year, Nominal) %>%
    filter(Nominal > 0 & !is.na(Department)) %>%
    ggplot(aes(Year, Nominal, color = Component)) +
    geom_line() +
    ylab("in Mio. CHF") +
    facet_wrap(~Department,  scales = "free")
}

plot_agriculture(pk_agr, language='fr')
plot_agriculture(pk_agr, language='de')
plot_agriculture(pk_agr, language='it')
plot_agriculture(pk_agr, language='en')