R/plot_reg_share.R

Defines functions plot_reg_share

Documented in plot_reg_share

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

#' A simple chart to compare a population group between the Region and the rest of the world
#' 
#' Simple treemap charts to do the comparison
#' 
#' @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 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 dplyr  desc select  case_when lag mutate group_by filter summarise ungroup
#'               pull distinct n arrange across slice left_join
#' @importFrom scales label_percent
#' @importFrom stringr str_wrap
#' @importFrom treemapify geom_treemap geom_treemap_text
#' @importFrom unhcrthemes theme_unhcr scale_fill_unhcr_d
#' 
#' @return plot a ggplot2 object 
#' 
#' @export

#' @examples
#' plot_reg_share(year = 2022,  
#'                              region = "Americas",
#'                              pop_type = "REF")
#' plot_reg_share(year = 2022,  
#'                              region = "Americas",
#'                              pop_type = "ASY")
#' plot_reg_share(year = 2022,  
#'                              region = "Americas",
#'                              pop_type = "OIP")
#' plot_reg_share(year = 2022,  
#'                              region = "Americas",
#'                              pop_type = "IDP")
#' plot_reg_share(year = 2022,  
#'                              region = "Americas",
#'                              pop_type = "STA")
plot_reg_share <- function(year = 2022,  
                             region = "Americas",
                             pop_type = "REF"){
  
 pop_typelabel <-   dplyr::case_when( 
           pop_type == "REF"  ~  "Refugees",
            pop_type=="IDP"   ~ "Internally displaced persons",
            pop_type=="ASY"   ~ "Asylum seekers",
            pop_type=="OOC"   ~ "Others of concern to UNHCR",
            pop_type=="STA"   ~ "Stateless Persons",
            pop_type=="OIP"   ~ "Other people in need of international protection",
            pop_type=="HCO"   ~ "Host community" )
    
    

datatree <-   dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                                                     y= ForcedDisplacementStat::reference, 
                                                     by = c("CountryAsylumCode" = "iso_3")) |>
  dplyr::filter( Year == year  ) |>
  dplyr::filter( Population.type %in% pop_type  ) |>
 # dplyr::filter( Year == year | Year == (year -1) ) |>
  dplyr::select(Year, UNHCRBureau ,Population.type, Population.type.label, Value) |>
    dplyr::mutate(Compare = ifelse(UNHCRBureau == region, region, "Rest of the World")) |>
  dplyr::group_by( Year, Compare, Population.type.label , Population.type ) |>
  dplyr::summarise(Value = sum( Value, na.rm = TRUE))  |>
  dplyr::ungroup(Year, Compare ,Population.type, Population.type.label)  |>
  dplyr::mutate(  freq = scales::label_percent(accuracy= .1, suffix = "%")(Value / sum(Value)) ) 
 
## Treemapify
p <- ggplot(datatree, 
       aes(area = Value, 
           fill = Compare, 
           label = paste0(  format(round(datatree$Value, -3),  big.mark=",") , "\n in ", Compare )  )) +
  treemapify::geom_treemap() +
  treemapify::geom_treemap_text(colour = "white",
                    place = "centre", size = 15) +
  scale_fill_unhcr_d(palette = "pal_unhcr") +
  unhcrthemes::theme_unhcr(font_size = 12)  + ## Insert UNHCR Style
  theme(legend.position = "none") +
  ## and the chart labels
  labs(title = paste0("Share of ", pop_typelabel , " within ", region),
        subtitle = stringr::str_wrap(paste0("As of ",  year,
                          ", about ",
                          datatree |> filter( Compare == region) |> select(freq) |> pull(), 
                          " of that global population category are hosted in the region"), 80),
       x = "",
       y = "",
       caption = "Source: UNHCR.org/refugee-statistics")
 
 return(p)
    
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.