Skip to content

Commit

Permalink
update with better mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard-Legoupil committed Feb 28, 2024
1 parent 6a79e1a commit 19feb76
Show file tree
Hide file tree
Showing 101 changed files with 3,275 additions and 1,766 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export(show_contributions)
export(show_donors)
export(show_earmarking)
export(show_expenditure)
export(show_goal_sdg)
export(show_indicators)
export(show_indicators_time)
export(show_outcome_rbm)
export(show_partnership)
export(show_sectors)
export(show_sectors_rbm)
Expand Down
54 changes: 54 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
#' \describe{
#' \item{\code{result_title}}{character result_title}
#' \item{\code{sector_rbm}}{character sector_rbm}
#' \item{\code{area_id}}{character COLUMN_DESCRIPTION}
#'}
#' @source \url{https://www.unhcr.org/what-we-do/build-better-futures/compass/}
"mapping_result"
Expand All @@ -378,10 +379,63 @@
#' \item{\code{sector_desc}}{character COLUMN_DESCRIPTION}
#' \item{\code{sector_vocabulary_name}}{character COLUMN_DESCRIPTION}
#' \item{\code{sector_rbm}}{character COLUMN_DESCRIPTION}
#' \item{\code{area_id}}{character COLUMN_DESCRIPTION}
#'}
#' @source \url{https://www.unhcr.org/what-we-do/build-better-futures/compass}
"mapping_sector"



#' @title mapping_sdg
#' @description Mapping UNHCR areas to UN SDG
#' @format A data frame with 26 rows and 2 variables:
#' \describe{
#' \item{\code{area}}{character COLUMN_DESCRIPTION}
#' \item{\code{sdg}}{character COLUMN_DESCRIPTION}
#'}
#' @source \url{https://www.unhcr.org/what-we-do/build-better-futures/compass}#' @examples
#' {
#' knitr::kable(head(mapping_sdg, 26))
#'}
"mapping_sdg"



#' @title mapping_indicator
#' @description Mapping Interpretation threshold for indicator values
#' @format A data frame with 47 rows and 27 variables:
#' \describe{
#' \item{\code{Indicator}}{character COLUMN_DESCRIPTION}
#' \item{\code{Indicator_lab2}}{character COLUMN_DESCRIPTION}
#' \item{\code{Area of work}}{character COLUMN_DESCRIPTION}
#' \item{\code{theme}}{character COLUMN_DESCRIPTION}
#' \item{\code{subtheme}}{character COLUMN_DESCRIPTION}
#' \item{\code{Results_Level}}{character COLUMN_DESCRIPTION}
#' \item{\code{area_id}}{character COLUMN_DESCRIPTION}
#' \item{\code{Area}}{character COLUMN_DESCRIPTION}
#' \item{\code{Area_id}}{character COLUMN_DESCRIPTION}
#' \item{\code{Ind_id}}{character COLUMN_DESCRIPTION}
#' \item{\code{Ind_seq}}{double COLUMN_DESCRIPTION}
#' \item{\code{Show_As}}{character COLUMN_DESCRIPTION}
#' \item{\code{Indicator_Code}}{character COLUMN_DESCRIPTION}
#' \item{\code{Reverse}}{logical COLUMN_DESCRIPTION}
#' \item{\code{threshold_red}}{double COLUMN_DESCRIPTION}
#' \item{\code{threshold_orange}}{double COLUMN_DESCRIPTION}
#' \item{\code{threshold_green}}{double COLUMN_DESCRIPTION}
#' \item{\code{standard_direction}}{character COLUMN_DESCRIPTION}
#' \item{\code{survey}}{character COLUMN_DESCRIPTION}
#' \item{\code{RAS}}{double COLUMN_DESCRIPTION}
#' \item{\code{STA}}{double COLUMN_DESCRIPTION}
#' \item{\code{IDP}}{double COLUMN_DESCRIPTION}
#' \item{\code{RET}}{double COLUMN_DESCRIPTION}
#' \item{\code{OOC}}{double COLUMN_DESCRIPTION}
#' \item{\code{all}}{double COLUMN_DESCRIPTION}
#' \item{\code{DEN}}{character COLUMN_DESCRIPTION}
#' \item{\code{max}}{character COLUMN_DESCRIPTION}
#'}
#' @source \url{http://somewhere.important.com/}
"mapping_indicator"

#### All code list doc ############


Expand Down
2 changes: 1 addition & 1 deletion R/show_budget_gap.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ show_budget_gap <- function(year,
subtitle = subtitt ,
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative). 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." )
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)

