R/plot_reg_evolution.R

Defines functions plot_reg_evolution

Documented in plot_reg_evolution

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

#' Evolution over time 
#' 
#' Display evoluation over time for specific population group (one or many) and 
#' defined number of years (lag)
#' 
#' @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 pop_type Character value. 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 geom_line
#'             scale_fill_manual scale_x_continuous scale_x_discrete  scale_y_continuous   sym theme 
#' @importFrom dplyr  desc select  case_when lag mutate group_by filter summarise ungroup
#'               pull distinct n arrange across slice left_join
#' @importFrom stringr str_replace  
#' @importFrom scales cut_short_scale label_percent label_number breaks_pretty pretty_breaks
#' @importFrom unhcrthemes theme_unhcr unhcr_pal
#' 
#' @return a ggplot2 object
#' 
#' @export
#' @examples
#' plot_reg_evolution(year = 2021,
#'                    lag = 5,
#'                    region = "Asia",
#'                    pop_type =  c( "REF", "IDP", "ASY", "OOC", "STA", "OIP"))
plot_reg_evolution <- function(year = 2022,
                              lag = 5,
                              region,
                              pop_type =  c( "REF", "IDP", "ASY", "OOC", "STA", "OIP"
                                             #"HCO"
                                             ) ){
  
  allpop <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                              y= ForcedDisplacementStat::reference, 
                              by = c("CountryAsylumCode" = "iso_3")) |> 
              dplyr::filter(Population.type   %in%  pop_type & 
                              Year >=  (year - lag) &
                              UNHCRBureau  == region )|> 
              dplyr::group_by(Year) |> 
              dplyr::summarise(Value = sum(Value, na.rm = TRUE))
  
  p <-  ggplot(allpop) + 
    geom_line(aes(  x = Year, 
                    y = Value  ),
              size = 1,
              color = unhcr_pal(n = 1, "pal_blue")) +
  geom_text( aes(x = Year,
                 y = Value, 
                 label = label_number(scale_cut = cut_short_scale(), accuracy=1)(Value)),
             vjust = -1, 
             size = 4 ) +
  labs(
    title = paste0( region, ": People of Concern | ", (year - lag), "-", year),
    y = "Number of people",
    caption = "Source: UNHCR.org/refugee-statistics" ) +
  scale_x_continuous(breaks = pretty_breaks(n =7)) +
  scale_y_continuous(
    expand = expansion(c(0, 0.1)),
    breaks = pretty_breaks(n = 4),
    labels = scales::label_number(accuracy = 1,   scale_cut = cut_short_scale()),
    limits = c(0, 20 * 1e6)
  ) +
  theme_unhcr( grid = "Y",
               axis = "x",  axis_title = "" ,
              font_size = 14 )
  
  return(p)
    
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.