# Import 
indtagter_raw <-
  statgl_url("OFXREAI", lang = language) %>% 
  statgl_fetch(
    "sector"      = px_all(),
    "transaction" = 43,
    "time"        = px_all(),
    .col_code     = TRUE) %>% 
  as_tibble()
udgifter_raw <-
  statgl_url("OFXREAU", lang = language) %>% 
  statgl_fetch(
    "sector"      = px_all(),
    "transaction" = 44,
    "time"        = px_all(),
    .col_code     = TRUE) %>% 
  as_tibble()
indtagter <- 
  indtagter_raw %>% 
  mutate(transaction = transaction %>% str_remove_all("[:digit:]|[:punct:]|\\+") %>% trimws()) %>% 
  spread(2, 4)
udgifter <- 
  udgifter_raw %>% 
  mutate(transaction = transaction %>% str_remove_all("[:digit:]|[:punct:]|\\+") %>% trimws()) %>% 
  spread(2, 4)
drift <-
  indtagter %>% 
  left_join(udgifter) %>% 
  gather(transaction, value, -(1:2)) %>% 
  mutate(value = value / 10^6,
         time = time %>% make_date())
  
# Plot
drift %>%
  ggplot(aes(
    x     = time,
    y     = value,
    color = transaction
  )) +
  geom_line(size = 2) +
  facet_wrap(~ sector, scales = "free") +
  theme_statgl() + 
  scale_color_statgl() +
  labs(
    title = sdg17$figs$fig1$title[language],
    subtitle = " ",
    x = " ",
    y = sdg17$figs$fig1$y_lab[language],
    color = " ",
    caption = sdg17$figs$fig1$cap[language]
  )
 
# Import 
indtagter_raw <-
  statgl_url("OFXREAI", lang = language) %>% 
  statgl_fetch(
    "sector"      = 0,
    "transaction" = 43,
    "time"        = px_all(),
    .col_code     = TRUE) %>% 
  as_tibble()
udgifter_raw <-
  statgl_url("OFXREAU", lang = language) %>% 
  statgl_fetch(
    "sector"      = 0,
    "transaction" = 44,
    "time"        = px_all(),
    .col_code     = TRUE) %>% 
  as_tibble()
bnp_raw <-
  statgl_url("NRX02", lang = language) %>% 
  statgl_fetch(
    "units"        = "L",
    "account name" = "LBNPTOT",
    "time"         = px_all(),
    .col_code      = TRUE) %>% 
  as_tibble()
# Transform
saldo <- 
  bnp_raw %>% 
  select(3, 4) %>% 
  rename("bnp" = 2) %>% 
  left_join(udgifter_raw  %>% select(3, 4) %>% rename("expenditure" = 2)) %>% 
  left_join(indtagter_raw %>% select(3, 4) %>% rename("revenue" = 2)) %>% 
  mutate(saldo = (revenue - expenditure) / bnp * 10^-3,
         time  = time %>% make_date(),
         type  = "saldo")
# Plot
saldo %>% 
  ggplot(aes(
    x    = time,
    y    = saldo,
    fill = type
  )) +
  geom_col() +
  scale_y_continuous(labels  = scales::percent_format(
    scale = 100, 
    accuracy = 1,
    big.mark = ".",
    decimal.mark = ","
    )) +
  theme_statgl() + 
  scale_fill_statgl() +
  theme(legend.position = "None") +
  labs(
    title = sdg17$figs$fig2$title[language],
    subtitle = sdg17$figs$fig2$sub[language],
    x = " ",
    y = sdg17$figs$fig2$y_lab[language],
    color = " ",
    caption = sdg17$figs$fig2$cap[language]
  )# Import
PRXPRISV_raw <-
  statgl_url("PRXPRISV", lang = language) %>% 
  statgl_fetch(
      "commodity group" = px_all(),
      "time"            = px_all(),
    .col_code           = TRUE) %>% 
  as_tibble()
# Transform
PRXPRISV <- 
  PRXPRISV_raw %>% 
  mutate(time              = time %>% as.character() %>% readr::parse_date(format = "%Y %b"),
         `commodity group` = `commodity group` %>% factor(levels = unique(`commodity group`)))
# Plot  
cpi <-
  PRXPRISV %>% 
  ggplot(aes(
    x     = time,
    y     = value,
    color = `commodity group` 
  )) +
  geom_line(linewidth = 1) +
  theme_statgl() + 
  scale_color_statgl() +
  labs(
    title    = (statgl_url("PRXPRISV", lang = language) %>% statgl_meta())$title,
    subtitle = " ",
    x        = " ",
    y        = sdg17$figs$fig3$y_lab[language],
    color    = sdg17$figs$fig3$color[language],
    caption  = sdg17$figs$fig3$cap[language]
  )
  
plotly::ggplotly(cpi)# Import
PRXPRISH_raw <-
  statgl_url("PRXPRISH", lang = "da") %>%
  statgl_fetch(
      "time"  = px_all(),
      "type"  = 0,
    .col_code = TRUE) %>% 
  as_tibble()
time <- statgl_url("PRXPRISH", lang = "en") %>%
  statgl_fetch(
      "time"  = px_all()) %>% 
  select(time_eng = time)
fig_title <- unlist(statgl_meta(statgl_url("PRXPRISH", lang = language))$title %>% str_split(", "))[2]
fig_sub   <- unlist(statgl_meta(statgl_url("PRXPRISH", lang = language))$title %>% str_split(", "))[1]
# Transform
PRXPRISH <-
  PRXPRISH_raw %>% 
  cbind(time) %>% 
  mutate(time = time_eng %>% parse_date(format = "%Y %B")) %>% 
  select(-time_eng)
