-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a79e1a
commit 19feb76
Showing
101 changed files
with
3,275 additions
and
1,766 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# WARNING - Generated by {fusen} from dev/dev_unhcr_programme.Rmd: do not edit by hand | ||
|
||
#' show_goal_sdg | ||
#' | ||
#' @description Show resource allocation in line with SDG goal | ||
#' @param year A numeric value or a list of value. | ||
#' @param programme_lab A character vector corresponding to the name of the programme. | ||
#' @param iati_identifier_ops A character vector corresponding to the name of the operation. | ||
#' @param ctr_name A character vector corresponding to the name of the country. | ||
#' | ||
#' @import ggplot2 | ||
#' @import dplyr | ||
#' @import scales | ||
#' @import unhcrthemes | ||
#' @importFrom stats reorder | ||
#' | ||
#' @export | ||
#' @return a graph | ||
#' @examples | ||
#' show_goal_sdg(year = c( 2020, 2021, 2022), | ||
#' ctr_name = "Brazil") | ||
show_goal_sdg <- function(year, | ||
programme_lab = NULL, | ||
iati_identifier_ops = NULL, | ||
ctr_name = NULL ){ | ||
# Check if only one argument is passed | ||
if (!is.null(programme_lab) && !is.null(iati_identifier_ops)) { | ||
stop("Please pass only one of the arguments programme_lab or iati_identifier_ops.") | ||
} else if (!is.null(programme_lab) && !is.null(ctr_name)) { | ||
stop("Please pass only one of the arguments programme_lab or ctr_name.") | ||
} else if (!is.null(iati_identifier_ops) && !is.null(ctr_name)) { | ||
stop("Please pass only one of the arguments iati_identifier_ops or ctr_name.") | ||
} | ||
|
||
# Join dataActivity and dataSector on iati_identifier | ||
df <- iati::dataSector |> | ||
dplyr::left_join(iati::dataActivity, by = c("iati_identifier")) | ||
|
||
if (!is.null(programme_lab)) { | ||
thisprogramme_lab <- programme_lab | ||
thisyear <- year | ||
df <- df |> | ||
dplyr::mutate(year = factor(year)) |> | ||
dplyr::filter(programmme_lab == thiprogramme_lab & | ||
year %in% thisyear & | ||
sector_vocabulary_name == "Reporting Organisation 2") |> | ||
dplyr::left_join(iati::mapping_sector, by= c("sector_desc")) |> | ||
dplyr::left_join(iati::mapping_sdg, by= c("sector_rbm" = "area")) | ||
|
||
} else if (!is.null(iati_identifier_ops)) { | ||
thisiati_identifier_ops <- iati_identifier_ops | ||
thisyear <- year | ||
df <- df |> | ||
dplyr::mutate(year = factor(year)) |> | ||
dplyr::filter(iati_identifier_ops == thisiati_identifier_ops & | ||
year %in% thisyear & | ||
sector_vocabulary_name == "Reporting Organisation 2") |> | ||
dplyr::left_join(iati::mapping_sector, by= c("sector_desc"))|> | ||
dplyr::left_join(iati::mapping_sdg, by= c("sector_rbm" = "area")) | ||
} else if (!is.null(ctr_name)) { | ||
thisctr_name <- ctr_name | ||
thisyear <- year | ||
df <- df |> | ||
dplyr::mutate(year = factor(year)) |> | ||
dplyr::filter(ctr_name == thisctr_name & | ||
year %in% thisyear & | ||
sector_vocabulary_name == "Reporting Organisation 2") |> | ||
dplyr::left_join(iati::mapping_sector, by= c("sector_desc"))|> | ||
dplyr::left_join(iati::mapping_sdg, by= c("sector_rbm" = "area")) | ||
} | ||
|
||
df <- df |> | ||
dplyr::group_by(sector_desc, sector_rbm,sdg, year) |> | ||
dplyr::summarise(sector_pct = mean( as.numeric(sector_pct)) ) |> | ||
dplyr::group_by( sdg, year) |> | ||
dplyr::summarise(sector_pct = sum(sector_pct, rm.na = TRUE)) |> | ||
# dplyr::summarise(sector_pct = sum(sector_pct, na.rm = TRUE)/sum(df$sector_pct, na.rm = TRUE)*100) |> | ||
# top_n(5, wt = sector_pct) |> | ||
dplyr::mutate(sdg = as.factor(sdg)) | ||
|
||
|
||
## Now joining budget & Expenditure to make the chart more informative... | ||
df_bud <- iati::dataTransaction |> | ||
dplyr::left_join(iati::dataActivity, by= c("iati_identifier")) | ||
|
||
if (!is.null(programme_lab)) { | ||
thisprogramme_lab <- programme_lab | ||
thisyear <- year | ||
df_bud <- df_bud |> | ||
# levels(as.factor(df$programmme_lab)) | ||
dplyr::filter( programmme_lab == thisprogramme_lab & | ||
year %in% thisyear & | ||
transaction_type_name == "Expenditure") | ||
} else if (!is.null(iati_identifier_ops)) { | ||
thisiati_identifier_ops <- iati_identifier_ops | ||
thisyear <- year | ||
df_bud <- df_bud |> | ||
dplyr::filter(iati_identifier_ops == thisiati_identifier_ops & | ||
year %in% thisyear & | ||
transaction_type_name == "Expenditure") | ||
} else if (!is.null(ctr_name)) { | ||
thisctr_name <- ctr_name | ||
thisyear <- year | ||
df_bud <- df_bud |> | ||
dplyr::filter( ctr_name == thisctr_name & | ||
year %in% thisyear & | ||
transaction_type_name == "Expenditure") | ||
} | ||
|
||
df_bud2 <- df_bud |> | ||
dplyr::group_by(iati_identifier, year) |> | ||
dplyr::summarise(transaction_value= sum(transaction_value, na.rm = TRUE)) |> | ||
dplyr::left_join(iati::dataBudget |> | ||
dplyr::mutate(budget_value= as.numeric(budget_value)) |> | ||
dplyr::group_by(iati_identifier) |> | ||
dplyr::summarise(budget_value= sum(budget_value, na.rm = TRUE)) | ||
, by= c("iati_identifier")) |> | ||
dplyr::select(iati_identifier, year,budget_value, transaction_value ) |> | ||
dplyr::mutate( year2 = glue::glue('{year} - Bud:{scales::label_number(accuracy = .2, scale_cut = scales::cut_short_scale())(budget_value)}$/ Exp:{scales::label_number(accuracy = .2, scale_cut = scales::cut_short_scale())(transaction_value)}$ ')) | ||
|
||
df <- df |> | ||
dplyr::left_join(df_bud2, by = c("year")) | ||
|
||
|
||
|
||
p <- ggplot2::ggplot(data = df, | ||
ggplot2::aes(x = stats::reorder(sdg, sector_pct), | ||
y = sector_pct | ||
)) + | ||
# unhcrthemes::theme_unhcr(grid = TRUE, axis = "Y", axis_title = "Sector Percentage") + | ||
unhcrthemes::theme_unhcr(grid = "X", axis = "y", axis_title = "X", font_size = 18) + | ||
|
||
ggplot2::geom_bar(stat = "identity", fill = "#0072BC") + | ||
ggplot2::coord_flip()+ | ||
ggplot2::facet_wrap( ggplot2::vars(year2), labeller = labeller(year2 = ggplot2::label_wrap_gen(5))) + | ||
ggplot2::scale_fill_viridis_d(option = "inferno", na.value = "grey50") + | ||
ggplot2::scale_y_continuous(expand = ggplot2::expansion(mult = c(0, .1)), labels = scales::label_number(scale_cut = scales::cut_short_scale())) + | ||
ggplot2::labs(title = "Resource Allocation per SDG (%)", | ||
subtitle = paste0("Recorded in ", programme_lab, ctr_name,iati_identifier_ops, | ||
" "), | ||
x = "Sectors", y = "% of Total Funding", | ||
caption = "Source: Data published by UNHCR as part of the International Aid Transparency Initiative (IATI). UNHCR budget is needs-based. It represents the total amount of money that would be required were UNHCR to meet all of the needs that it is seeking to address.") | ||
|
||
return(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.