knitr::opts_chunk$set(
                      comment = "#>",
                      message=FALSE, 
                      warning=FALSE,
                      #fig.retina = 2, 
                      fig.width = 8,
                      fig.asp = 0.718,
                     # fig.align = "center",
                     # dev = "ragg_png",
                      out.width = "90%"
)
unlink(".RData")
library(testthat)
library(ggplot2)
library(tidyverse)
library(scales)
library(unhcrthemes)
# library(extrafont) 
# font_import()
# loadfonts()
# font_install('fontcm')
# Load already included functions if relevant
pkgload::load_all(export_all = FALSE)

Population group in the region

#' 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

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)

}
plot_reg_treemap(year = 2022,  region = "Americas")
#test_that("plot_reg_treemap works", {   expect_true(inherits(plot_reg_treemap, "function")) })

Plot World Comparison

#' A simple chart to compare a population group between the Region and the rest of the world
#' 
#' Simple treemap charts to do the comparison
#' 
#' @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 stringr str_wrap
#' @importFrom treemapify geom_treemap geom_treemap_text
#' @importFrom unhcrthemes theme_unhcr scale_fill_unhcr_d
#' 
#' @return plot a ggplot2 object 
#' 
#' @export

plot_reg_share <- function(year = 2022,  
                             region = "Americas",
                             pop_type = "REF"){

 pop_typelabel <-   dplyr::case_when( 
           pop_type == "REF"  ~  "Refugees",
            pop_type=="IDP"   ~ "Internally displaced persons",
            pop_type=="ASY"   ~ "Asylum seekers",
            pop_type=="OOC"   ~ "Others of concern to UNHCR",
            pop_type=="STA"   ~ "Stateless Persons",
            pop_type=="OIP"   ~ "Other people in need of international protection",
            pop_type=="HCO"   ~ "Host community" )



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::mutate(Compare = ifelse(UNHCRBureau == region, region, "Rest of the World")) |>
  dplyr::group_by( Year, Compare, Population.type.label , Population.type ) |>
  dplyr::summarise(Value = sum( Value, na.rm = TRUE))  |>
  dplyr::ungroup(Year, Compare ,Population.type, Population.type.label)  |>
  dplyr::mutate(  freq = scales::label_percent(accuracy= .1, suffix = "%")(Value / sum(Value)) ) 

## Treemapify
p <- ggplot(datatree, 
       aes(area = Value, 
           fill = Compare, 
           label = paste0(  format(round(datatree$Value, -3),  big.mark=",") , "\n in ", Compare )  )) +
  treemapify::geom_treemap() +
  treemapify::geom_treemap_text(colour = "white",
                    place = "centre", size = 15) +
  scale_fill_unhcr_d(palette = "pal_unhcr") +
  unhcrthemes::theme_unhcr(font_size = 12)  + ## Insert UNHCR Style
  theme(legend.position = "none") +
  ## and the chart labels
  labs(title = paste0("Share of ", pop_typelabel , " within ", region),
        subtitle = stringr::str_wrap(paste0("As of ",  year,
                          ", about ",
                          datatree |> filter( Compare == region) |> select(freq) |> pull(), 
                          " of that global population category are hosted in the region"), 80),
       x = "",
       y = "",
       caption = "Source: UNHCR.org/refugee-statistics")

 return(p)

}
plot_reg_share(year = 2022,  
                             region = "Americas",
                             pop_type = "REF")
plot_reg_share(year = 2022,  
                             region = "Americas",
                             pop_type = "ASY")
plot_reg_share(year = 2022,  
                             region = "Americas",
                             pop_type = "OIP")
plot_reg_share(year = 2022,  
                             region = "Americas",
                             pop_type = "IDP")
plot_reg_share(year = 2022,  
                             region = "Americas",
                             pop_type = "STA")
# test_that("plot_reg_share works", {   expect_true(inherits(plot_reg_share, "function")) })

Evolution Over Time

#' Evolution over time 
#' 
#' Display evoluation over time for specific population group (one or many) and 
#' defined number of years (lag)
#' 
#' @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 lag Number of year to used as comparison base 
#' @param pop_type Character value. 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 geom_line
#'             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 stringr str_replace  
#' @importFrom scales cut_short_scale label_percent label_number breaks_pretty pretty_breaks
#' @importFrom unhcrthemes theme_unhcr unhcr_pal
#' 
#' @return a ggplot2 object
#' 
#' @export
plot_reg_evolution <- function(year = 2022,
                              lag = 5,
                              region,
                              pop_type =  c( "REF", "IDP", "ASY", "OOC", "STA", "OIP"
                                             #"HCO"
                                             ) ){

  allpop <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                              y= ForcedDisplacementStat::reference, 
                              by = c("CountryAsylumCode" = "iso_3")) |> 
              dplyr::filter(Population.type   %in%  pop_type & 
                              Year >=  (year - lag) &
                              UNHCRBureau  == region )|> 
              dplyr::group_by(Year) |> 
              dplyr::summarise(Value = sum(Value, na.rm = TRUE))

  p <-  ggplot(allpop) + 
    geom_line(aes(  x = Year, 
                    y = Value  ),
              size = 1,
              color = unhcr_pal(n = 1, "pal_blue")) +
  geom_text( aes(x = Year,
                 y = Value, 
                 label = label_number(scale_cut = cut_short_scale(), accuracy=1)(Value)),
             vjust = -1, 
             size = 4 ) +
  labs(
    title = paste0( region, ": People of Concern | ", (year - lag), "-", year),
    y = "Number of people",
    caption = "Source: UNHCR.org/refugee-statistics" ) +
  scale_x_continuous(breaks = pretty_breaks(n =7)) +
  scale_y_continuous(
    expand = expansion(c(0, 0.1)),
    breaks = pretty_breaks(n = 4),
    labels = scales::label_number(accuracy = 1,   scale_cut = cut_short_scale()),
    limits = c(0, 20 * 1e6)
  ) +
  theme_unhcr( grid = "Y",
               axis = "x",  axis_title = "" ,
              font_size = 14 )

  return(p)

}
plot_reg_evolution(year = 2021,
                   lag = 5,
                   region = "Asia",
                   pop_type =  c( "REF", "IDP", "ASY", "OOC", "STA", "OIP"))
t# est_that("plot_reg_evolution works", {  expect_true(inherits(plot_reg_evolution, "function")) })

