Makro II: Zusätzliches Material

by Markus Mößler


VGR: Entstehung, Verwendung und Verteilung


Dieses Dokument enthält eine Auswertung der VGR Daten von der OECD.


Vorbereitung

Wir verwenden die vierteljährlichen VGR Daten von der Organisation für wirtschaftliche Zusammenarbeit und Entwicklung (OECD).

Die Daten können über das R-Paket oecd direkt in die Software R geladen werden.

  • R-Paket oecd, Link: Search and extract data from the OECD
  • Heruntergelden werden R-Pakete mit: install.packages(OECD).
  • Geladen werden R-Pakete mit: library(OECD).

Für die Auswertung verwenden wir vorwiegend R-Pakete Teil der base Installation.

Außerde verwenden wir außerdem folgenden R-Pakete:

  • ggplot2, Link: Create elegant data visualisations using the grammar of graphics
  • dplyr, Link: A grammar of data manipulation
  • tidyr, Link: Tidy messy data
  • lubridate, Link: Make dealing with dates a little easier
  • scales, Link: Scale functions for visualization
  • zoo, Link: S3 Infrastructure for Regular and Irregular Time Series (Z’s Ordered Observations)

Die folgenden R-Pakete werden für die Darstellung der Ergebnisse verwendet:

  • gridExtra, Link: Miscellaneous functions for “grid” graphics
  • kableExtra, Link: Construct complex table with ‘kable’ and pipe syntax
library(gridExtra)
library(kableExtra)

Die folgenden R-Funktionen werden zur Berechnung von Anteilen, Differenzen, und Wachstumsbeiträgen verwendet.

L01_fun  <- function(x) (lag(x, n = 1)) # Construct lag
D01_fun  <- function(x) (lag(x, n = 0) - lag(x, n = 1)) # Construct difference
G01_fun  <- function(x) ((lag(x, n = 0) - lag(x, n = 1))/lag(x, n = 1)*100) # Construct growth rate
X00_fun  <- function(x,y) (x/y)*100 # Construct share
GX01_fun <- function(x,y) ((lag(x, n = 0) - lag(x, n = 1))/lag(x, n = 1)*100*lag(x, n = 1)/lag(y, n = 1)) # Construct growth contribution

Die folgenden R-Funktionen werden verwendet um bestimmte Umformungen der Daten vorzunehmen.

