# WARNING - Generated by {fusen} from /dev/dev_plot_Region.Rmd: do not edit by hand
#' Plot Biggest Increase in Refugee Population
#'
#'
#' @param year Numeric value of the year (for instance 2020)
#' @param lag Number of year to used as comparison base
#' @param topn how many top countries to show..
#' @param region Character value with the related UNHCR bureau - when left null,
#' it will display the whole world
#' @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
#' @importFrom ggalt geom_dumbbell
#' @importFrom unhcrthemes theme_unhcr
#'
#' @return a ggplot2 object
#'
#' @export
#' @examples
#' plot_reg_increase(year = 2021,
#' lag = 5,
#' topn = 5,
#' region = "Americas",
#' pop_type = c("REF", "ASY", "OIP"))
#'
#' plot_reg_increase(year = 2021,
#' lag = 5,
#' topn = 5,
#' region = "Asia",
#' pop_type = c("REF", "ASY", "OIP"))
plot_reg_increase <- function( year = 2021,
lag = 5,
topn = 5,
region = "Americas",
pop_type = c("REF", "ASY", "OIP") ){
# library(tidyverse)
thisyear <- year
baseline <- thisyear -lag
data <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long,
y= ForcedDisplacementStat::reference,
by = c("CountryAsylumCode" = "iso_3")) |>
filter(Population.type %in% pop_type) |>
filter(Year == baseline | Year == thisyear) |>
mutate( Year = case_when(
Year == thisyear ~ paste("thisyear"),
Year == baseline ~ paste("baseline") )) |>
filter(UNHCRBureau == region) |>
group_by( CountryAsylumName, Year) |>
summarise(Value2 = sum(Value) ) |>
select(CountryAsylumName, Year, Value2) |>
mutate(CountryAsylumName = str_replace(CountryAsylumName, " \\(Bolivarian Republic of\\)", ""),
CountryAsylumName = str_replace(CountryAsylumName, "Iran \\(Islamic Republic of\\)", "Iran"),
CountryAsylumName = str_replace(CountryAsylumName, "United Kingdom of Great Britain and Northern Ireland", "UK")) |>
# mutate(Year = paste0("year_",Year )) |>
spread(Year, Value2) |>
mutate(gap = thisyear -baseline ) |>
arrange(desc(gap)) |>
head(topn)
p <- ggplot(data, aes(x = baseline ,
xend = thisyear,
y = reorder(CountryAsylumName, gap),
group = CountryAsylumName)) +
ggalt::geom_dumbbell(colour = "#dddddd",
size = 3,
colour_x = "#0072bc",
colour_xend = "#FAAB18") +
labs(title = paste0("Where did Refugee Population increased in ", region , "?"),
subtitle = paste0("Biggest increase in Refugee Population, ", baseline," - ",thisyear),
x="", y ="",
caption = "Source: UNHCR.org/refugee-statistics") +
## Format axis number
scale_x_continuous(labels = scales::label_number(accuracy = 1,
scale_cut = cut_short_scale()))+
theme_unhcr(font_size = 14) + ## Insert UNHCR Style
theme(panel.grid.major.x = element_line(color = "#cbcbcb"),
panel.grid.major.y = element_blank()) ### changing grid line that should appear
return(p)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.