Plot Evolution of Population type per year

#' Display evolution of different population type over years
#' 
#' Summary of Evolution
#'  
#' @param year Numeric value of the year (for instance 2020)
#' @param lag Number of year to used as comparison base
#' @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, 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 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
#'
#'

plot_reg_population_type_per_year <- function(year = 2022,
                                              lag = 5,
                                     region,
                                     pop_type =  c("REF", "ASY", "IDP", "OIP", "STA", "OOC") ) {


  df <- ForcedDisplacementStat::end_year_population_totals_long  |> 
        dplyr::left_join( ForcedDisplacementStat::reference |> 
                      dplyr::select(coa_region = `UNHCRBureau`, iso_3), 
                      by = c("CountryAsylumCode" = "iso_3")) |> 
  dplyr::filter(coa_region == region &
               Year >= (year - lag)  & 
              Population.type %in% pop_type ) |>


    group_by(Year, coa_region, Population.type, Population.type.label)  |> 
    summarise(Value = sum(Value, na.rm = TRUE)) |> 
    ungroup()



  year_breaks <- diff(range(df$Year)) + 1 

  p <- ggplot(df) +
    geom_col(aes(x = Year, y = Value, fill = Population.type.label ),
             width = 0.7) +
    # geom_text(aes(x = Year, 
    #               y = Value, 
    #               color = Population.type.label, 
    #               label = label_number(accuracy = 1,
    #                                scale_cut = cut_short_scale())(Value)),
    #           position = position_stack(vjust = 0.5),
    #           show.legend = FALSE,
    #           size = 5) +

     scale_fill_manual( values = c(   "Internally displaced persons" = "#00B398",
                                     "Other people in need of international protection"="#EF4A60",
                                     "Asylum seekers"= "#18375F",
                                     "Refugees" = "#0072BC",
                                     "Others of concern to UNHCR" ="#8395b9",
                                     "Stateless Persons"="#E1CC0D",
                                     "Host community"="#1b9e77")) +
    scale_x_continuous(breaks = scales::breaks_pretty(n = year_breaks)) +
     scale_y_continuous(expand = expansion(c(0, 0.1))) +
    labs(title = paste0(region, ": Population type per year"), 
         subtitle = "Number of people (thousand)",
         caption = "Source: UNHCR.org/refugee-statistics") +
    theme_unhcr(grid = FALSE, axis = "x", 
                axis_title = FALSE, axis_text = "x",
              font_size = 14) +
    stat_summary(fun = sum, aes(x = Year,
                                y = Value,
                                label = scales::label_number(accuracy = 1,
                                                       scale_cut = cut_short_scale())(..y..),
                                group = Year),
                 geom = "text", size = 5,
                 vjust = -0.5) +
    theme(legend.direction = "vertical",
          legend.key.size = unit(0.8, 'cm'),
          text = element_text(size = 20),
          plot.subtitle=element_text(size=19),
          plot.title = element_text(size=23),
          plot.caption = element_text(size=13))

  return(p) # print(p)    
}
plot_reg_population_type_per_year(year = 2022,
                                              lag = 5,
                                     region  = "Americas",
                                     pop_type =   c("REF", "ASY", "IDP", "OIP", "STA", "OOC") )
# test_that("plot_reg_population_type_per_year works", {   expect_true(inherits(plot_reg_population_type_per_year, "function")) })

Plot Population Origin-Destination within the region

#' Plot Population Origin-Destination within the region 
#' 
#' Chord diagram showing Origin destination see - https://jokergoo.github.io/circlize_book/book/
#' 

#' @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
#' 
#' @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 summarize
#'               
#' @importFrom tidyr replace_na
#' @importFrom graphics title 
#' @importFrom circlize chordDiagram circos.track circos.text CELL_META
#' @importFrom unhcrthemes theme_unhcr
#' 
#' @return plot a ggplot2 object 
#' 
#' @export
plot_reg_origin_dest <- function(year = 2022,  region = "Americas"){



chords <- ForcedDisplacementStat::end_year_population_totals |>
  dplyr::left_join( ForcedDisplacementStat::reference |> 
                      dplyr::select(coa_region = `UNHCRBureau`, iso_3),  by = c("CountryAsylumCode" = "iso_3")) |> 
  dplyr::filter(coa_region == region &
           Year == year) |> 
  dplyr::mutate(across(REF:OIP, ~ tidyr::replace_na(as.numeric(.), 0)),
         total = REF + ASY + OIP, #+ IDP  + STA + OOC,
         # Lump together factor levels into "other"
         CountryAsylumName = forcats::fct_lump_prop(CountryAsylumName, prop = .02, w = total),
         CountryOriginName = forcats::fct_lump_prop(CountryOriginName, prop = .02, w = total)) |> 
   dplyr::group_by(CountryOriginName, CountryAsylumName) |>
   dplyr::summarize(total = sum(total), .groups = "drop")  |>
# CountryOriginName = fct_recode(CountryOriginName, "Other" = "China")
  dplyr::mutate(CountryOriginName = stringr::str_replace(CountryOriginName, " \\(Bolivarian Republic of\\)", ""),
        CountryAsylumName = stringr::str_replace(CountryAsylumName, " \\(Bolivarian Republic of\\)", ""),
        CountryOriginName = stringr::str_replace(CountryOriginName, " \\(Plurinational State of\\)", ""),
        CountryAsylumName = stringr::str_replace(CountryAsylumName, " \\(Plurinational State of\\)", ""),
        CountryOriginName = stringr::str_replace(CountryOriginName, "United States of America", "USA"),
        CountryAsylumName = stringr::str_replace(CountryAsylumName, "United States of America", "USA"))

circlize::chordDiagram(chords,
                       self.link = 1,
                      # grid.col = colorRampPalette(RColorBrewer::brewer.pal(11, "Paired"))(15),
                       annotationTrack = "grid" ,
                     # preAllocateTracks = list(track.height = max(strwidth(unlist(dimnames(chords))))),
                      preAllocateTracks = 1.6
)

circlize::circos.track(track.index = 1,
                        panel.fun = function(x, y) {
                             circlize::circos.text(circlize::CELL_META$xcenter,
                                                   circlize::CELL_META$ylim[1],
                                                   circlize::CELL_META$sector.index,
                                                   facing = "clockwise",
                                                   niceFacing = TRUE,
                                                   adj = c(0, 0.5))
                           },
                           bg.border = NA) # here set bg.border to NA is important


title(main = "Movement of Forcibly Displaced Population", 
      sub = paste0("In ", region, " as of ", year),
      cex.main = 1.5)


     return(invisible(NULL))


}
plot_reg_origin_dest(year = 2022,  region = "Asia")
#test_that("plot_reg_origin_dest works", {   expect_true(inherits(plot_reg_origin_dest, "function")) })

