R/plot_reg_decrease.R

Defines functions plot_reg_decrease

Documented in plot_reg_decrease

# WARNING - Generated by {fusen} from /dev/dev_plot_Region.Rmd: do not edit by hand

#' Plot Biggest decrease in Refugee Population
#' 
#' 
#' @param year Numeric value of the year (for instance 2020)
#' @param region Character value with the related UNHCR bureau - when left null,
#'                it will display the whole world
#' @param lag Number of year to used as comparison base
#' @param topn how many top countries to show..
#' @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 unhcrthemes theme_unhcr
#' 
#' @return a ggplot2 object
#' 
#' @export
#' @examples
#' plot_reg_decrease(year = 2021,
#'                   lag = 5,
#'                   topn = 5,
#'                   region = "Americas",
#'                   pop_type = c("REF", "ASY", "OIP"))
plot_reg_decrease <- 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 =  baseline - thisyear   ) |>
    arrange(desc(gap)) |>
    head(topn) |>
    gather(key = Year, 
           value = Value2,
           -CountryAsylumName,
           -gap) |>
    mutate( Year = case_when(
                    Year == "thisyear" ~ paste(thisyear),
                    Year == "baseline" ~ paste(baseline) )  )
  
 p <- ggplot(data, 
       aes(x = reorder(CountryAsylumName, gap),
           y = Value2, 
          fill = as.factor(Year))) +
  
  coord_flip() +
  geom_bar(stat = "identity", position = "dodge") +
  geom_hline(yintercept = 0, size = 1, colour = "#333333") +
  scale_fill_manual(values = c("#0072bc", "#FAAB18")) +
  labs(title = "Biggest Decrease of Population",
       subtitle = paste0( topn, " Biggest change in Refugee Population, ",
                          region, " " ,  baseline," - ",thisyear), 
       x="", y ="",
       caption = "Source: UNHCR.org/refugee-statistics") + 
   ## Format axis number
  scale_y_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)
    
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.