Skip to content

Commit

Permalink
adding charts in the library
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard-Legoupil committed Feb 15, 2024
1 parent 26b0917 commit 9a43cd9
Show file tree
Hide file tree
Showing 106 changed files with 2,675 additions and 710 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ URL: https://unhcr-americas.github.io/iati/docs/
BugReports: https://github.com/unhcr-americas/iati/issues
Imports:
dplyr,
forcats,
ggplot2,
glue,
knitr,
magrittr,
refugees,
scales,
showtext,
stats,
stringr,
sysfonts,
systemfonts,
tidyr,
tidyselect,
unhcrthemes
Suggests:
rmarkdown,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(show_budget_gap)
export(show_contributions)
export(show_donors)
export(show_earmarking)
export(show_expenditure)
export(show_indicators)
export(show_partnership)
export(show_sectors)
export(show_top_donors)
import(dplyr)
import(ggplot2)
import(refugees)
import(scales)
import(tidyselect)
import(unhcrthemes)
importFrom(magrittr,"%>%")
importFrom(stats,reorder)
22 changes: 11 additions & 11 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
## iati_budget ##########
#' @title dataBudget
#' @description Display budget by operation
#' @format A data frame with 1194 rows and 8 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character COLUMN_DESCRIPTION}
#' \item{\code{budget_type}}{double COLUMN_DESCRIPTION}
Expand Down Expand Up @@ -111,7 +111,7 @@

#' @title dataResult
#' @description DATASET_DESCRIPTION
#' @format A data frame with 26667 rows and 24 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character A globally unique identifier for the activity.}
#' \item{\code{result_type}}{double COLUMN_DESCRIPTION}
Expand Down Expand Up @@ -161,7 +161,7 @@
## iati_sector ##########
#' @title sector
#' @description DATASET_DESCRIPTION
#' @format A data frame with 23793 rows and 6 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character A globally unique identifier for the activity.}
#' \item{\code{sector_vocabulary}}{character COLUMN_DESCRIPTION}
Expand All @@ -182,7 +182,7 @@
## iati_transaction ##########
#' @title transaction
#' @description Transactions recording committed or actual funds flowing in or out of an aid activity.
#' @format A data frame with 40419 rows and 29 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character COLUMN_DESCRIPTION}
#' \item{\code{transaction_ref}}{character COLUMN_DESCRIPTION}
Expand All @@ -207,8 +207,8 @@
#' \item{\code{provider_org_type_description}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type1_name}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type1_description}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type2_name}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type2_description}}{character COLUMN_DESCRIPTION}
#' \item{\code{earmarking_name}}{character COLUMN_DESCRIPTION}
#' \item{\code{earmarking_description}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type_vocabulary1_name}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type_vocabulary1_description}}{character COLUMN_DESCRIPTION}
#' \item{\code{aid_type_vocabulary2_name}}{character COLUMN_DESCRIPTION}
Expand All @@ -227,7 +227,7 @@
## iati_default_aid_type ##########
#' @title dataDefault_aid_type
#' @description Default_aid_type
#' @format A data frame with 825 rows and 3 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character A globally unique identifier for the activity.}
#' \item{\code{default_aid_type_code}}{character aid_type_code}
Expand Down Expand Up @@ -287,7 +287,7 @@
## iati_location ##########
#' @title dataLocation
#' @description DATASET_DESCRIPTION
#' @format A data frame with 3664 rows and 15 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character A globally unique identifier for the activity.}
#' \item{\code{location_ref}}{character COLUMN_DESCRIPTION}
Expand Down Expand Up @@ -318,7 +318,7 @@
#' @description An organisation involved with the activity. May be a donor, fund, agency, etc.
#' Specifying the ref identifier is strongly recommended. May contain the organisation name as narrative.
#' If the reporting organisation plays a role in the activity it should be repeated here. One organisation may play more than one role (eg, funding and implementing): in such a case each role should be reported and the name of the organisation repeated.
#' @format A data frame with 13990 rows and 7 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character A globally unique identifier for the activity.}
#' \item{\code{participating_org_eng}}{character COLUMN_DESCRIPTION}
Expand All @@ -342,7 +342,7 @@
## iati_related_activity ##########
#' @title dataRelated_activity
#' @description DATASET_DESCRIPTION
#' @format A data frame with 2950 rows and 3 variables:
#' @format A data frame
#' \describe{
#' \item{\code{iati_identifier}}{character A globally unique identifier for the activity.}
#' \item{\code{related_activity_ref}}{character COLUMN_DESCRIPTION}
Expand Down Expand Up @@ -729,7 +729,7 @@
#' \item{\code{url}}{character url}
#' \item{\code{status}}{character status}
#'}
#' @source \url{https://iatistandard.org/en/iati-standard/203/codelists/}
#' @source \url{https://iatistandard.org/en/iati-standard/203/codelists/earmarkingcategory/}
#' @examples
#' {
#' knitr::kable(head(codeEarmarkingCategory, 10))
Expand Down
16 changes: 15 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,19 @@ globalVariables(unique(c(
# show_partnership:
"participating_org_eng", "participating_org_role_name", "participating_org_type_name",
# show_sectors:
"sector_desc", "sector_pct", "sector_vocabulary", "thiprogramme_lab"
"sector_desc", "sector_pct", "sector_vocabulary", "thiprogramme_lab",
# show_budget_gap:
"budget_gap", "budget_value", "iati_identifier", "transaction_value",
# show_donors:
"provider_org_type_name",
# show_earmarking:
"earmarking_name",
# show_expenditure:
"asylum_seekers", "budget_value", "coa_name", "iati_identifier", "idps", "oip", "ooc", "refugees", "returned_idps", "returned_refugees", "stateless", "transaction_value", "weight",
# show_indicators:
"baseline", "deviation_actual_target", "deviation_color", "progress_baseline", "result_indicator_actual_value", "result_indicator_ascending", "result_indicator_target_value_1",
# show_partnership:
"Accountable",
# show_top_donors:
"transaction_provider_org"
)))
100 changes: 100 additions & 0 deletions R/show_budget_gap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# WARNING - Generated by {fusen} from dev/dev_unhcr_programme.Rmd: do not edit by hand