Plot Main country of Asylum in the Region

#' Main country of origin - Absolute value
#'
#' @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 top_n_countries Numeric value of number of main countries that the graph should display
#' 
#' @param pop_type Character value. Possible population type (e.g.: REF, IDP, ASY,   OIP, OOC, STA)
#' 
#' @param show_diff_label logical to indicate whether or not adding the the label displaying difference in percentage compared to the previous year
#' 
#' @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 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
#'

plot_reg_population_type_abs <- function(year = 2022,
                                         region = "Americas",
                                         top_n_countries = 9,
                                         pop_type = "REF",
                                         show_diff_label = TRUE) {



  cols_poptype <- list(ASY = c("Asylum seekers", "#18375F"),
                       REF = c("Refugees", "#0072BC"),
                       #VDA = c("Venezuelans Displaced Abroad", "#EF4A60"), 
                       OIP = c("Other people in need of international protection", "#EF4A60"), 
                       OOC = c("Others of Concern to UNHCR", "#999999"),
                       IDP = c("Internally displaced persons", "#00B398"),
                       STA = c("Stateless Persons", "#E1CC0D")
  )

  labelcat <- dplyr::case_when( pop_type == "ASY"  ~ "Asylum seekers", 
                                pop_type == "REF"  ~ "Refugees",
                                pop_type == "OIP"  ~ "Other people in need of international protection", 
                                pop_type == "OOC"  ~ "Others of Concern to UNHCR", 
                                pop_type == "IDP"  ~ "Internally displaced persons", 
                                pop_type == "STA"  ~ "Stateless Persons")


  colorlab <- dplyr::case_when( pop_type == "ASY"  ~ "#18375F", 
                                pop_type == "REF"  ~ "#0072BC",
                                pop_type == "OIP"  ~ "#EF4A60", 
                                pop_type == "OOC"  ~ "#999999", 
                                pop_type == "IDP"  ~ "#00B398", 
                                pop_type == "STA"  ~ "#E1CC0D")


  df <- ForcedDisplacementStat::end_year_population_totals_long |>
    dplyr::left_join(
      ForcedDisplacementStat::reference |>
        dplyr::select(coa_region = `UNHCRBureau`, iso_3),
      by = c("CountryAsylumCode" = "iso_3")
    ) |>
    dplyr::filter(coa_region == region &
                    (Year == year | Year == year - 1) &
                    Population.type %in% pop_type) |>
    dplyr::select(Year, CountryAsylumName, Value) |>
    dplyr::group_by(Year, CountryAsylumName) |>
    dplyr::summarise(Value = sum(Value, na.rm = TRUE)) |>
    dplyr::ungroup() |>
    dplyr::group_by(CountryAsylumName) |>
    dplyr::arrange(Year) |>
    dplyr::mutate(diff_pop_type_value = scales::label_percent(accuracy = 0.1,
                                                              trim = FALSE) (((Value - lag(
                                                                Value
                                                              )) / lag(Value)))) |>
    dplyr::ungroup() |>
    dplyr::filter(Year == year) |>
    dplyr::arrange(desc(Value))  |>
    head(top_n_countries)


  p <-  ggplot(df) +
    geom_col(aes(x = reorder(CountryAsylumName, Value),
                 y = Value),
             fill = colorlab ,
             width = 0.8) +
    coord_flip() +
    ## Position label differently in the bar in white - outside bar in black
    geom_text(
      data = subset(df, Value < max(Value) / 1.5),
      aes(
        y = Value,
        x = reorder(CountryAsylumName, Value),
        label = label_number(accuracy = 1,
                             scale_cut = cut_short_scale())(Value)
      ),
      hjust = -0.1 ,
      vjust = 0.5,
      colour = "black",
      size = 6
    ) +
    geom_text(
      data = subset(df, Value >= max(Value) / 1.5),
      aes(
        y = Value,
        x = reorder(CountryAsylumName, Value),
        label = label_number(accuracy = 1,
                             scale_cut = cut_short_scale())(Value)
      ),
      hjust = 1.1 ,
      vjust = 0.5,
      colour = "white",
      size = 6
    )


  # Add diff labels
  if(show_diff_label == TRUE) {
    # diff label positive general
    p <- p + geom_text( 
      data = subset(df, with(df, grepl('^[0-9]', diff_pop_type_value)) & 
                      (Value < max(Value) / 1.5)),
      aes(
        y = Value,
        x = reorder(CountryAsylumName, Value),
        label = paste(intToUtf8(9650), diff_pop_type_value)
      ),
      hjust = -1.2,
      vjust = 0.5,
      colour = "grey",
      size = 5
    ) +
      # diff label negative general
      geom_text(
        data = subset(df, with(df, grepl('-', diff_pop_type_value)) &
                        (Value < max(Value) / 1.5)),
        aes(
          y = Value,
          x = reorder(CountryAsylumName, Value),
          label = paste(intToUtf8(9660), diff_pop_type_value)
        ),
        hjust = -1.2,
        vjust = 0.5,
        colour = "#0472bc",
        size = 5
      ) +
      # diff label positive max
      geom_text(
        data = subset(df, with(df, grepl('^[0-9]', diff_pop_type_value)) & 
                        (Value >= max(Value) / 1.5)),
        aes(
          y = Value,
          x = reorder(CountryAsylumName, Value),
          label = paste(intToUtf8(9650), diff_pop_type_value)
        ),
        hjust = -0.4,
        vjust = 0.5,
        colour = "grey",
        size = 5
      ) +
      # diff label negative max
      geom_text(
        data = subset(df, with(df, grepl('-', diff_pop_type_value)) & 
                        (Value >= max(Value) / 1.5)),
        aes(
          y = Value,
          x = reorder(CountryAsylumName, Value),
          label = paste(intToUtf8(9660), diff_pop_type_value)
        ),
        hjust = -0.4,
        vjust = 0.5,
        colour = "#0472bc",
        size = 5
      )}

  p <- p +
    labs(
      title = paste0("Main Host Countries for ", labelcat ,
                     " | ",  year, " in ", region),
      subtitle = paste0(
        "Number of people for top ",
        top_n_countries,
        " countries in the region"
      ),
      caption = "Source: UNHCR.org/refugee-statistics"
    ) +
    scale_y_continuous(expand = expansion(c(0, 0.1))) +
    theme_unhcr(
      font_size = 14,
      grid = FALSE,
      axis = "y",
      axis_title = FALSE,
      axis_text = "y"
    ) +
    theme(
      legend.direction = "vertical",
      legend.key.size = unit(0.8, 'cm'),
      text = element_text(size = 20),
      plot.subtitle = element_text(size = 19),
      plot.title = element_text(size = 23),
      plot.caption = element_text(size = 13)
    )

  return(p)
}
plot_reg_population_type_abs(year = 2022,
                              region = "Americas",
                              top_n_countries = 5,
                              pop_type = "REF",
                              show_diff_label = TRUE
                              )

