R/plot_ctr_recognition.R

Defines functions plot_ctr_recognition

Documented in plot_ctr_recognition

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

#' Display a Chart with Refugee Recognition Rates 
#' 
#' In the absence of an internationally agreed methodology for calculating recognition rates,
#'  UNHCR uses two rates to compute the proportion of refugee claims accepted during the year: 
#'  
#'   * The Refugee Recognition Rate divides the number of asylum-seekers granted Convention refugee
#'   status by the total number of accepted (Convention and, where relevant, complementary 
#'   protection) and rejected cases (aka substantive decision). 
#'   
#'   * The Total Recognition Rate divides the number of asylum-seekers granted Convention
#'    refugee status and / or complementary form of protection by the total number
#'    of accepted (Convention and, where relevant, complementary protection) and
#'     rejected cases.
#'     
#' Non-substantive decisions are, to the extent possible, excluded from both 
#' calculations. For the purpose of international comparability,  UNHCR only 
#' uses these two recognition rates and does not report nationally calculated rates.
#'      
#'      See https://www.unhcr.org/4ce531e09.pdf
#' 
#' @param year Numeric value of the year  
#'              
#' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country of Asylum
#' @param top_n_countries Numeric value of number of main countries that the graph should display
#' @param measure this can be either:
#'            * RefugeeRecognitionRate 
#'            * TotalRecognitionRate
#' @param order_by this can be either:
#'            * Recognized  
#'            * ComplementaryProtection  
#'            * TotalDecided 
#'            
#' @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 guide_axis facet_wrap vars
#'             scale_fill_manual scale_x_continuous scale_x_discrete  scale_y_continuous   sym theme  
#' @importFrom utils  head
#' @importFrom tidyselect where
#' @importFrom stringr  str_replace 
#' @importFrom scales cut_short_scale label_percent label_number breaks_pretty pretty_breaks
#' @importFrom stats  reorder aggregate 
#' @importFrom dplyr  desc select  case_when lag mutate group_by filter summarise ungroup
#'               pull distinct n arrange across slice left_join
#' @importFrom tidyr pivot_longer
#' @importFrom unhcrthemes theme_unhcr  scale_fill_unhcr_d
#' 
#' @return a ggplot2 object
#' 
#' @export
#' @examples
#' plot_ctr_recognition(year = 2022,
#'                      country_asylum_iso3c = "USA",
#'                      top_n_countries = 10, 
#'                      measure = "RefugeeRecognitionRate",
#'                      order_by = "TotalDecided" )
plot_ctr_recognition <- function(year = 2022,
                                 country_asylum_iso3c  ,
                                 top_n_countries = 10, 
                                 measure = "RefugeeRecognitionRate",
                                 order_by = "TotalDecided" ){
   
   ctrylabel <- ForcedDisplacementStat::reference |> 
                 filter(iso_3 == country_asylum_iso3c ) |> 
               select(ctryname) |> 
                pull()
   
  
   measurelabel <-
     dplyr::case_when(
       measure == "RefugeeRecognitionRate"  ~ "Refugee Recognition Rate",
       measure == "TotalRecognitionRate"  ~ "Total Recognition Rate"
     )
   
   order_bylabel <-
     dplyr::case_when(
       order_by == "Recognized"  ~ "Recognized Refugee Status Decisions",
       order_by == "ComplementaryProtection"  ~ "Complementary Protection Decisions",
       order_by == "TotalDecided"  ~ "Total Decision (independently of the outcome)"
     )
   
   topOrigin <-  ForcedDisplacementStat::asylum_decisions |>
     filter(CountryAsylumCode == country_asylum_iso3c &
              Year == year) |>
     ## the below is change - DecisionsAveragePersonsPerCase- is just indicative... so no need to use it to m
     # mutate(DecisionsAveragePersonsPerCase = map_dbl(DecisionsAveragePersonsPerCase, ~replace_na(max(as.numeric(.), 1), 1))) |>
     mutate(DecisionsAveragePersonsPerCase = 1) |>
     group_by(CountryOriginName) |>
     summarize(
       Recognized = sum(Recognized * DecisionsAveragePersonsPerCase, na.rm = TRUE),
       ComplementaryProtection = sum(
         ComplementaryProtection * DecisionsAveragePersonsPerCase,
         na.rm = TRUE
       ),
       TotalDecided = sum(TotalDecided * DecisionsAveragePersonsPerCase, na.rm = TRUE)
     ) |>
     mutate(
       RefugeeRecognitionRate = (Recognized) / TotalDecided,
       TotalRecognitionRate = (Recognized + ComplementaryProtection) / TotalDecided
     ) |>
     # filter(TotalDecided  != 0) |>
     # filter(TotalDecided  > 1000)  |>
     mutate(CountryOriginName = str_replace(CountryOriginName, " \\(Bolivarian Republic of\\)", ""))

   topOrigin1 <-  topOrigin  |>
     mutate(measured = .data[[measure]])  |>
     mutate(order_by = .data[[order_by]])  |>
     arrange(desc(order_by)) |>
     head(top_n_countries)   
 
 
  
 
   rsdorigin <- ggplot() +
     geom_bar(data = topOrigin1, 
              aes(y = measured ,
                  x = reorder(CountryOriginName, measured)),
              stat = "identity", fill = "#0072bc") +
     coord_flip() +
     #scale_y_continuous( labels = scales::label_number(accuracy = 1,   scale_cut = cut_short_scale())) + ## Format axis number
     scale_y_continuous( labels =   scales::label_percent(accuracy = 0.1, suffix = "%")) +
     
     #facet_grid(.~ ctry_asy) +
     #  geom_hline(yintercept = 0, size = 1.1, colour = "#333333")   +
     labs(
       title = paste0(measurelabel, " | ", year, " in ", ctrylabel),
       caption = 'Source: UNHCR.org/refugee-statistics ',
       subtitle = paste0(
         "For top ",
         top_n_countries,
         " Countries of Origin ordered by ",
         order_bylabel
       ),
       x = " ",  y = " " ) +
     theme_unhcr(
       grid = "Y",
       axis = "x",
       axis_title = "" ,
       font_size = 14
     ) +
     theme(#axis.text.x = element_blank(),
       # legend.position = "none",
       
       panel.grid.major.x = element_line(color = "#cbcbcb"),
       panel.grid.major.y = element_blank()) ### changing grid line that should appear) 


  return(rsdorigin)

    
}
Edouard-Legoupil/unhcrdatapackage documentation built on Nov. 6, 2023, 6:10 p.m.