R/plot_ctr_solution.R

Defines functions plot_ctr_solution

Documented in plot_ctr_solution

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

#' Plotting solutions within a country
#' 
#' As part of UNHCR standard approach, there are mostly 3 types of durable solutions for Refugees:
#'  - Voluntary Repatriation in origin country 
#'  - Naturalization in the host country
#'  - Resettlement in a third country 
#'
#' @param year Numeric value of the year (for instance 2020)
#' @param lag Number of year to used as comparison base
#' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country of Asylum
#' @param sol_type Vector of character values. Possible population type (e.g.:"NAT" "RST" "RET" "RDP")
#' 
#' @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
#' @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
#' 
#' @return a ggplot2 object
#' 
#' @export
#'
 
#' @examples
#' plot_ctr_solution(year = 2021,
#'                   country_asylum_iso3c= "UGA",
#'                   lag = 10,
#'                   sol_type = c("NAT", "RST", "RET", "RDP"))
plot_ctr_solution <- function(year = 2021,
                              lag = 10,
                              country_asylum_iso3c = country_asylum_iso3c,
                              sol_type ){
  
    ctrylabel <- ForcedDisplacementStat::reference |> 
                 filter(iso_3 == country_asylum_iso3c ) |> 
               select(ctryname) |> 
                pull()
  
  
    Solution <- ForcedDisplacementStat::solutions_long  |>
                dplyr::left_join(y= ForcedDisplacementStat::reference, 
                               by = c("CountryAsylumCode" = "iso_3")) |>
      
              filter(CountryAsylumCode  == country_asylum_iso3c & 
                      Year > (year - lag) &
                      Solution.type  %in% sol_type 
                      #Solution.type.label != "IDP returns" 
                      ) |>
              mutate(Year = as.factor(Year) ) |>
              group_by(Year, Solution.type.label ) |>
              summarise(Value2 = sum(Value) )  |>
              mutate( valabel =  ifelse(Value2 > 1000, 
                                       paste(scales::label_number(accuracy = 0.1,
                                                      scale_cut = cut_short_scale())(Value2)),
                                       as.character(Value2)) )   

    
    
    ncat <- ifelse( nlevels(as.factor(Solution$Solution.type.label)) %in% c(2,4), 2,3)
    #levels(as.factor(solutions_long.asy$Solution.type.label))
    Solution$Solution.type.label <- factor(Solution$Solution.type.label,
                                                     levels = c("Naturalisation", 
                                                                "Resettlement arrivals",
                                                                "Refugee returns",
                                                                "IDP returns" )) 



if( nrow(Solution) ==  0 ){
  
    info <-  paste0("There\'s no recorded solutions \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 {
  

#Make plot
p <- ggplot(Solution, aes(x = Year, y = Value2  )) + 
  geom_bar(stat = "identity", 
           position = "identity", 
           fill = "#0072bc" ) + # here we configure that it will be bar chart
 # scale_y_continuous( labels = scales::label_number(accuracy = 1,   scale_cut = cut_short_scale())) + ## Format axis number
  
  scale_y_continuous( labels = scales::label_number(accuracy = 1,   scale_cut = cut_short_scale()))+

  scale_x_discrete(guide = guide_axis(check.overlap = TRUE))+
  #xlim(c(year-lag, year +1)) +
  facet_wrap( vars(Solution.type.label ), ncol = ncat) +
  #geom_hline(yintercept = 0, size = 1.1, colour = "#333333") +
  theme_unhcr(font_size = 14)  + ## Insert UNHCR Style
  theme(panel.grid.major.y  = element_line(color = "#cbcbcb"), 
        panel.grid.major.x  = element_blank(), 
        panel.grid.minor = element_blank()) + ### changing grid line that should appear
  ## and the chart labels
  labs(title = "What are the trends in terms of Solutions?",
       subtitle = paste0("Data for Forcibly Displaced People across Borders as of ", year, " filtered for ", ctrylabel ), 
       x = "",
       y = "",
       caption = "Source: UNHCR.org/refugee-statistics.\n Resettlement – in this context this is the country of arrival – i.e. the country to which a refugee has been resettled \n Returns – in this context this is the country of departure – i.e. the country from which a refugee has voluntarily repatriated.")

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