plot_reg_population_type_abs(year = 2022,
                              region = "Americas",
                              top_n_countries = 5,
                              pop_type = "ASY",
                              show_diff_label = FALSE
                              )

Plot Biggest decrease in Refugee Population

#' Plot Biggest decrease in Refugee Population
#' 
#' 
#' @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 lag Number of year to used as comparison base
#' @param topn how many top countries to show..
#' @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 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
plot_reg_decrease <- function(year = 2021,
                              lag = 5,
                              topn = 5,
                              region = "Americas",
                              pop_type =  c("REF", "ASY", "OIP") ){

  # library(tidyverse)

  thisyear <- year
  baseline <- thisyear -lag
  data <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                                                     y= ForcedDisplacementStat::reference, 
                                                     by = c("CountryAsylumCode" = "iso_3")) |>
    filter(Population.type  %in% pop_type)  |>
    filter(Year == baseline | Year == thisyear) |>
    mutate( Year = case_when(
      Year == thisyear ~ paste("thisyear"),
      Year == baseline ~ paste("baseline") )) |>
    filter(UNHCRBureau == region) |>
    group_by( CountryAsylumName, Year) |>
    summarise(Value2 = sum(Value) )  |>
    select(CountryAsylumName, Year, Value2) |> 
  mutate(CountryAsylumName = str_replace(CountryAsylumName, " \\(Bolivarian Republic of\\)", ""),
        CountryAsylumName = str_replace(CountryAsylumName, "Iran \\(Islamic Republic of\\)", "Iran"),
        CountryAsylumName = str_replace(CountryAsylumName, "United Kingdom of Great Britain and Northern Ireland", "UK")) |> 
   # mutate(Year = paste0("year_",Year )) |>
    spread(Year, Value2) |>
    mutate(gap =  baseline - thisyear   ) |>
    arrange(desc(gap)) |>
    head(topn) |>
    gather(key = Year, 
           value = Value2,
           -CountryAsylumName,
           -gap) |>
    mutate( Year = case_when(
                    Year == "thisyear" ~ paste(thisyear),
                    Year == "baseline" ~ paste(baseline) )  )

 p <- ggplot(data, 
       aes(x = reorder(CountryAsylumName, gap),
           y = Value2, 
          fill = as.factor(Year))) +

  coord_flip() +
  geom_bar(stat = "identity", position = "dodge") +
  geom_hline(yintercept = 0, size = 1, colour = "#333333") +
  scale_fill_manual(values = c("#0072bc", "#FAAB18")) +
  labs(title = "Biggest Decrease of Population",
       subtitle = paste0( topn, " Biggest change in Refugee Population, ",
                          region, " " ,  baseline," - ",thisyear), 
       x="", y ="",
       caption = "Source: UNHCR.org/refugee-statistics") + 
   ## Format axis number
  scale_y_continuous( labels = scales::label_number(accuracy = 1, 
                                                    scale_cut = cut_short_scale()))+
  theme_unhcr(font_size = 14)  + ## Insert UNHCR Style
  theme(panel.grid.major.x = element_line(color = "#cbcbcb"), 
        panel.grid.major.y = element_blank()) ### changing grid line that should appear

  return(p)

}
plot_reg_decrease(year = 2021,
                  lag = 5,
                  topn = 5,
                  region = "Americas",
                  pop_type = c("REF", "ASY", "OIP"))
#test_that("plot_reg_decrease works", {   expect_true(inherits(plot_reg_decrease, "function")) })

Plot Biggest increase in Refugee Population

#' Plot Biggest Increase in Refugee Population
#' 
#' 

#' @param year Numeric value of the year (for instance 2020)
#' @param lag Number of year to used as comparison base
#' @param topn how many top countries to show..
#' @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 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 ggalt geom_dumbbell
#' @importFrom unhcrthemes theme_unhcr
#' 
#' @return a ggplot2 object
#' 
#' @export
plot_reg_increase <- function(  year = 2021,
                              lag = 5,
                              topn = 5,
                              region = "Americas",
                              pop_type =  c("REF", "ASY", "OIP") ){

  # library(tidyverse)

  thisyear <- year
  baseline <- thisyear -lag
  data <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                                                     y= ForcedDisplacementStat::reference, 
                                                     by = c("CountryAsylumCode" = "iso_3")) |>
    filter(Population.type  %in% pop_type)  |>
    filter(Year == baseline | Year == thisyear) |>
    mutate( Year = case_when(
      Year == thisyear ~ paste("thisyear"),
      Year == baseline ~ paste("baseline") )) |>
    filter(UNHCRBureau == region) |>
    group_by( CountryAsylumName, Year) |>
    summarise(Value2 = sum(Value) )  |>
    select(CountryAsylumName, Year, Value2) |> 
    mutate(CountryAsylumName = str_replace(CountryAsylumName, " \\(Bolivarian Republic of\\)", ""),
        CountryAsylumName = str_replace(CountryAsylumName, "Iran \\(Islamic Republic of\\)", "Iran"),
        CountryAsylumName = str_replace(CountryAsylumName, "United Kingdom of Great Britain and Northern Ireland", "UK")) |> 
   # mutate(Year = paste0("year_",Year )) |>
    spread(Year, Value2) |>
    mutate(gap =   thisyear -baseline  ) |>
    arrange(desc(gap)) |>
    head(topn) 

 p <- ggplot(data, aes(x = baseline , 
                       xend = thisyear,
                       y = reorder(CountryAsylumName, gap),
                       group = CountryAsylumName)) + 
  ggalt::geom_dumbbell(colour = "#dddddd",
                size = 3,
                colour_x = "#0072bc",
                colour_xend = "#FAAB18") + 
  labs(title = paste0("Where did Refugee Population increased in ", region , "?"),
       subtitle = paste0("Biggest increase in Refugee Population, ", baseline," - ",thisyear),
       x="", y ="",
       caption = "Source: UNHCR.org/refugee-statistics") +
   ## Format axis number
  scale_x_continuous(labels = scales::label_number(accuracy = 1,   
                                                   scale_cut = cut_short_scale()))+ 
  theme_unhcr(font_size = 14)  + ## Insert UNHCR Style
  theme(panel.grid.major.x = element_line(color = "#cbcbcb"), 
        panel.grid.major.y = element_blank()) ### changing grid line that should appear

 return(p)

}
plot_reg_increase(year = 2021,
                  lag = 5,
                  topn = 5,
                  region = "Americas",
                  pop_type = c("REF", "ASY", "OIP"))

