dta_for_plot <- dta_for_plot_all |>
filter (metric %in% c ("trade_deficit_surplus" ) )
dta_for_plot_labels <- dta_for_plot |>
filter (year == 2023 ) |>
group_by (metric) |>
slice_max (order_by = abs (value),
n = 10 ) |>
ungroup () |>
select (country, metric) |>
mutate (country_label = country)
p1 <- dta_for_plot |>
left_join (
dta_for_plot_labels,
by = c ("country" , "metric" )
) |>
ggplot (aes (year, value, color = country, group = country)) +
geom_hline (yintercept = 0 , lty = 2 , linewidth = 0.15 , alpha = 0.5 ) +
geom_line (
linewidth = 0.5 ,
na.rm = TRUE ,
show.legend = FALSE
) +
geom_text_repel (aes (x = 2024 ,
y = if_else (year == 2023 ,
value,
NA_real_ ),
label = country_label),
size = 3 , hjust = 0 ,
direction = "y" ,
na.rm = TRUE ,
show.legend = FALSE ) +
scale_y_continuous (expand = expansion (mult = c (0.005 , 0.02 ))) +
expand_limits (x = 2030 ) +
facet_wrap (~ metric) +
labs (
x = NULL ,
y = "FJD ($M)"
)
p2 <- dta_for_plot |>
left_join (
dta_for_plot_labels,
by = c ("country" , "metric" )
) |>
ggplot (aes (year, value, color = country, group = country)) +
geom_hline (yintercept = 0 , lty = 2 , linewidth = 0.15 , alpha = 0.5 ) +
geom_smooth (
linewidth = 0.5 ,
se = FALSE , method = 'loess' , formula = 'y ~ x' ,
na.rm = TRUE ,
show.legend = FALSE
) +
geom_text_repel (aes (x = 2024 ,
y = if_else (year == 2023 ,
value,
NA_real_ ),
label = country_label),
size = 3 , hjust = 0 ,
direction = "y" ,
na.rm = TRUE ,
show.legend = FALSE ) +
scale_y_continuous (expand = expansion (mult = c (0.005 , 0.02 ))) +
expand_limits (x = 2030 ) +
facet_wrap (~ metric) +
labs (
subtitle = "Smoothed lines" ,
x = NULL ,
y = "FJD ($M)"
)
p1 / p2 +
plot_annotation (
title = "Fiji blance of trade" ,
subtitle = glue ("Top ten countries with which Fiji had largest surplus or deficit in 2023 labeled." ,
" {trade_year_min} to {trade_year_max}, excluding 2020-2022." ),
caption = my_caption_fbos
)