# WARNING - Generated by {fusen} from /dev/dev_plot_Country.Rmd: do not edit by hand
#' Increases and Decreases in Population Groups
#'
#' @param year Numeric value of the year (for instance 2020)
#' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country of Asylum
#' @param pop_type Vector of character values. Possible population type (e.g.: REF, IDP, ASY, OIP, OOC, STA)
#'
#' @importFrom ggplot2 ggplot aes coord_flip element_blank element_line
#' element_text expansion geom_bar geom_col geom_hline unit stat_summary
#' geom_label geom_text labs position_stack scale_color_manual scale_colour_manual
#' geom_text
#' scale_fill_manual scale_x_continuous scale_x_discrete scale_y_continuous sym theme
#' @importFrom utils head
#' @importFrom tidyselect where
#' @importFrom stringr str_replace
#' @importFrom scales cut_short_scale label_percent label_number breaks_pretty
#' @importFrom stats reorder aggregate
#' @importFrom dplyr desc select case_when lag mutate group_by filter summarise ungroup
#' pull distinct n arrange across slice left_join
#' @importFrom tidyr pivot_longer gather separate spread
#' @importFrom unhcrthemes theme_unhcr
#'
#' @return a ggplot2 object
#'
#' @export
#'
#' @examples
#' #
#' plot_ctr_diff_in_pop_groups(year = 2022,
#' country_asylum_iso3c = "ROU",
#' pop_type = c("REF", "ASY")
#' )
#'
plot_ctr_diff_in_pop_groups <- function(year = 2021,
country_asylum_iso3c = country_asylum_iso3c,
pop_type = pop_type
) {
# diff_perc <- function(x){
# x = as.numeric((x - lag(x))/lag(x)) + 0
#
# }
# \(x) as.numeric((x - lag(x))/lag(x)) + 0
df <- ForcedDisplacementStat::end_year_population_totals |>
filter(CountryAsylumCode != "UKN",
!is.na(CountryAsylumCode),
(Year == year-1 | Year == year), #### Parameter
CountryAsylumCode == country_asylum_iso3c #### Parameter
) |>
group_by(Year, CountryAsylumName) |>
summarise(across(where(is.numeric), sum)) |>
ungroup() |>
arrange(Year) |>
select(-c(Year)) |>
group_by(CountryAsylumName) |>
summarise(across(where(is.numeric),
list(diffabs = diff,
# diffperc = diff_perc),
diffper = \(x) as.numeric((x - lag(x))/lag(x)) + 0 ),
.names = "{.col}_{.fn}")) |>
ungroup() |>
slice(2)
df <- replace(df, is.na(df), 0)
df <- df |>
tidyr::gather(v, value, REF_diffabs:HCO_diffper) |>
tidyr::separate(v, c("population_type", "value_type"), sep = "\\_") |>
tidyr::spread(key = value_type, value = value)
df <- df |>
mutate(diffper = label_percent(accuracy = 1, trim = FALSE) (diffper))
# mutate(diffper = label_percent(accuracy = 1, trim = FALSE) (\(x) as.numeric((x - lag(x))/lag(x)) + 0 ))
df <- df |>
filter(population_type %in% pop_type)
p <- ggplot() +
geom_col(data = df,
aes(x = population_type,
y = diffabs,
fill = population_type),
width = 0.8) +
scale_fill_manual(
values = c("ASY" = "#18375F",
"REF" = "#0072BC",
"OIP" = "#EF4A60",
#"VDA" = "#EF4A60",
"OOC" = "#999999",
"IDP" = "#00B398",
"STA" = "#E1CC0D"),
drop= TRUE, limits = force, guide="none" ) +
scale_x_discrete(labels= c("ASY" = "Asylum-seekers",
"REF" = "Refugees",
#"VDA" = "Venezuelans Displaced\nAbroad", ,
"OIP" = "Other people in need\n of international protection",
"OOC" = "Others of Concern\nto UNHCR",
"IDP" = "Internally Displaced\nPersons",
"STA" = "Stateless Persons"),
drop = TRUE,
limits = force) +
## Adding labels with conditionned positions...
geom_text( data = subset(df, diffabs >= 0 & diffabs < max(diffabs) / 1.5),
aes(x = population_type,
y = diffabs,
label = label_number(accuracy = 1,
scale_cut = cut_short_scale())(diffabs)),
vjust = -0.5 , colour = "black", size = 5 ) +
geom_text( data = subset(df, diffabs >= 0 & diffabs < max(diffabs) / 1.5),
aes( x = population_type,
y = diffabs,
label = paste(intToUtf8(9650), diffper)),
vjust = -2.0 , colour = "black", size = 5 ) +
geom_text( data = subset(df, diffabs >= 0 & diffabs >= max(diffabs) / 1.5),
aes( x = population_type,
y = diffabs,
label = label_number(accuracy = 1,
scale_cut = cut_short_scale())(diffabs) ),
vjust = 2.7 , colour = "white", size = 5 ) +
geom_text( data = subset(df, diffabs >= 0 & diffabs >= max(diffabs) / 1.5),
aes(x = population_type,
y = diffabs,
label = paste(intToUtf8(9650), diffper)),
vjust = 1.2 , colour = "white", size = 5 ) +
geom_text( data = subset(df, diffabs < 0/ 1.5),
aes( x = population_type,
y = diffabs,
label = label_number(accuracy = 1,
scale_cut = cut_short_scale())(diffabs)),
vjust = 1.2, colour = "black", size = 5 ) +
geom_text( data = subset(df, diffabs < 0 / 1.5),
aes( x = population_type,
y = diffabs,
label = paste(intToUtf8(9660), diffper)),
vjust = 2.7, colour = "black", size = 5 ) +
geom_hline(yintercept = 0, color = "black") +
labs(title = paste0(df |>
distinct(CountryAsylumName) |>
pull(),
": Increases and Decreases in Population Groups | ",
year-1,
"-",
year),
subtitle = "Number of people and percentage",
caption = "Source: UNHCR.org/refugee-statistics") +
theme_unhcr(grid = FALSE, axis = "x",
axis_title = FALSE, axis_text = "x",
font_size = 14) +
theme(axis.line.x = element_line(color="white"),
axis.text.x = element_text(size=10))
# geom_text(
# data = subset(df, diffabs < 0 & diffabs <= min(diffabs) / 1.5),
# aes(
# x = population_type,
# y = diffabs,
# label = label_number(accuracy = 1,
# scale_cut = cut_short_scale())(diffabs)
# ),
# vjust = -2.0 ,
# colour = "white",
# size = 5
# ) +
# geom_text(
# data = subset(df, diffabs < 0 & diffabs <= min(diffabs) / 1.5),
# aes(
# x = population_type,
# y = diffabs,
# label = diffper
# ),
# vjust = -0.5 ,
# colour = "white",
# size = 5
# ) +
return(p) # print(p)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.