plot_reg_increase(year = 2021,
                  lag = 5,
                  topn = 5,
                  region = "Asia",
                  pop_type = c("REF", "ASY", "OIP"))
#test_that("plot_reg_increase works", {   expect_true(inherits(plot_reg_increase, "function")) })

Proportion of the population who are refugees, by country of origin (SDG indicator 10.7.4 )

#' Proportion of the population who are refugees, by country of origin 
#' 
#' The proportion of a country???s population who become refugees is SDG indicator 10.7.4 
#' it consistutes is a useful way to identify the countries of origin producing the most refugees relative to their number of inhabitants.
#' 
#' @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
#' 
#' @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 stringr str_replace               
#' @importFrom WDI WDI          
#' @importFrom forcats fct_reorder    
#' @importFrom unhcrthemes theme_unhcr
#' 
#' @return a ggplot2 object
#' 
#' @export

plot_reg_prop_origin <- function(year = 2022,  region = "Americas"){


  ## World bank API to retrieve total population
  # wb_data <- wbstats::wb( indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD",
  #                                     "NY.GDP.PCAP.CD", "NY.GNP.PCAP.CD"),
  #              startdate = 1990, 
  #              enddate = year, 
  #              return_wide = TRUE)
  # 
  # # Renaming variables for further matching
  # names(wb_data)[1] <- "iso_3"
  # names(wb_data)[2] <- "Year"

  wb_data <- WDI::WDI(country='all' ,
                      indicator=c("SP.POP.TOTL", "NY.GDP.MKTP.CD",
                                  "NY.GDP.PCAP.CD", "NY.GNP.PCAP.CD"),
                      start = 1990, 
                      end = year,
                      extra = TRUE)   
  # Renaming variables for further matching
  names(wb_data)[3] <- "iso3c"
  names(wb_data)[4] <- "Year"  


      wb_data$Year <- as.numeric(wb_data$Year)


  departed <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                                                     y= ForcedDisplacementStat::reference, 
                                                     by = c("CountryOriginCode" = "iso_3")) |> 
    dplyr::filter(Population.type  %in% c("REF","ASY","OIP") & 
           Year ==  year &
           UNHCRBureau  == region #&
           #!(is.na(UNHCRBureau))
           ) |>
    dplyr::group_by(CountryOriginName, CountryOriginCode) |>
    dplyr::summarise(Value2 = sum(Value) )  |>
    #mutate( value3 =  format_si(Value2))|>
    dplyr::mutate(CountryOriginName = stringr::str_replace(CountryOriginName, 
                                                           " \\(Bolivarian Republic of\\)", "")) |>

    ## Now merge with WB Data 
    dplyr::left_join(wb_data |> 
                       dplyr::select("SP.POP.TOTL","iso3c", "Year")|>
                       dplyr::filter(Year ==  year-1), 
                     by = c(  "CountryOriginCode" = "iso3c"    ))|>  
    dplyr::mutate(ref.part = round(Value2/(SP.POP.TOTL+Value2),4)  ) |>
    dplyr::arrange(desc(ref.part))|>
    head(10)  

p <- ggplot(departed,
                 aes( x= ref.part, forcats::fct_reorder(CountryOriginName, ref.part))) +

   geom_col(fill = "#0072BC") +

  # geom_col( fill = ifelse(departed$CountryOriginCode %in% c("VEN"), "#0072BC", "#CCCCCC")) +

  geom_label(aes(label = scales::label_percent(accuracy = .1)(ref.part) ),
             color = "black", hjust = "inward") +
  scale_x_continuous(labels = scales::label_percent(accuracy = .1)) +
  labs(x = NULL, 
       y = NULL,
       title = stringr::str_wrap(paste0("Number of refugees, asylum seekers & displaced across borders by country of origin in", region), 60),
       subtitle =    stringr::str_wrap( "Top 10 Countries, as a proportion of the national population of that country of origin (SDG indicator 10.7.4)", 80),
         caption = stringr::str_wrap("Source: UNHCR.org/refugee-statistics. \n Total count of population who have been recognized as refugees as a proportion of the total population of their country of origin, expressed per 100,000 population. Refugees refers to persons recognized by the Government and/or UNHCR, or those in a refugee-like situation.  Population refers to total resident population in a given country in a given year."), 100) +


  unhcrthemes::theme_unhcr(grid = "X", 
              axis = "y", 
              axis_title = "x",
              font_size = 14)  

return(p)

}
plot_reg_prop_origin(year = 2022,  region = "Americas")
# test_that("plot_reg_prop_origin works", {   expect_true(inherits(plot_reg_prop_origin, "function")) })

Decision on Refugee Status Determination