#' show_budget_gap
#'
#' @param year A numeric value corresponding to the first year of focus until the most recent year within the dataset.
#' @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 tidyselect
#' @import scales
#' @import unhcrthemes
#'
#' @export
#' @return a graph
#' @examples
#' show_budget_gap(year = 2018,
#' ctr_name = "Brazil")
show_budget_gap <- 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.")
}

df <- iati::dataTransaction |>
dplyr::left_join(iati::dataActivity, by= c("iati_identifier"))

if (!is.null(programme_lab)) {
thisprogramme_lab <- programme_lab
thisyear <- year
df <- df |>
# levels(as.factor(df$programmme_lab))
dplyr::filter( programmme_lab == thisprogramme_lab &
year >= thisyear &
transaction_type_name == "Expenditure")
} else if (!is.null(iati_identifier_ops)) {
thisiati_identifier_ops <- iati_identifier_ops
thisyear <- year
df <- df |>
dplyr::filter(iati_identifier_ops == thisiati_identifier_ops &
year >= thisyear &
transaction_type_name == "Expenditure")
} else if (!is.null(ctr_name)) {
thisctr_name <- ctr_name
thisyear <- year
df <- df |>
dplyr::filter( ctr_name == thisctr_name &
year >= thisyear &
transaction_type_name == "Expenditure")
}

df2 <- df |>
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(budget_gap = (budget_value - transaction_value) / budget_value *100)



p <- df2 |>
# dplyr::filter(transaction_value_USD <= 1000000 & transaction_value_USD > 1000) |>
ggplot2::ggplot(ggplot2::aes(x = year, group = 1)) +
ggplot2::geom_line(ggplot2::aes(y = budget_gap,
color = "Budget"),
linewidth = 1.5,
color = "#F592A0") +
ggplot2::scale_y_continuous(
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) +
unhcrthemes::theme_unhcr(grid = "Y", axis = "X",
axis_title = "X", font_size = 18,
legend = FALSE)+
ggplot2::labs(
title = paste0( "Budget Gap (in %)"),
subtitle = paste0("In ", programme_lab, ctr_name,iati_identifier_ops, " recorded since ", year,""),
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative)" )

return(p)

}
97 changes: 97 additions & 0 deletions R/show_contributions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# WARNING - Generated by {fusen} from dev/dev_unhcr_programme.Rmd: do not edit by hand

#' Title
#'
#' @description What’s the breakdown by contribution Type (Un-earmarked, Tightly earmarked, etc.) from Donor Funds by Year?
#'
#' @param year A numeric value corresponding to the first year of focus until the most recent year within the dataset.
#' @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
#'
#' @export
#' @return a graph
#' @examples
#' knitr::kable(iati::dataTransaction |>
#' dplyr::select(aid_type1_name, aid_type1_description) |>
#' dplyr::distinct() |>
#' dplyr::filter(!(is.na(aid_type1_name))))
#' show_contributions(year = 2018,
#' ctr_name = "Brazil")
#' show_contributions(year = 2018,
#' programme_lab = "The Americas")
show_contributions <- 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.")
}

df <- iati::dataTransaction |>
dplyr::left_join(iati::dataActivity, by= c("iati_identifier"))

if (!is.null(programme_lab)) {
thisprogramme_lab <- programme_lab
thisyear <- year
df <- df |>
# levels(as.factor(df$programmme_lab))
dplyr::filter( programmme_lab == thisprogramme_lab &
year >= thisyear &
transaction_type_name == "Incoming Commitment")
} else if (!is.null(iati_identifier_ops)) {
thisiati_identifier_ops <- iati_identifier_ops
thisyear <- year
df <- df |>
dplyr::filter(iati_identifier_ops == thisiati_identifier_ops &
year >= thisyear &
transaction_type_name == "Incoming Commitment")
} else if (!is.null(ctr_name)) {
thisctr_name <- ctr_name
thisyear <- year
df <- df |>
dplyr::filter( ctr_name == thisctr_name &
year >= thisyear &
transaction_type_name == "Incoming Commitment")
}

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

p <- df2 |>
# dplyr::filter(transaction_value_USD <= 1000000 & transaction_value_USD > 1000) |>
ggplot2::ggplot(ggplot2::aes(y = transaction_value_USD ,
x = year,
fill = aid_type1_name)) +
ggplot2::geom_bar(alpha = 0.9, stat = "identity") +
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()) ) +
# scale_x_continuous(labels = scales::label_number(scale_cut = cut_short_scale())) +
# ggplot2::facet_wrap(~ trans_year) +
unhcrthemes::theme_unhcr(grid = "Y", axis = "X", axis_title = "X", font_size = 18)+
ggplot2::labs(
title = paste0("Commitment vs Contribution type (in USD)"),
subtitle = paste0("Recorded in ", programme_lab, ctr_name,iati_identifier_ops, " since ", year,""),
x = "",
y = "",
caption = "Data Source: UNHCR IATI (International Aid Transparency Initiative)" )

return(p)
}
Loading

0 comments on commit 9a43cd9

Please sign in to comment.