R/plot_ctr_solution_recognition.R

Defines functions plot_ctr_solution_recognition

Documented in plot_ctr_solution_recognition

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

#' Title
#' 
#' Description

#' @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
#' @param lag Number of year to used as comparison base
#' 
#' @import ForcedDisplacementStat 
#' @importFrom unhcrthemes theme_unhcr
#' @importFrom ggtext element_markdown
#' @importFrom dplyr  desc select  filter mutate group_by filter summarise ungroup
#'               pull   full_join
#' @importFrom tidyr pivot_longer  pivot_wider  
#' @importFrom ggbraid geom_braid 
#' @importFrom lubridate ymd  
#' @importFrom stringr str_wrap   
#' @importFrom scales label_number cut_short_scale    
#' 
#' @return a ggplot2 object
#' @export
#' @examples
#' plot_ctr_solution_recognition(year = 2022,
#'                   country_asylum_iso3c= "UGA",
#'                   lag = 10)
plot_ctr_solution_recognition <- function(year = 2021,
                              lag = 10,
                              country_asylum_iso3c = country_asylum_iso3c){
   ctrylabel <- ForcedDisplacementStat::reference |> 
                 filter(iso_3 == country_asylum_iso3c ) |> 
               select(ctryname) |> 
                pull()
  
  
  new_refugee_recognitions_and_refugee_like_increases <-
    ForcedDisplacementStat::asylum_decisions |>
    filter(CountryAsylumCode == country_asylum_iso3c  &
             Year > (year - lag))  |>
    mutate(Year = as.factor(Year)) |>
    group_by(Year, CountryAsylumCode) |>
    summarize (new_refugee_recognitions_and_refugee_like_increases = sum(Recognized , na.rm = TRUE))
  
  refugee_solutions_naturalisation_resettlement_and_returns  <-
    ForcedDisplacementStat::solutions_long  |>
    filter(
      CountryAsylumCode  == country_asylum_iso3c &
        Year > (year - lag) &
        Solution.type.label != "IDP returns"
    ) |>
    mutate(Year = as.factor(Year)) |>
    group_by(Year, CountryAsylumCode) |>
    summarise(refugee_solutions_naturalisation_resettlement_and_returns = sum(Value))
  
  df_raw <-
    full_join(
      refugee_solutions_naturalisation_resettlement_and_returns,
      new_refugee_recognitions_and_refugee_like_increases,
      by = c("Year", "CountryAsylumCode")
    )|>
      dplyr::mutate(Year = lubridate::ymd(Year, truncated = 2L)) 
  
  
  if( nrow(df_raw) ==  0 ){
  
    info <-  paste0("There\'s no recorded solutions or recognitions \n in ", ForcedDisplacementStat::reference |>
             dplyr::filter( iso_3 == country_asylum_iso3c) |>
             dplyr::pull(ctryname) , " for ", year)
    p <- ggplot() +  annotate(stringr::str_wrap("text", 80), 
                              x = 1, y = 1, size = 11,  
                              label = info ) +  theme_void()  
  
  } else {
    
    df_long <- df_raw |>
      dplyr::rename(new = new_refugee_recognitions_and_refugee_like_increases,
                    sol = refugee_solutions_naturalisation_resettlement_and_returns) |>
      dplyr::mutate(year = lubridate::ymd(Year, truncated = 2L)) |>
      tidyr::pivot_longer(cols = new:sol)
    
    df_wide <-
      tidyr::pivot_wider(df_long, 
                         names_from = name, 
                         values_from = value)
    
    p <-
    ggplot() +
    ggbraid::geom_braid(data = df_wide,
                        aes( x = year,
                          ymin = new,
                          ymax = sol,
                          fill = new < sol
                        ),
                        alpha = 0.3) +
    geom_line(data = df_long,
              aes(x = year, y = value, color = name),
              linewidth = .75) +
    scale_x_date() +
    scale_y_continuous(
      labels = scales::label_number(scale_cut = scales::cut_short_scale()),
      limits = c(0, NA),
      expand = expansion(c(0, 0.02))
    ) +
    scale_color_manual(values = c("#0072bc", "#00B398")) +
    scale_fill_manual(values = c("#0072bc", "#00B398")) +
    labs(title = stringr::str_wrap( "How do <span style='color:#0072bc;'>refugee recognitions<sup>a</sup></span> compare with available <span style='color:#00B398;'>solutions<sup>b</sup></span>?", 80),
         subtitle = paste0("Data  for ", ctrylabel , " as of ", year ), 
         caption = "Source: UNHCR.org/refugee-statistics \n (a) Refugee recognition refers to the legal process through which an individual is granted refugee status by a country's government.\n (b) Refugee solutions includes returns, i.e departure to country of origin, resettlement to a third country and naturalization.") +
    unhcrthemes::theme_unhcr(
      font_size = 14,
      legend = FALSE,
      axis_title = FALSE,
      grid = "Yy"
    ) +
    theme(
      ## used to display part of the title in different colors
      plot.title = ggtext::element_markdown()
    )
    
  }
  
  return(p)
    
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.