#' Plot Chart on Refugee Status Determination
#' 
#' Show the main host and origin countries based on number of decisions
#' 
#' @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 top_n_countries Numeric value of number of main countries that the graph should display
#' @param measure this can be either:
#'            * Recognized  
#'            * ComplementaryProtection  
#'            * TotalDecided 
#'            * RefugeeRecognitionRate 
#'            * TotalRecognitionRate
#' 
#' @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 geom_line
#'             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 stringr str_replace  
#' @importFrom scales cut_short_scale label_percent label_number breaks_pretty pretty_breaks
#' @importFrom patchwork plot_annotation
#' @importFrom unhcrthemes theme_unhcr unhcr_pal
#' 
#' 
#' @return a ggplot2 object
#' 
#' @export
plot_reg_rsd <- function(year = 2022,
                        region,
                        top_n_countries = 10, 
                        measure = "Recognized"){

  measurelabel <- dplyr::case_when( measure == "Recognized"  ~ "Recognized Refugee Status Decisions", 
                       measure == "ComplementaryProtection"  ~ "Complementary Protection Decisions",
                       measure == "TotalDecided"  ~ "Total Decision (independently of the outcome)", 
                       measure == "RefugeeRecognitionRate"  ~ "Refugee Recognition Rate", 
                       measure == "TotalRecognitionRate"  ~ "Total Recognition Rate")

topasyl <-  ForcedDisplacementStat::asylum_decisions |>
  ## Add reference for the filters
  dplyr::left_join( ForcedDisplacementStat::reference |> 
                      select(coa_region = `UNHCRBureau`, iso_3),
                    by = c("CountryAsylumCode" = "iso_3")) |> 
  filter(coa_region == region & 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(CountryAsylumName) |> 
  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(CountryAsylumName = str_replace(CountryAsylumName, "United States of America", "USA"))

topasyl1 <-  topasyl  |>
  mutate( measured = .data[[measure]]) |>
  arrange(desc(measured)) |>
  head(top_n_countries)  


topOrigin <-  ForcedDisplacementStat::asylum_decisions |>
  ## Add reference for the filters
  dplyr::left_join( ForcedDisplacementStat::reference |> 
                      select(coa_region = `UNHCRBureau`, iso_3),  by = c("CountryOriginCode" = "iso_3")) |> 
  filter(coa_region == region & 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]])  |>
  arrange(desc(measured)) |>
  head(top_n_countries)   


rsdasyl <-  ggplot(topasyl1, aes(y = measured, 
             x = reorder(CountryAsylumName, measured))) + 



  scale_y_continuous( labels =   scales::label_percent(accuracy = 0, suffix = "%") ) + 


  scale_y_continuous( labels =    ifelse( measure %in% c("RefugeeRecognitionRate", "TotalRecognitionRate"), 
                        scales::label_percent(accuracy = 0, suffix = "%"),
                        scales::label_number(accuracy = 1,   scale_cut = cut_short_scale()) )
                      ) + ## Format axis number
  #facet_grid(.~ ctry_asy) +  
  geom_bar( stat ="identity", fill = "#0072bc") +
  coord_flip() +
 # geom_hline(yintercept = 0, linewidth = 1.1, colour = "#333333") +
  labs(#title = "Number of RSD application  in 2020",
       subtitle = paste0( "For top ", top_n_countries, " Countries of Asylum"),
       x = " ", 
       y = " "  ) +
  theme_unhcr( grid = "Y",
               axis = "x",  axis_title = "" ,
              font_size = 10) +
  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) 


rsdorigin <- ggplot(topOrigin1, aes(y = measured, 
             x = reorder(CountryOriginName, measured))) +


  scale_y_continuous( labels =   ifelse( measure %in% c("RefugeeRecognitionRate", "TotalRecognitionRate"), 
                        scales::label_percent(accuracy = 0, suffix = "%"),
                        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())) + ## Format axis number
  #facet_grid(.~ ctry_asy) +  
  geom_bar( stat ="identity", fill = "#0072bc") +
  coord_flip() +
#  geom_hline(yintercept = 0, size = 1.1, colour = "#333333")   +
  labs(#title = "Number of RSD application per country of Origin in 2020",
       subtitle = paste0( "For top ", top_n_countries, " Countries of Origin"),
       x = " ", 
       y = " " ) +
  theme_unhcr( grid = "Y",
               axis = "x",  axis_title = "" ,
              font_size = 10 ) + 
  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) 


#joining charts 
requireNamespace("patchwork")
patchworkRSDa <- rsdasyl + rsdorigin
patchworkRSDa1 <- patchworkRSDa +
  #unhcRstyle::unhcr_theme(base_size = 8)  +  ## Insert UNHCR Style
  theme(legend.position = "none") +
  patchwork::plot_annotation(
    title = paste0(measurelabel, " | ", year, ", in ",region),
   # subtitle = ' ',
    caption = 'Source: UNHCR.org/refugee-statistics '
   ## \n Because of different types of procedure, data from the US may \n includes multiple applications per applicants compared to other countries
  )  

  return(patchworkRSDa1)
}
plot_reg_rsd(year = 2022,
             region = "Americas" ,
                        top_n_countries = 10, 
                        measure = "Recognized")


plot_reg_rsd(year = 2022,
             region = "Americas" ,
                        top_n_countries = 5, 
                        measure = "ComplementaryProtection")


plot_reg_rsd(year = 2022,
             region = "Americas" ,
                        top_n_countries = 10, 
                        measure = "TotalDecided")


plot_reg_rsd(year = 2022,
             region = "Americas" ,
                        top_n_countries = 10, 
                        measure = "RefugeeRecognitionRate")


plot_reg_rsd(year = 2022,
             region = "Americas" ,
                        top_n_countries = 10, 
                        measure = "TotalRecognitionRate")


# plot_reg_rsd(year = 2022,
#              region = "Europe", 
#                         top_n_countries = 10, 
#                         measure = "Recognized")
# test_that("plot_reg_rsd works", {   expect_true(inherits(plot_reg_rsd, "function")) })

Plot Evolution of Solutions in the Region

#' Plot Solution over time in the region
#' 
#' Description
#' @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 lag Number of year to used as comparison base
#' @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 geom_smooth
#'             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  if_else 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
#' @importFrom ggrepel geom_label_repel
#' 
#' @return a ggplot2 object
#' 
#' @export
#'  

