# WARNING - Generated by {fusen} from /dev/dev_plot_Region.Rmd: do not edit by hand
#' 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
#' @examples
#' plot_reg_prop_origin(year = 2022, region = "Americas")
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.