Expand Down
2 changes: 1 addition & 1 deletion R/show_contributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ show_contributions <- function(year,
subtitle = paste0("Recorded in ", programme_lab, ctr_name,iati_identifier_ops, " since ", year,""),
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative)" )
caption = "Source: Data published by UNHCR as part of the International Aid Transparency Initiative (IATI)" )

return(p)
}
2 changes: 1 addition & 1 deletion R/show_donors.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ show_donors <- function(year,
subtitle = paste0("Recorded in ", programme_lab, ctr_name,iati_identifier_ops, " since ", year,""),
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative)" )
caption = "Source: Data published by UNHCR as part of the International Aid Transparency Initiative (IATI)" )
}

return(p)
Expand Down
21 changes: 15 additions & 6 deletions R/show_earmarking.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
#' dplyr::select(earmarking_name, earmarking_description) |>
#' dplyr::distinct() |>
#' dplyr::filter(!(is.na(earmarking_name))))
#' show_earmarking(year = 2018,
#' programme_lab = NULL,
#' iati_identifier_ops = NULL,
#' show_earmarking(year = 2018,
#' ctr_name = "Brazil")
show_earmarking <- function(year,
programme_lab = NULL,
Expand Down Expand Up @@ -79,20 +77,31 @@ show_earmarking <- function(year,
year >= thisyear &
transaction_type_name == "Incoming Commitment")
}

# levels(as.factor(df$earmarking_name))

df2 <- df |>
dplyr::group_by(year, earmarking_name) |>
dplyr::summarise( transaction_value_USD = sum(transaction_value_USD , na.rm = TRUE)) |>
dplyr::mutate(earmarking_name = as.character(earmarking_name) ) |>
dplyr::mutate(earmarking_name = as.factor(earmarking_name) )


cols_earmarking_name <- c("Earmarked" = "#e1cc0d",
"Softly Earmarked" = "#0072bc",
"Tightly Earmarked" = "#ef4a60",
"Unearmarked" = "#00b398" )

p <- df2 |>
# dplyr::filter(transaction_value_USD <= 1000000 & transaction_value_USD > 1000) |>
ggplot2::ggplot(ggplot2::aes(y = transaction_value_USD ,
x = year,
fill = earmarking_name)) +
ggplot2::geom_bar(alpha = 0.9, stat = "identity") +
ggplot2::scale_fill_viridis_d(option = "inferno", na.value = "grey50") +
#ggplot2::scale_fill_viridis_d(option = "inferno", na.value = "grey50") +
ggplot2::scale_fill_manual(values = cols_earmarking_name,
drop = TRUE,
limits = force,
na.value = "grey50") +
ggplot2::scale_y_continuous(
expand = ggplot2::expansion(mult = c(0, .1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()) ) +
Expand All @@ -104,7 +113,7 @@ show_earmarking <- function(year,
subtitle = paste0("Recorded in ", programme_lab, ctr_name,iati_identifier_ops, " since ", year,""),
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative)" )
caption = "Source: Data published by UNHCR as part of the International Aid Transparency Initiative (IATI)" )

return(p)
}
4 changes: 2 additions & 2 deletions R/show_expenditure.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ show_expenditure <- function(year,
ggplot2::scale_color_manual(name = " ",
values = c("Expenditure" = "#F592A0", "Budget" = "#00568D")) +
ggplot2::scale_y_continuous(
expand = ggplot2::expansion(mult = c(0, .1)),
# expand = ggplot2::expansion(mult = c(0, .1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()) ) +
# scale_x_continuous(labels = scales::label_number(scale_cut = cut_short_scale())) +
# ggplot2::facet_wrap(~ trans_year) +
Expand All @@ -157,7 +157,7 @@ show_expenditure <- function(year,
subtitle = subtitt,
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative). 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." )
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)
}
145 changes: 145 additions & 0 deletions R/show_goal_sdg.R
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)
}
4 changes: 2 additions & 2 deletions R/show_indicators.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ show_indicators <- function(year,
"Deviation between reported \"Actual\" value and programmatic \"Target\" (in %)" ) ,
110),
caption = stringr::str_wrap(
"Data Source: UNHCR IATI (International Aid Transparency Initiative)" ,
"Source: Data published by UNHCR as part of the International Aid Transparency Initiative (IATI)" ,
110) )
}
} else if( type == "progress") {
Expand Down Expand Up @@ -295,7 +295,7 @@ show_indicators <- function(year,
"Progress comparison between \"Actual\" reported value and their \"baseline\" (in %)" ) ,
110),
caption = stringr::str_wrap(
"Data Source: UNHCR IATI (International Aid Transparency Initiative)" ,
"Source: Data published by UNHCR as part of the International Aid Transparency Initiative (IATI)" ,
110) )
}
}
Expand Down
Loading

0 comments on commit 19feb76

Please sign in to comment.