QNA |>
filter(LOCATION %in% c("USA", "EA20"),
SUBJECT %in% c("B1_GE", "P31S14_S15", "P51"),
MEASURE == "VOBARSA",
FREQUENCY == "Q") |>
quarter_to_date() |>
left_join(QNA_var$LOCATION, by = "LOCATION") |>
select(SUBJECT, LOCATION, Location, date, obsValue) |>
left_join(SNA_TABLE3 |>
filter(TRANSACT == "POPNC",
MEASURE == "PER") |>
year_to_date() |>
select(LOCATION, date, POPNC_PER = obsValue),
by = c("date", "LOCATION")) |>
filter(date >= as.Date("1999-10-01"),
month(date) %% 3 == 1) |>
mutate(date = date + months(3) -days(1)) |>
mutate(Location2 = ifelse(LOCATION == "EA20", "Zone euro", "États-Unis"),
Location2 = factor(Location2, levels = c("Zone euro", "États-Unis"))) |>
mutate(Location = ifelse(LOCATION == "EA20", "Europe", "United States")) |>
select(SUBJECT,Location, Location2, LOCATION, date, obsValue, POPNC_PER) |>
group_by(SUBJECT, LOCATION) |>
mutate(POPNC_PER_i = spline(x = date, y = POPNC_PER, xout = date)$y,
obsValue = obsValue/POPNC_PER_i) |>
arrange(date) |>
mutate(obsValue = 100 * obsValue / base_value(obsValue, date, as.Date("2007-12-31"))) |>
ungroup() |>
mutate(Subject = case_when(SUBJECT == "B1_GE" ~ "PIB / habitant",
SUBJECT == "P31S14_S15" ~ "Consommation / habitant",
SUBJECT == "P51" ~ "Investissement / habitant")) |>
mutate(Subject = factor(Subject, levels = c("PIB / habitant", "Consommation / habitant", "Investissement / habitant"))) |>
ggplot() + theme_minimal() + xlab("") + ylab("1er trimestre 2008 = 100") +
geom_line(aes(x = date, y = obsValue, color = Location2)) +
geom_image(data = . %>%
filter(date == as.Date("2015-12-31")) %>%
mutate(image = paste0("../../icon/flag/round/", str_to_lower(gsub(" ", "-", Location)), ".png")),
aes(x = date, y = obsValue, image = image), asp = 1.5) +
geom_rect(data = nber_recessions |>
filter(Peak >= as.Date("1995-01-01")),
aes(xmin = Peak, xmax = Trough, ymin = 0, ymax = +Inf),
fill = 'grey', alpha = 0.5) +
scale_color_manual(values = c("#003399", "#BF0A30")) +
scale_x_date(breaks = seq(1960,2100, 5) |> paste0("-01-01") |> as.Date(),
labels = date_format("%Y")) +
theme(legend.position = "top",
legend.title = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
scale_y_log10(breaks = seq(70, 200, 2)) +
facet_wrap(~ Subject)