# pak::pak("hughjonesd/ggmagnify")
library(ggmagnify)
md <- max(spreads_data$date)
date_inset <- floor_date(md - months(3), unit = "month")
spreads_hyb <- spreads_data |>
mutate(
date_h = floor_date(date, unit = "month"),
date_h = if_else(date < date_inset, date_h, date)
) |>
group_by(date_h, pays) |>
summarize(
taux = mean(taux, na.rm = TRUE),
n = n(), .groups = "drop"
) |>
rename(
date = date_h
) |>
mutate(
date_label = ifelse(n > 1,
stamp(
orders = "%B %Y",
locale = "fr_FR.utf8", exact = TRUE, quiet = TRUE
)(date),
stamp("22/12/2024",
orders = "%d/%m/%Y",
locale = "fr_FR.utf8", quiet = TRUE
)(date)
),
tooltip = str_c(
"<b>", pays, "</b><br>",
date_label,
"<br>Ecart de taux avec l'Allemagne : ", f_taux(taux)
),
)
inset_plot <- ggplot(spreads_hyb) +
aes(x = date, y = taux, fill = pays, color = pays, group = pays) +
geom_line(linewidth = 0.75, alpha = 0.5, show.legend = FALSE) +
geom_point_interactive(
aes(tooltip = tooltip, data_id = date),
stroke = 0.5, size = 1,
col = "white", shape = 21, show.legend = FALSE
) +
scale_color_manual(
aesthetics = c("fill", "color"),
name = NULL, values = cc
) +
theme_ofce(
base_size = 7,
panel.grid.major.y = element_line(color = "gray", linewidth = 0.25),
axis.line = element_blank()
) +
geom_vline(xintercept = as.Date("2024-6-30"), linewidth = 0.25, color = "grey") +
labs(
y = NULL,
x = NULL,
colour = NULL,
caption = NULL
) +
scale_x_date(
labels = scales::label_date_short(format = c("%Y", "%B")),
date_breaks = "1 month"
) +
scale_y_continuous(
labels = ~ str_c(.x, "%"), breaks = seq(0, 2))
from <- list(md - days(50), md, 0, 2)
to <- list(md - years(4), md + years(1), 3.75, 5)
sh <- ggplot(spreads_hyb) +
aes(x = date, y = taux, fill = pays, color = pays, group = pays) +
geom_line(linewidth = 0.75, alpha = 0.5, show.legend = FALSE) +
geom_point_interactive(
aes(tooltip = tooltip, data_id = date),
stroke = 0.5, size = 1,
col = "white", shape = 21, show.legend = FALSE
) +
scale_color_manual(
aesthetics = c("fill", "color"),
name = NULL, values = cc
) +
theme_ofce(plot.margin = ggplot2::margin(10, 60, 10, 10)) +
guides(
x = guide_axis(minor.ticks = TRUE),
y = guide_axis(minor.ticks = TRUE)
) +
labs(
y = "Ecart de taux à 10 ans",
x = NULL,
colour = NULL,
caption = glue::glue("*Source* : investing.com<br>Mis à jour : {date_maj}")
) +
scale_ofce_date(
labels = scales::label_date_short(format = c("%Y")) ) +
scale_y_continuous(
labels = ~ str_c(.x, "%"), breaks = seq(0, 5),
minor_breaks = scales::breaks_width(0.1),
guide = "axis_minor",
expand = expansion(), limits = c(-0.25, NA),
) +
annotations +
coord_cartesian(clip = "off") +
ggmagnify::geom_magnify(
from = from, to = to, linewidth = 0.1,
colour = "grey25", shadow = TRUE,
plot = inset_plot, axes = "xy",
shadow.args = list(sigma = 5, colour = "grey80", x_offset = 5, y_offset = 5)
)
girafy(add_logo(sh), r = 2.5)