dat_man_fun <- function(dat.fra, arg.lis, col.nam.sel, col.nam.out){
  
  # add dat.fra as first element to list
  arg.lis[[1]] <- dat.fra
  
  # select columns of dat.fra
  dat.fra <- arg.lis[[1]]
  arg.lis[[1]] <- subset(arg.lis[[1]], select = col.nam.sel)

  # core only
  do.call(apply, args = arg.lis)
  
  # altogether
  res <- cbind(dat.fra,
               setNames(
                 as.data.frame(
                   do.call(
                     apply, args = arg.lis)), col.nam.out))
  
  return(res)

}
oecd_qna_fun <- function(dat, loc, sub, mea, fre, sta.dat, end.dat, sub.inp.lis) {
  
  # Download if necessary!
  if(is.null(dat)){
    # Input list
    fil <- list(loc, sub, mea, fre)
    # > Download
    qna.dat <- get_dataset(dataset = "QNA", filter = fil) %>%
      subset(yq(Time) %in% seq(sta.dat, end.dat, by = "quarter")) # filter time period
  } 
  if(!is.null(dat)){
    qna.dat <- dat %>%
      subset(LOCATION %in% loc) %>%
      subset(SUBJECT %in% sub) %>%
      subset(MEASURE %in% mea) %>%
      subset(FREQUENCY %in% fre) %>%
      subset(yq(Time) %in% seq(sta.dat, end.dat, by = "quarter"))
  }

  # > Transform data to wide
  dat.l.00 <- qna.dat # long format
  dat.w.00 <- dat.l.00 %>%
    subset(select = c("LOCATION", "Time", "SUBJECT", "ObsValue")) %>%
    spread(SUBJECT, ObsValue) # wide format
  # Note: tidyr (1st generation) approach
  
  # > Construct sector aggregates
  nn <- length(sub.inp.lis)
  sub.inp.lab <- names(sub.inp.lis)
  dat.01 <- dat.w.00
  for (ii in (1:nn)) {
    dat.01 <- dat.01 %>%
      dat_man_fun(list(X = NULL, MARGIN = 1, FUN = "sum"), sub.inp.lis[[ii]], sub.inp.lab[[ii]])
  }

  # > Construct sector shares
  sub.sel <- sub.inp.lab
  sub.x  <-  paste(sub.sel, "_X00", sep = "")
  sub.g  <-  paste(sub.sel, "_G01", sep = "")
  sub.gx <-  paste(sub.sel, "_GX01", sep = "")
  
  dat.02 <- dat.01 %>% 
    dat_man_fun(list(X = NULL, MARGIN = 2, FUN = "X00_fun", y = dat.01$tot), sub.sel, sub.x) %>%
    dat_man_fun(list(X = NULL, MARGIN = 2, FUN = "G01_fun"), sub.sel, sub.g) %>%
    dat_man_fun(list(X = NULL, MARGIN = 2, FUN = "GX01_fun", y = dat.01$tot), sub.sel, sub.gx)

  dat.03 <- dat.02 %>%
    subset(select = c("LOCATION", "Time", sub.sel, sub.x,sub.g,sub.gx))

  # > Select values and transform wide to long
  dat.04 <- dat.02 %>% 
    gather(SUBJECT,ObsValue,sub.sel[1]:sub.gx[nn]) %>% # long format
    subset(select = c("LOCATION", "Time", "SUBJECT", "ObsValue"))

  ret.lis <- list(dat = dat.02,
                  dat.03 = dat.03,
                  dat.04 = dat.04,
                  sub.sel = sub.sel, sub.x = sub.x, sub.g = sub.g, sub.gx = sub.gx,
                  end.dat = end.dat, sta.dat = sta.dat,
                  nn = nn)

  return(ret.lis)
  
}

Überblick

Beispiel: Land Deutschland, 2018

Überblick

Beispiel: Land Deutschland, 2018

Die Struktur der VGR Daten der OECD kann duch die Funktion get_data_structure heruntergalden werden.

source("https://raw.githubusercontent.com/mmoessler/macro-dashboard/main/r-scripts/r_oecd_helper_functions.R")
qna.str <- OECD_get_data_structure_fun("QNA")
qna.str$VAR_DESC
##                 id        description
## 1         LOCATION            Country
## 2          SUBJECT            Subject
## 3          MEASURE            Measure
## 4        FREQUENCY          Frequency
## 5             TIME             Period
## 6        OBS_VALUE  Observation Value
## 7      TIME_FORMAT        Time Format
## 8       OBS_STATUS Observation Status
## 9             UNIT               Unit
## 10       POWERCODE    Unit multiplier
## 11 REFERENCEPERIOD   Reference period

Die Daten können duch die Funktion get_dataset heruntergalden werden.

