
# -> 0) set up
rm(list=ls())

library(OECD)
library(dplyr)
library(zoo)
library(ggplot2)
library(plotly)

# -> 1) load data

# load transformed data
load("../01_data/qna_dat_01.RData")

# visualize data no 1 ----

# Germany, contribution to growth
plot_ger <- qna_dat %>%
  filter(LOCATION %in% c("DEU")) %>%
  ggplot(aes(x = TIME, y = gro_con)) + 
  geom_bar(data = . %>% filter(SUBJECT %in% c("B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")),
           aes(fill = SUBJECT), 
           position = "stack", 
           stat = "identity") +
  geom_point(data = . %>% filter(SUBJECT %in% c("B1G")),
             aes(y = gro, group = SUBJECT), size = 0.5) + 
  geom_line(data = . %>% filter(SUBJECT %in% c("B1G")),
            aes(y = gro, group = SUBJECT), size = 0.5) +
  scale_fill_discrete(name = "", 
                      labels = c("B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")) +
  scale_x_yearqtr(
    breaks = seq(from = min(qna_dat$TIME), to = max(qna_dat$TIME), by = 1),
    minor_breaks = seq(from = min(qna_dat$TIME), to = max(qna_dat$TIME), by = 1/4),
    format = "%Y-Q%q",
    expand = c(0, 0)
  ) +
  scale_y_continuous(
    breaks = seq(from = -20, to = 20, by = 2),
    minor_breaks = seq(from = -20, to = 20, by = 1)
  ) +
  labs(
    title = "QNA - Output Components (growth rate (line) contribution to growth rate (bars))",
    subtitle = paste0("Last observation: ", max(qna_dat$TIME)),
    caption = "Source: OECD",
    x = "", y = "QoQ change in % (line) and contribution in %-points (bars)"
  ) +
  scale_color_discrete(name = "", 
                       labels = c("B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")) +
  theme(legend.position = "bottom")

# Framce, contribution to growth
plot_fra <- qna_dat %>%
  filter(LOCATION %in% c("FRA")) %>%
  ggplot(aes(x = TIME, y = gro_con)) + 
  geom_bar(data = . %>% filter(SUBJECT %in% c("B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")),
           aes(fill = SUBJECT), 
           position = "stack", 
           stat = "identity") +
  geom_point(data = . %>% filter(SUBJECT %in% c("B1G")),
             aes(y = gro, group = SUBJECT), size = 0.5) + 
  geom_line(data = . %>% filter(SUBJECT %in% c("B1G")),
            aes(y = gro, group = SUBJECT), size = 0.5) +
  scale_fill_discrete(name = "", 
                      labels = c("B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")) +
  scale_x_yearqtr(
    breaks = seq(from = min(qna_dat$TIME), to = max(qna_dat$TIME), by = 1),
    minor_breaks = seq(from = min(qna_dat$TIME), to = max(qna_dat$TIME), by = 1/4),
    format = "%Y-Q%q",
    expand = c(0, 0)
  ) +
  scale_y_continuous(
    breaks = seq(from = -20, to = 20, by = 2),
    minor_breaks = seq(from = -20, to = 20, by = 1)
  ) +
  labs(
    title = "QNA - Output Components (growth rate (line) contribution to growth rate (bars))",
    subtitle = paste0("Last observation: ", max(qna_dat$TIME)),
    caption = "Source: OECD",
    x = "", y = "QoQ change in % (line) and contribution in %-points (bars)"
  ) +
  scale_color_discrete(name = "", 
                       labels = c("B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")) +
  theme(legend.position = "bottom")

# visualize data no 2 ----

# Germany, contribution to growth
ggplotly(plot_ger)

# France, contribution to growth
ggplotly(plot_fra)
