R/plot_reg_treemap.R

Defines functions plot_reg_treemap

Documented in plot_reg_treemap

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

#' Population group in the region  
#' 
#' @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 treemapify geom_treemap geom_treemap_text
#' @importFrom unhcrthemes theme_unhcr
#' 
#' @return plot a ggplot2 object 
#' 
#' @export

#' @examples
#' plot_reg_treemap(year = 2022,  region = "Americas")
plot_reg_treemap <- function(year = 2022,  
                             region = "Americas",
                             pop_type = c("REF", "ASY", "IDP", "OIP", "STA", "OOC")){
  
  ## Comparison of Refugees
## Filter popdata for the country report

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::group_by( Year, UNHCRBureau, Population.type.label , Population.type ) |>
  dplyr::summarise(Value = sum( Value, na.rm = TRUE))  |>
  dplyr::ungroup(Year, UNHCRBureau ,Population.type, Population.type.label)  |>
  dplyr::mutate(  freq = scales::label_percent( accuracy= .1, suffix = "%")(Value / sum(Value)) ) |>
  dplyr::filter(UNHCRBureau  == region   )   |>
  dplyr::mutate(  freqinReg = scales::label_percent( accuracy= .1, suffix = "%") (Value / sum(Value)) )  
 
## Treemapify
p <- ggplot(datatree, 
       aes(area = Value, 
           fill = Population.type, 
           label = paste0(freqinReg, "\n", Population.type.label)  )) +
  treemapify::geom_treemap() +
  treemapify::geom_treemap_text(colour = "white",
                    place = "centre", size = 15) +
 # scale_fill_viridis_c() +
  scale_fill_manual( values = c(   "IDP" = "#00B398",
                                     "OIP"="#EF4A60",
                                     "ASY" = "#18375F",
                                     "REF" = "#0072BC",
                                     "OOC" ="#8395b9",
                                     "STA"="#E1CC0D",
                                     "HCO"="#1b9e77")) +
  unhcrthemes::theme_unhcr(font_size = 22)  + ## Insert UNHCR Style
  theme(legend.position = "none") +
  ## and the chart labels
  labs(title = paste0("Population of Concern & Affected Host Communities in ", region),
        subtitle = paste0("As of   ",  year, ", a total of ", format(round(sum(datatree$Value), -3),  big.mark=","), " Individuals"),
       x = "",
       y = "",
       caption = "Source: UNHCR.org/refugee-statistics")
 
 return(p)
    
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.