fig_title <- unlist(statgl_meta(statgl_url("PRXPRISH", lang = language))$title %>% str_split(", "))[2]
fig_sub   <- unlist(statgl_meta(statgl_url("PRXPRISH", lang = language))$title %>% str_split(", "))[1]
# Plot
PRXPRISH %>% 
  ggplot(aes(
    x     = time,
    y     = value,
    color = type
  )) +
  geom_line(size = 2) +
  scale_y_continuous(labels  = scales::percent_format(
    scale = 1, 
    accuracy = 1, 
    big.mark = ".",
    decimal.mark = ","
    )) +
  theme_statgl() + 
  scale_color_statgl() +
  theme(legend.position = "none") +
  labs(
    title    = fig_title,
    subtitle = fig_sub,
    x        = " ",
    y        = " ",
    caption  = sdg17$figs$fig4$cap[language]
  ) # Import
NRX11_raw <-
  statgl_url("NRX11", lang = language) %>% 
  statgl_fetch(
      "units"   = "K",
      "account" = px_all(),
      "time"    = px_all(),
    .col_code   = TRUE) %>% 
  as_tibble()
var <- unique(NRX11_raw[[2]])
vec <- c(var[1], var[4], var[5], var[6], var[7], var[2])
# Transform
NRX11 <-
  NRX11_raw %>% 
  filter(account %in% vec) %>% 
  mutate(account = account %>% factor(levels = unique(vec)),
         time    = time    %>% make_date()) %>% 
  arrange(account, time) %>% 
  group_by(account) %>% 
  mutate(pct = (value - lag(value)) / lag(value)) %>% 
  ungroup()
# Plot
NRX11 %>% 
  ggplot(aes(
    x     = time,
    y     = pct,
    color = account
  )) +
  geom_line(size = 2) +
  geom_hline(yintercept = 0, linetype = "dashed") + 
  facet_wrap(~ account, scales = "free") +
  scale_y_continuous(labels  = scales::percent_format(
    scale        = 100, 
    accuracy     = 1, 
    big.mark     = ".",
    decimal.mark = ","
  )) +
  theme_statgl() + 
  scale_color_statgl() +
  labs(
    title    = sdg17$figs$fig5$title[language],
    subtitle = NRX11[[1]][1],
    x        = " ",
    y        = sdg17$figs$fig5$y_lab[language],
    color    = " ",
    caption  = sdg17$figs$fig5$cap[language]
  )# Import 
NRD11_raw <- 
  statgl_url("NRX11", lang = language) %>% 
  statgl_fetch(
    units   = "L",
    account = c("0000", "3200"),
    time    = px_all(),
    .col_code   = TRUE
  ) %>% 
  as_tibble()
OFXREAI_raw <- 
  statgl_url("OFXREAI", lang = language) %>% 
  statgl_fetch(
    sector      = c(0),
    transaction = c(27),
    time        = px_all(),
    .col_code   = TRUE
  ) %>% 
  as_tibble()
# Transform
bnp <- 
  NRD11_raw %>% 
  mutate(account = account %>% factor(levels = unique(account))) %>% 
  spread(account, value) %>% 
  left_join(OFXREAI_raw %>%
              mutate(
                transaction = transaction %>% 
                  trimws() %>% 
                  str_remove_all("[:digit:]") %>% 
                  str_remove("...") %>%
                  trimws(),
                value = value / 1000
                ) %>% 
              spread(transaction, value) %>% 
              select(-1)
            )
  
labels     <- bnp %>% colnames()
vec        <- 1:length(labels)
names(vec) <- labels
bnp_relativ <-
  bnp %>% 
  rename(
    Y = 3,
    O = 4,
    B = 5
  ) %>% 
  mutate(
    O = O / Y * 100,
    B = B / Y * 100
  ) %>% rename(vec) %>% 
  select(-3) %>% 
  gather(key, value, -units, -time) %>% 
  mutate(time = time %>% as.numeric(),
         key  = key %>% factor(levels = unique(key)))
sub_lab <- 
  bnp_relativ %>% 
  select(1) %>% 
  separate(units, c("units", "drop"), ",") %>% 
  pull(units) %>% 
  unique()
# Plot
bnp_relativ %>% 
  ggplot(aes(
    x     = time,
    y     = value,
    fill  = key
  )) +
  geom_col(size = 2) +
  facet_wrap(~ key, scales = "free", ncol = 1) +
  theme_statgl() + 
  theme(legend.position = "none") +
  scale_y_continuous(labels  = scales::percent_format(
    scale = 1
  )) +
  scale_fill_statgl() +
  labs(
    title    = sdg17$figs$fig6$title[language],
    subtitle = sub_lab,
    y        = " ",
    x        = " ",
    caption  = sdg17$figs$fig6$cap[language]
  )# Import 
time  <- seq(2018, 2020)
value <- c(80, 92.6, 92.6) 
type  <- "internet"
title   <- sdg17$figs$fig7$title[language]
caption <- sdg17$figs$fig7$cap[language]
unit    <- sdg17$figs$fig7$unit[language]
# Plot
data.frame(time, value, type) %>% 
  as_tibble() %>% 
  ggplot(aes(
    x = time, 
    y = value,
    fill = type
    )) + 
  geom_col() +
  expand_limits(y = 100) +
  scale_y_continuous(labels  = scales::percent_format(
    scale        = 1,
    accuracy     = 1,
    big.mark     = ".",
    decimal.mark = ","
  )) +
  scale_fill_statgl() +
  theme_statgl() + 
  theme(legend.position = "none") +
  labs(
    title    = title,
    subtitle = unit,
    x        = " ",
    y        = " ",
    caption  = caption
  )