# > Inputs for download
# loc <- qna.str$LOCATION[,1]
loc <- c("DEU", "FRA", "ITA", "EA20", "GBR", "USA")
sub <- qna.str$SUBJECT[,1]
mea <- c("LNBQRSA") # Measure: Constant prices seasonal adjusted
fre <- c("Q") # Frequency: Quarterly frequency
sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
end.dat <- as.Date("2023-07-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01
fil <- list(loc, sub, mea, fre) # Collect inputs for get_dataset function

# > Download
qna.dat.00 <- get_dataset(dataset = "QNA", filter = fil)

qna.dat.00 <- get_dataset(dataset = "QNA", filter = fil) %>%
  subset(yq(Time) %in% seq(sta.dat, end.dat, by = "quarter")) # filter time period

qna.dat.00$ObsValue <- as.numeric(qna.dat.00$ObsValue)

head(qna.dat.00)
tail(qna.dat.00)
# > Inputs for download
# loc <- qna.str$LOCATION[,1]
loc <- c("DEU", "FRA", "ITA", "EA20", "GBR", "USA")
sub <- qna.str$SUBJECT[,1]
mea <- c("LNBQRSA") # Measure: Constant prices seasonal adjusted
fre <- c("Q") # Frequency: Quarterly frequency
sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
end.dat <- as.Date("2023-07-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01
fil <- list(loc, sub, mea, fre) # Collect inputs for get_dataset function

if (upd.dat) {

  # > Download
  # qna.dat.00 <- get_dataset(dataset = "QNA", filter = fil) %>%
  #   mutate(Time = Time) %>%
  #   subset(yq(Time) %in% seq(sta.dat, end.dat, by = "quarter")) # filter time period
  
  # > Download
  qna.dat.00 <- get_dataset(dataset = "QNA", filter = fil)
  
  # qna.dat.00 <- get_dataset(dataset = "QNA", filter = fil) %>%
  #   subset(yq(Time) %in% seq(sta.dat, end.dat, by = "quarter")) # filter time period
  
  qna.dat.00 <- get_dataset(dataset = "QNA", filter = fil) %>%
    subset(yq(Time) %in% seq(sta.dat, end.dat, by = "quarter")) # filter time period
  
  qna.dat.00$ObsValue <- as.numeric(qna.dat.00$ObsValue)
  
  head(qna.dat.00)
  tail(qna.dat.00)

} else {
  
  load("./data/qna_dat_00.RData")
  
  qna.dat.00$ObsValue <- as.numeric(qna.dat.00$ObsValue)
  
  head(qna.dat.00)
  tail(qna.dat.00)
  
}
## # A tibble: 6 × 11
##   FREQUENCY LOCATION MEASURE OBS_STATUS ObsValue POWERCODE REFERENCEPERIOD SUBJECT TIME_FORMAT Time    UNIT 
##   <chr>     <chr>    <chr>   <chr>         <dbl> <chr>     <chr>           <chr>   <chr>       <chr>   <chr>
## 1 Q         EA20     LNBQRSA <NA>        226804. 6         2015            P32S13  P3M         2022-Q2 EUR  
## 2 Q         EA20     LNBQRSA <NA>        227686. 6         2015            P32S13  P3M         2022-Q3 EUR  
## 3 Q         EA20     LNBQRSA <NA>        228884. 6         2015            P32S13  P3M         2022-Q4 EUR  
## 4 Q         EA20     LNBQRSA <NA>        227277. 6         2015            P32S13  P3M         2023-Q1 EUR  
## 5 Q         EA20     LNBQRSA <NA>        227688. 6         2015            P32S13  P3M         2023-Q2 EUR  
## 6 Q         EA20     LNBQRSA <NA>        228051. 6         2015            P32S13  P3M         2023-Q3 EUR

Zurück zur Startseite


Entstehung

Deutschland

# > Germany, output
loc <- c("DEU") # Location: Germany
mea <- c("LNBQRSA") # Measure: Constant prices seasonal adjusted
fre <- c("Q") # Frequency: Quarterly frequency
sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
end.dat <- as.Date("2023-07-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01

sub <- c("B1_GA","B1GVA","B1GVB_E","B1GVF","B1GVG_I","B1GVJ","B1GVK","B1GVL","B1GVM_N","B1GVO_Q","B1GVR_U","D21_D31")
sub.agr <- c("B1GVA")
sub.ind <- c("B1GVB_E")
sub.con <- c("B1GVF")
sub.ser <- c("B1GVG_I","B1GVJ","B1GVK","B1GVL","B1GVM_N","B1GVO_Q","B1GVR_U")
sub.tls <- c("D21_D31") # taxes less subsidies on products
sub.tot <- c("B1_GA")
sub.inp.lis <- list(agr = sub.agr,
                    ind = sub.ind,
                    con = sub.con,
                    ser = sub.ser,
                    tls = sub.tls,
                    tot = sub.tot)

ger.out.01.res <- oecd_qna_fun(qna.dat.00, loc, sub, mea, fre, sta.dat, end.dat, sub.inp.lis)
# > Inputs
oecd.qna.res <- ger.out.01.res

dat.inp <- oecd.qna.res$dat.04 %>%
  subset(yq(Time) %in% oecd.qna.res$end.dat) %>%
  subset(SUBJECT %in% oecd.qna.res$sub.x[-oecd.qna.res$nn])

# > Labels
lab.inp <- c("Landw.",
             "Bau",
             "Industrie",
             "Service",
             "St-Su")

# > Plot shares as pieplot
plt.x <- dat.inp %>%
  ggplot(aes(x = "", y = ObsValue, fill = SUBJECT)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y") +
  scale_fill_discrete(name = "", labels = lab.inp) +
  theme(legend.position = "bottom") +
  labs(x = "Anteil in Prozent", y = dat.inp[1,"Time"],
       caption = "Quelle: OECD, vierteljährliche VGR") +
  theme(plot.caption = element_text(face = "italic"))
plt.x

# > Inputs
oecd.qna.res <- ger.out.01.res

dat.inp <- oecd.qna.res$dat.04 %>%
  subset(yq(Time) %in% seq(as.Date(oecd.qna.res$sta.dat),as.Date(oecd.qna.res$end.dat),0.25)[-1]) %>%
  subset(SUBJECT %in% oecd.qna.res$sub.gx)

# > Labels
lab.inp <- c("Landw.",
             "Bau",
             "Industrie",
             "Service",
             "St-Su")

# > Plot growth contributions
plt.gx <- dat.inp %>%
  ggplot() +
  geom_bar(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q"), fill = SUBJECT), position = "stack", stat = "identity", subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[-oecd.qna.res$nn])) +
  geom_point(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
  geom_line(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
  scale_fill_discrete(name="",labels = lab.inp) +
  theme(legend.position = "bottom") +
  labs(x = "",y = "Veränderung in Prozentpunkte (QoQ)",
       caption = "Quelle: OECD, vierteljährliche VGR") +
  theme(axis.text.x = element_text(angle = 90),
        plot.caption = element_text(face = "italic")) +  
  scale_x_yearqtr(breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 1)[-1],
                  minor_breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 0.25)[-1],
                  format = "%Y-Q%q",
                  expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(-10, 10, 2))
plt.gx


Zurück zur Startseite


Vergleich

oecd_qna_out_fun <- function(loc, sta.dat, end.dat,
                             plt.x.tit = "", plt.gx.tit = ""){
  
  
  # > Data preparation
  # loc <- c("DEU") # Location: Germany
  mea <- c("LNBQRSA") # Measure: Constant prices seasonal adjusted
  fre <- c("Q") # Frequency: Quarterly frequency
  # sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
  # end.dat <- as.Date("2023-04-01")
  # Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01

  sub <- c("B1_GA","B1GVA","B1GVB_E","B1GVF","B1GVG_I","B1GVJ","B1GVK","B1GVL","B1GVM_N","B1GVO_Q","B1GVR_U","D21_D31")
  sub.agr <- c("B1GVA")
  sub.ind <- c("B1GVB_E")
  sub.con <- c("B1GVF")
  sub.ser <- c("B1GVG_I","B1GVJ","B1GVK","B1GVL","B1GVM_N","B1GVO_Q","B1GVR_U")
  sub.tls <- c("D21_D31") # taxes less subsidies on products
  sub.tot <- c("B1_GA")
  sub.inp.lis <- list(agr = sub.agr,
                      ind = sub.ind,
                      con = sub.con,
                      ser = sub.ser,
                      tls = sub.tls,
                      tot = sub.tot)

  out.01.res <- oecd_qna_fun(qna.dat.00, loc, sub, mea, fre, sta.dat, end.dat, sub.inp.lis)
  
  
  # > Illustration share levels
  oecd.qna.res <- out.01.res

  dat.inp <- oecd.qna.res$dat.04 %>%
    subset(yq(Time) %in% oecd.qna.res$end.dat) %>%
    subset(SUBJECT %in% oecd.qna.res$sub.x[-oecd.qna.res$nn])

  # >> Labels
  lab.inp <- c("Landw.",
               "Bau",
               "Industrie",
               "Service",
               "St-Su")

  # >> Plot shares as pieplot
  plt.x <- dat.inp %>%
    ggplot(aes(x = "", y = ObsValue, fill = SUBJECT)) +
    geom_bar(width = 1, stat = "identity") +
    coord_polar("y") +
    scale_fill_discrete(name = "", labels = lab.inp) +
    theme(legend.position = "bottom") +
    labs(x = "Anteil in Prozent", y = dat.inp[1,"Time"],
         title = plt.x.tit,
         caption = "Quelle: OECD, vierteljährliche VGR") +
    theme(plot.caption = element_text(face = "italic"))
  
  
  # > Illustration growth contribution
  oecd.qna.res <- out.01.res

  dat.inp <- oecd.qna.res$dat.04 %>%
    subset(yq(Time) %in% seq(as.Date(oecd.qna.res$sta.dat), as.Date(oecd.qna.res$end.dat), 0.25)[-1]) %>%
    subset(SUBJECT %in% oecd.qna.res$sub.gx)

  # >> Labels
  lab.inp <- c("Landw.",
               "Bau",
               "Industrie",
               "Service",
               "St-Su")

  # >> Plot growth contributions
  plt.gx <- dat.inp %>%
    ggplot() +
    geom_bar(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q"), fill = SUBJECT), position = "stack", stat = "identity", subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[-oecd.qna.res$nn])) +
    geom_point(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
    geom_line(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
    scale_fill_discrete(name = "", labels = lab.inp) +
    theme(legend.position = "bottom") +
    labs(x = "", y = "Veränderung in Prozentpunkte (QoQ)",
         title = plt.gx.tit,
         caption = "Quelle: OECD, vierteljährliche VGR") +
    theme(axis.text.x = element_text(angle = 90),
          plot.caption = element_text(face = "italic")) +  
    scale_x_yearqtr(breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 1)[-1],
                    minor_breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 0.25)[-1],
                    format = "%Y-Q%q",
                    expand = c(0, 0)) +
    scale_y_continuous(breaks = seq(-20, 20, 2))
  
  ret.lis <- list(plt.x=plt.x, plt.gx=plt.gx, out.01.res=out.01.res, lab.inp=lab.inp)
  
  return(ret.lis)
  
}
# > Start and end date
sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
end.dat <- as.Date("2023-07-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01

# > Germany
loc <- c("DEU")
oecd.qna.out.ger <- oecd_qna_out_fun(loc, sta.dat, end.dat, plt.x.tit = "Deutschland (Anteile)", plt.gx.tit = "Deutschland (Wachstumsbeiträge)")
# > France
loc <- c("FRA")
oecd.qna.out.fra <- oecd_qna_out_fun(loc, sta.dat, end.dat, plt.x.tit = "Frankreich (Anteile)", plt.gx.tit = "Frankreich (Wachstumsbeiträge)")
# > Italy
loc <- c("ITA")
oecd.qna.out.ita <- oecd_qna_out_fun(loc, sta.dat, end.dat, plt.x.tit = "Italien (Anteile)", plt.gx.tit = "Italien (Wachstumsbeiträge)")
# # > USA
# loc <- c("USA")
# oecd.qna.out.usa <- oecd_qna_out_fun(loc, sta.dat, end.dat, plt.x.tit = "Vereinigte Staaten (Anteile)", plt.gx.tit = "Vereinigte Staaten (Wachstumsbeiträge)")
# > Eurozone
loc <- c("EA20")
oecd.qna.out.ea20 <- oecd_qna_out_fun(loc, sta.dat, end.dat, plt.x.tit = "Euroraum (Anteile)", plt.gx.tit = "Euroraum (Wachstumsbeiträge)")
# > United Kingdom
loc <- c("GBR")
oecd.qna.out.gbr <- oecd_qna_out_fun(loc, sta.dat, end.dat, plt.x.tit = "Vereinigtes Königreich (Anteile)", plt.gx.tit = "Vereinigtes Königreich (Wachstumsbeiträge)")


Zurück zur Startseite


Verwendung

Deutschland

# > Germany, expenditure
loc <- c("DEU") # Location: Germany
mea <- c("LNBQRSA") # Measure: Constant prices seasonal adjusted
fre <- c("Q") # Frequency: Quarterly frequency
sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
end.dat <- as.Date("2023-07-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01

sub <- c("B1_GE","P3","P5","B11")
sub.con <- c("P3")
sub.inv <- c("P5")
sub.ext <- c("B11")
sub.tot <- c("B1_GE")
sub.inp.lis <- list(con = sub.con,
                    inv = sub.inv,
                    ext = sub.ext,
                    tot = sub.tot)

ger.exp.01.res <- oecd_qna_fun(qna.dat.00, loc,sub, mea, fre, sta.dat, end.dat, sub.inp.lis)
# > Inputs
oecd.qna.res <- ger.exp.01.res

dat.inp <- oecd.qna.res$dat.04 %>%
  subset(yq(Time) %in% oecd.qna.res$end.dat) %>%
  subset(SUBJECT %in% oecd.qna.res$sub.x[-oecd.qna.res$nn])

# > Labels
lab.inp <- c("Konsum",
             "Außenwirtschaft",
             "Investitionen")

# > Plot shares as pieplot
plt.x <- dat.inp %>%
  ggplot(aes(x = "", y = ObsValue, fill = SUBJECT)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y") +
  scale_fill_discrete(name = "", labels = lab.inp) +
  theme(legend.position = "bottom") +
  labs(x = "Anteil in Prozent", y = dat.inp[1,"Time"],
       caption = "Quelle: OECD, vierteljährliche VGR") +
  theme(plot.caption = element_text(face = "italic"))
plt.x

# > Inputs
oecd.qna.res <- ger.exp.01.res

dat.inp <- oecd.qna.res$dat.04 %>%
  subset(yq(Time) %in% seq(as.Date(oecd.qna.res$sta.dat), as.Date(oecd.qna.res$end.dat), 0.25)[-1]) %>%
  subset(SUBJECT %in% oecd.qna.res$sub.gx)

# > Labels
lab.inp <- c("Konsum",
             "Außenwirtschaft",
             "Investitionen")

# > Plot growth contributions
plt.gx <- dat.inp %>%
  ggplot() +
  geom_bar(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q"), fill = SUBJECT), position = "stack", stat = "identity", subset(dat.inp, SUBJECT %in% oecd.qna.res$sub.gx[-oecd.qna.res$nn])) +
  geom_point(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp, SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
  geom_line(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
  scale_fill_discrete(name = "", labels = lab.inp) +
  theme(legend.position = "bottom") +
  labs(x = "", y = "Veränderung in Prozentpunkte (QoQ)",
       caption = "Quelle: OECD, vierteljährliche VGR") +
  theme(axis.text.x = element_text(angle = 90),
        plot.caption = element_text(face = "italic")) +  
  scale_x_yearqtr(breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 1)[-1],
                  minor_breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 0.25)[-1],
                  format = "%Y-Q%q",
                  expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(-10, 10, 2))

plt.gx


Zurück zur Startseite


Vergleich

oecd_qna_exp_fun <- function(loc,sta.dat,end.dat,
                             plt.x.tit = "", plt.gx.tit = ""){
  
  
  # > Data preparation
  # loc <- c("DEU") # Location: Germany
  mea <- c("LNBQRSA") # Measure: Constant prices seasonal adjusted
  fre <- c("Q") # Frequency: Quarterly frequency
  # sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
  # end.dat <- as.Date("2023-04-01")
  # Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01

  sub <- c("B1_GE","P3","P5","B11")
  sub.con <- c("P3")
  sub.inv <- c("P5")
  sub.ext <- c("B11")
  sub.tot <- c("B1_GE")
  sub.inp.lis <- list(con = sub.con,
                      inv = sub.inv,
                      ext = sub.ext,
                      tot = sub.tot)

  exp.01.res <- oecd_qna_fun(qna.dat.00, loc, sub, mea, fre, sta.dat, end.dat, sub.inp.lis)
  
  
  # > Illustration share levels
  oecd.qna.res <- exp.01.res

  dat.inp <- oecd.qna.res$dat.04 %>%
    subset(yq(Time) %in% oecd.qna.res$end.dat) %>%
    subset(SUBJECT %in% oecd.qna.res$sub.x[-oecd.qna.res$nn])

  # > Labels
  lab.inp <- c("Konsum",
               "Außenwirtschaft",
               "Investitionen")

  # > Plot shares as pieplot
  plt.x <- dat.inp %>%
    ggplot(aes(x = "", y = ObsValue, fill = SUBJECT)) +
    geom_bar(width = 1, stat = "identity") +
    coord_polar("y") +
    scale_fill_discrete(name = "", labels = lab.inp) +
    theme(legend.position = "bottom") +
    labs(x = "Anteil in Prozent", y = dat.inp[1,"Time"],
         title = plt.x.tit,
         caption = "Quelle: OECD, vierteljährliche VGR") +
    theme(plot.caption = element_text(face = "italic"))
  
  
  # > Illustration growth contribution
  oecd.qna.res <- exp.01.res

  dat.inp <- oecd.qna.res$dat.04 %>%
    subset(yq(Time) %in% seq(as.Date(oecd.qna.res$sta.dat), as.Date(oecd.qna.res$end.dat), 0.25)[-1]) %>%
    subset(SUBJECT %in% oecd.qna.res$sub.gx)

  # > Labels
  lab.inp <- c("Konsum",
               "Außenwirtschaft",
               "Investitionen")

  # > Plot growth contributions
  plt.gx <- dat.inp %>%
    ggplot() +
    geom_bar(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q"), fill = SUBJECT), position = "stack", stat = "identity", subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[-oecd.qna.res$nn])) +
    geom_point(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
    geom_line(aes(y = ObsValue, x = as.yearqtr(Time, "%Y-Q%q")), subset(dat.inp,SUBJECT %in% oecd.qna.res$sub.gx[oecd.qna.res$nn]), size = 0.5) +
    scale_fill_discrete(name = "", labels = lab.inp) +
    theme(legend.position = "bottom") +
    labs(x = "", y = "Veränderung in Prozentpunkte (QoQ)",
         title = plt.gx.tit,
         caption = "Quelle: OECD, vierteljährliche VGR") +
    theme(axis.text.x = element_text(angle = 90),
          plot.caption = element_text(face = "italic")) +  
    scale_x_yearqtr(breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat), 1)[-1],
                    minor_breaks = seq(as.yearqtr(oecd.qna.res$sta.dat), as.yearqtr(oecd.qna.res$end.dat),0.25)[-1],
                    format = "%Y-Q%q",
                    expand = c(0, 0)) +
    scale_y_continuous(breaks = seq(-20, 20, 2))
  
  ret.lis <- list(plt.x = plt.x, plt.gx = plt.gx, exp.01.res = exp.01.res, lab.inp = lab.inp)
  
  return(ret.lis)
  
}
# > Start and end date
sta.dat <- as.Date("2005-01-01") # Periods: "yyyy-mm-dd"
end.dat <- as.Date("2023-07-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01

# > Germany
loc <- c("DEU")
oecd.qna.exp.ger <- oecd_qna_exp_fun(loc, sta.dat, end.dat, plt.x.tit =  "Deutschland (Anteile)", plt.gx.tit = "Deutschland (Wachstumsbeiträge)")
# > France
loc <- c("FRA")
oecd.qna.exp.fra <- oecd_qna_exp_fun(loc, sta.dat, end.dat, plt.x.tit =  "Frankreich (Anteile)", plt.gx.tit = "Frankreich (Wachstumsbeiträge)")
# > Italy
loc <- c("ITA")
oecd.qna.exp.ita <- oecd_qna_exp_fun(loc, sta.dat, end.dat, plt.x.tit =  "Italien (Anteile)", plt.gx.tit = "Italien (Wachstumsbeiträge)")
# > USA
loc <- c("USA")
oecd.qna.exp.usa <- oecd_qna_exp_fun(loc, sta.dat, end.dat, plt.x.tit =  "Vereinigte Staaten (Anteile)", plt.gx.tit = "Vereinigte Staaten (Wachstumsbeiträge)")
# > Eurozone
loc <- c("EA20")
oecd.qna.exp.ea20 <- oecd_qna_exp_fun(loc, sta.dat, end.dat, plt.x.tit =  "Euroraum (Anteile)", plt.gx.tit = "Euroraum (Wachstumsbeiträge)")
# > United Kingdom
loc <- c("GBR")
oecd.qna.exp.gbr <- oecd_qna_exp_fun(loc, sta.dat, end.dat, plt.x.tit =  "Vereinigtes Königreich (Anteile)", plt.gx.tit = "Vereinigtes Köngreich (Wachstumsbeiträge)")


Zurück zur Startseite