R/plot_ctr_keyfig.R

Defines functions plot_ctr_keyfig

Documented in plot_ctr_keyfig

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

#' Summary Key Figures
#' 
#'  Key figures can be highlighted with humanitarian icons
#'   https://fontawesome.com/icons/categories/humanitarian  
#' @param year Numeric value of the year (for instance 2020)
#' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country of Asylum
#' @return ggplot2
#' 
#' @importFrom dplyr  desc select  case_when lag mutate group_by filter summarise ungroup
#'               pull distinct n arrange across slice left_join
#' @importFrom ggtext geom_textbox
#' @importFrom unhcrthemes theme_unhcr
#' @importFrom extrafont fonttable font_import
#' @importFrom sysfonts font_add
#' @importFrom utils download.file
#' 
#' @export

#' @examples
#' plot_ctr_keyfig(year = 2022,
#'                 country_asylum_iso3c = "COL")
plot_ctr_keyfig <- function(country_asylum_iso3c, year){

CountryAsylum_name_text <-  ForcedDisplacementStat::reference |> 
  dplyr::filter(iso_3 == country_asylum_iso3c ) |> 
  dplyr::distinct(ctryname) |> 
  dplyr::pull()

total_poc <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

total_poc_last_year <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == as.numeric(year)-1,
                CountryAsylumCode == country_asylum_iso3c) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

perc_change_poc <- ((total_poc - total_poc_last_year)/total_poc_last_year)*100

total_ref <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c,
                Population.type == "REF"
  ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

total_asy <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c,
                Population.type == "ASY"
  ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

total_oip <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c,
                Population.type == "OIP"
  ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

total_idp <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c,
                Population.type == "IDP"
  ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

total_sta <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c,
                Population.type == "STA"
  ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

total_ooc <- ForcedDisplacementStat::end_year_population_totals_long |> 
  dplyr::filter(Year == year,
                CountryAsylumCode == country_asylum_iso3c,
                Population.type == "OOC"
  ) |> 
  dplyr::summarise(sum(Value, na.rm = TRUE)) |> 
  dplyr::pull()

## Create the frame to be displayed with a facetted ggplot  
keyfig <- data.frame(
  ## order the factor for correct display
  fig = factor( c("Refugees", "Asylum-seekers", "Other in Need in International Protection",
               "Internally Displaced People", "Stateless People", "Others of Concerns"),
               levels = c("Refugees",
                          "Asylum-seekers",
                          "Other in Need in International Protection",
                          "Internally Displaced People",
                          "Stateless People",
                          "Others of Concerns")),  
  pal = c("REF", "ASY",  "OIP", "IDP",  "STA", "OOC"),
  icon = c("\ue4fb", # "Refugees" fa-hands-holding-circle
           "\ue553", # "Asylum-seekers" fa-person-walking-dashed-line-arrow-right
           "\ue54d", # "Other in Need in International Protection"fa-person-rays
           "\ue552", #  "Internally displaced people" fa-person-walking-arrow-right
           "\ue4b9", # "Stateless People" fa-arrows-down-to-people
           "\ue539" #  "Others of Concerns"  fa-person-arrow-up-from-line
           ),
  label = c(
  paste0( format(round(total_ref, 0), scientific = FALSE, big.mark=",") ),
  paste0( format(round(total_asy, 0),scientific = FALSE,  big.mark=",") ),
  paste0( format(round(total_oip, 0), scientific = FALSE, big.mark=",")), 
  paste0( format(round(total_idp, 0), scientific = FALSE, big.mark=",")),
  paste0( format(round(total_sta, 0),scientific = FALSE,  big.mark=",")), 
  paste0( format(round(total_ooc, 0), scientific = FALSE, big.mark=",") ) ) 
  )
  
 
 p <- ggplot(keyfig) +  
  ggtext::geom_textbox(aes(x = 0.3, 
                           y = 1,  
                 label = icon ,
                 color =  pal), 
                family = "Font Awesome 6 Free Solid",
                box.colour = NA , 
                hjust = 0, 
                vjust = 0,
               size = 13) +
  ggtext::geom_textbox(aes(x =  0.3, 
                           y =  1, 
                           label = label ,
                           color =  pal), 
                       family = "Lato", 
                       hjust = -0.5, 
                       vjust = 0,
                       size = 12,
                      # width = unit(25, "cm"), 
                       box.colour = NA) +
  scale_color_manual( values = c(   "IDP" = "#00B398",
                                   "OIP"="#EF4A60",
                                   "ASY" = "#18375F",
                                   "REF" = "#0072BC",
                                   "OOC" ="#8395b9",
                                   "STA"="#E1CC0D")) +
  xlim(c(0, 2)) +
  ylim(c(0, 3)) +
  facet_wrap(vars(fig), ncol = 2)  +
  labs(title = paste0("Key Figures for ", CountryAsylum_name_text, " as of ", year), 
       subtitle = paste0("A total of ",
                         format(round(total_poc, 0), scientific = FALSE, big.mark=","),
                         " people, ",
                        ifelse(perc_change_poc>0,"increasing", "decreasing" ) , 
                         " by ",  round(abs(perc_change_poc), 1), 
                         "% compared to the previous year"),
       caption = "Source: UNHCR.org/refugee-statistics") +
  unhcrthemes::theme_unhcr_map(  ) +
  theme(strip.text.x = element_text(size = 14),
        legend.position = "none"
        )  
  return(p)
  
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.