plot_reg_solution <- function( year = 2022, 
                            region = "Americas",
                            lag = 10){

    solution <-  dplyr::left_join( x= ForcedDisplacementStat::solutions_long, 
                                                     y= ForcedDisplacementStat::reference, 
                                                     by = c("CountryOriginCode" = "iso_3")) |> 
                dplyr::filter(Solution.type %in% c("NAT","RST","RET" ) & 
                       UNHCRBureau  == region &
                       Year >= (year - lag) ) |>
                 dplyr::group_by(Year, Solution.type, Solution.type.label )|>
                 dplyr::summarise(Value = sum(Value) ) |>
                 dplyr::ungroup() |>
                 dplyr::group_by( Solution.type  )|>
                 dplyr::mutate(label = dplyr::if_else(Year == max(Year), 
                                               as.character(Solution.type.label),
                                               NA_character_))|>
                 dplyr::ungroup() 

#Make plot
p <- ggplot(solution, aes(x = Year, 
                          y = Value, 
                          colour = Solution.type)) + # Adding reference to color
    geom_smooth(se = FALSE, method = "loess", span = .2) +
    scale_x_continuous( breaks = seq(year - lag, year, by = 5) )+ 
    scale_y_continuous( labels = scales::label_number(accuracy = 1, 
                                                      scale_cut = cut_short_scale()))+
    #scale_colour_viridis_d() + 
     ## Add color for each lines based on color-blind friendly palette
    scale_colour_manual( values = c( "NAT" = "#a6cee3",
                                     "RST"  = "#1f78b4",
                                     "RET" = "#b2df8a" )) +
    ## and the chart labels
    labs(title = paste0("Solution for Displacement in ", region) ,
         subtitle = paste0("Evolution in the past ", lag," years"),
         x = "",
         y = "",
         caption = "Source: UNHCR.org/refugee-statistics ") +   
    ggrepel::geom_label_repel(aes(label = label),
                              nudge_x = 1,
                              na.rm = TRUE) +
    unhcrthemes::theme_unhcr(grid = "Y", 
              axis = "x", 
              axis_title = "y",
              font_size = 14,
              legend = FALSE)  


 return(p)  

}
plot_reg_solution(year = 2022, 
                            region = "Americas",
                            lag = 10)
# test_that("plot_reg_solution works", {   expect_true(inherits(plot_reg_solution, "function")) })

Mapping Population

#' Plot a regional map
#' 
#' 
#' @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 topn how many top countries to show..
#' @param pop_type Vector of character values. Possible population type 
#'                (e.g.: REF, IDP, ASY, OIP, OOC, STA)
#'                
#' @param projection use a projection system - default is "Mercator"
#'           for instance this can be Bertin 1953 projection - 
#'          https://visionscarto.net/bertin-projection-1953)
#'          
#' @param maxSymbolsize size in point to adjust for the maximum value
#'  to display on the map
#' 
#' @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 utils  head
#' @importFrom sf st_transform st_as_sf st_drop_geometry
#' @importFrom rnaturalearth ne_countries
#' @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 graphics par
#' @importFrom unhcrthemes theme_unhcr
#' @importFrom WDI WDI 
#' @importFrom Hmisc cut2
#' 
#' @return a base plot
#' 
#' @export
#' 
plot_reg_map <- function(    year = 2022, 
                            region = "Americas",
                            topn = 5,
                            pop_type =  c("REF", "ASY", "OIP"),
                            projection = "Mercator",
                            maxSymbolsize = .25   ){




   ## World bank API to retrieve total population
  # wb_data <- wbstats::wb( indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD", "NY.GDP.PCAP.CD", "NY.GNP.PCAP.CD"),
  #              startdate = 1990, 
  #              enddate = year, 
  #              return_wide = TRUE)
  # 
  # # Renaming variables for further matching
  # names(wb_data)[1] <- "CountryAsylumCode"
  # names(wb_data)[2] <- "Year"

  wb_data <- WDI::WDI(country='all', 
                                 ## Population total https://data.worldbank.org/indicator/SP.POP.TOTL
                      indicator=c("SP.POP.TOTL",
                                  ## GDP current
                                  # https://data.worldbank.org/indicator/NY.GDP.MKTP.CD
                                            "NY.GDP.MKTP.CD",
                                  ## GDP per capita 
                                  # https://data.worldbank.org/indicator/NY.GDP.PCAP.CD
                                            "NY.GDP.PCAP.CD", 
                                  ## GNI per capita, Atlas method (current US$)
                                  # https://data.worldbank.org/indicator/NY.GNP.PCAP.CD
                                            "NY.GNP.PCAP.CD" 
                                  ),
                      start = year-1,
                      end = year,
                      extra = TRUE)   
  # Renaming variables for further matching
  names(wb_data)[3] <- "CountryAsylumCode"
  names(wb_data)[4] <- "Year"



      wb_data <- wb_data|>
        filter(Year == year-1) 

      ## Get spatial data to add ##########
      mapproject <- ""

      listctr <-  ForcedDisplacementStat::reference |>
        filter(UNHCRBureau == region) |>
        select(iso_3) |>
        pull()



      ## Loading the stat tables ######
      data <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, 
                                y= ForcedDisplacementStat::reference, 
                                by = c("CountryAsylumCode" = "iso_3")) |>
        filter(Population.type  %in% pop_type &
                 Year == year & 
                 UNHCRBureau == region ) |>
        group_by(Year, CountryAsylumName, CountryAsylumCode, UNHCRBureau,Latitude, Longitude  ) |>
        summarise(Value = sum(Value) ) |>
        ungroup()


      ## Join ########
      data2 <- data |> 
        left_join(wb_data, 
                  by = c( "CountryAsylumCode")) |>  
        mutate(ratio_disp_gdp = round( (Value / NY.GDP.MKTP.CD)*100, 4)  )  |> 
        mutate(ratio_disp_gdpcap = round(  (Value/ NY.GDP.PCAP.CD)*100, 2)  )   |> 
        mutate(ratio_disp_host = round( (Value/  SP.POP.TOTL)*100 , 2)  )  

      ## Get Break https://riatelab.github.io/mapsf/reference/mf_get_breaks.html
      # Discretize the variable
      data2$quintDisp <- Hmisc::cut2(data2$Value, g = 4)
      data2$quintDispGGP <- Hmisc::cut2(data2$ratio_disp_gdp, g = 4) 
      data2$quintDispHost <- Hmisc::cut2(data2$ratio_disp_host, g = 4)


      #names(data2)
      data2 <- data2 |>
        sf::st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)


      ## Getting world map for mapping
      world <- rnaturalearth::ne_countries(scale = "small", returnclass = "sf") |> 
        filter(continent != "Antarctica")  |>  
        filter(adm0_a3 %in%  listctr) 

      ### Need to fix specific case for Asisa... where the proj disperse the country...
      ##  filter(adm0_a3 != "VUT") 

      ## Manage Projection... 
      data2 <- data2 |>  
        # this is the crs from d, which has no EPSG code:
        sf::st_transform( '+init=epsg:4326')

      world <- world |>  
        # this is the crs from d, which has no EPSG code:
        sf::st_transform('+init=epsg:4326')
      #   # this is the crs from d, which has no EPSG code:
      #   #sf::st_transform(., '+init=epsg:4326')
      #   #sf::st_transform(., '+proj=bertin1953 +R=1 0.72 0.73')
      #   #sf::st_transform(., '+proj=bertin1953 +x_0=1000')
      #   sf::st_transform(., '+proj=bertin1953')


      PopMap <- data2 |> 
                          sf::st_drop_geometry()|> 
                          ungroup() |>
                          arrange( desc (Value)) |> 
                          head(topn) |> 
                          select(Value) 

      minPopMap <- PopMap |>   min()
      maxPopMap <- PopMap |>  max()             


     regionname <- dplyr::case_when( region == "Americas"  ~  "Americas",
                                  region == "Asia"  ~  "Asia & the Pacific",
                                  region == "EastAfrica"  ~  "Eastern Africa",
                                  region =="Europe"  ~  "Europe",
                                  region == "MENA"  ~  "Middle East & North Africa",
                                  region == "SouthAfrica"  ~  "Southern Africa",
                                  region == "WestAfrica"  ~  "Western Africa")          

      ## Generate Map   ##################
      #Maps is created here with [MapSF package](https://riatelab.github.io/mapsf/index.html)
      # Select a font already installed on your system !!
      par(family="Lato")
      # set a theme
      mapsfunhcr <- mapsf::mf_theme(bg ="#d4dff2", #  "#E2E7EB",  ## background color --> Used country 
                      # bg = "#cdd2d4", "#faebd7ff",  "#cdd2d4",
                      mar = c(0, 0, 2, 0), ## margins
                      tab = FALSE,  # if TRUE the title is displayed as a 'tab'
                      fg = "#0072BC",  ## foreground color --> for the top title - use UNHCR Blue..
                      pos = "left", # position, one of 'left', 'center', 'right'
                      inner = FALSE, # if TRUE the title is displayed inside the plot area.
                      line = 2, #number of lines used for the title
                      cex = 2.5, #cex of the title
                      #font = "Lato",
                      font = 1 ) #font of the title


           # map_fun <- function(data, init, theme){
     #      # Initiate a base map
     #      mf_init(x = init, theme = theme)
     #      # always use add = TRUE after mf_init()
     #      mf_shadow(data, col = "grey50", cex = 0.2 , add = TRUE)
     #      mf_map(data, add = TRUE)
     #      mf_title(txt = "Martinique ", fg = "#FFFFFF")
     #      mf_credits(txt = "Credit", bg = "#ffffff80") 
     #      return(invisible(NULL)) 
     #      }


      ##Initialise the map with a background
      mapsf::mf_init( x = world, theme = mapsfunhcr) 
      # always use add = TRUE after mf_init()

      # Plot a shadow
      mapsf::mf_shadow(world, col = "grey50", cex = 0.2 , 
                       add = TRUE)

      mapsf::mf_map(world, 
                    lwd = 0.5, 
                    border = "#93A3AB", 
                    col = "#FFFFFF", 
                    add = TRUE)

      # Set a layout
      mapsf::mf_title(txt = paste0("Forced Displacement in ",
                                   regionname, " | ", year), fg = "#FFFFFF")
      mapsf::mf_credits(txt = "Source: UNHCR.org/refugee-statistics - A Country is name if it features among the five largest population.\n The boundaries and names shown and the designations used on this map do not imply official endorsment or acceptance by the inited nations", bg = "#ffffff80" ) 


      ## Proportional Symbol with color based on other
      mapsf::mf_prop_typo( 
        x = data2, # frame to use.. 
        var = c("Value", "quintDispHost"),  ## First value for size, second for color
        inches = maxSymbolsize, # size of the biggest symbol (radius for circles, half width for squares) in inches
        val_max = maxPopMap,  # maximum value used for proportional symbols
        symbol = "circle", ## type of symbol- 'circle' or 'square'
        border = "grey25", ## border color of symbol
        lwd = 1.5, #  border width of symbol
        pal = "Inferno",  ## Color palette  - https://developer.r-project.org/Blog/public/2019/04/01/hcl-based-color-palettes-in-grdevices/
        alpha = .8,  ## if pal is a hcl.colors palette name, the alpha-transparency level
        leg_no_data = "No data", ## When no data
        col_na = "grey",  ## When no data
        leg_pos = c("topright", "bottomright"),  # position of the legend
        leg_title = c("Number of Individuals", "Ratio with Host Community"), # title of the legend
        leg_title_cex = c(.7, .7),  # title font size of the legend
        leg_val_cex = c(.5, .5), # content font size of the legend
        leg_val_rnd = .2, # number of decimal places of the values in the legend
        leg_frame = c(FALSE, FALSE), # add frame around the legend
        add = TRUE
      )
    # labels for a few  countries - https://riatelab.github.io/mapsf/reference/mf_label.html
     mapsf::mf_label(x = data2[data2$Value >= minPopMap,], 
                      var = "CountryAsylumName",  # name(s) of the variable(s) to plot
                      cex = 0.9, #  labels cex
                      col = "black", 
                      font = 3.5,
                      halo = TRUE, # add halo
                      bg = "white",  # halo color
                      r = 0.2,  # width of the halo
                      overlap = FALSE,  # if FALSE, labels are moved so they do not overlap.
                      lines = TRUE) 

     return(invisible(NULL))
}
plot_reg_map(  year = 2022,
                            region = "Asia",
                            topn = 5,
                            pop_type =  c("REF", "ASY", "OIP"),
                            projection = "Mercator",
                            maxSymbolsize = .25)


# plot_reg_map(  year = 2022,
#                             region = "WestAfrica",
#                             topn = 5,
#                             pop_type =  c("REF", "ASY", "OIP"),
#                             projection = "Mercator",
#                             maxSymbolsize = .25)
# test_that("plot_reg_map works", {   expect_true(inherits(plot_reg_map, "function")) })

Refresh package

# Keep eval=FALSE to avoid infinite loop in case you hit the knit button
# Execute in the console directly
fusen::inflate(flat_file = "dev/dev_plot_Region.Rmd", vignette_name = "Chart Library Region", overwrite = 'yes')
pkgdown::build_site()


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