From b5a853e7eb663c6693807b29e735b700dcb8ef9b Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Mon, 3 Mar 2025 00:27:08 +0100 Subject: [PATCH] add `plot_match_success_rate()` function --- NAMESPACE | 2 ++ R/plot_match_success_rate.R | 47 ++++++++++++++++++++++++++++++++++ man/plot_match_success_rate.Rd | 32 +++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 R/plot_match_success_rate.R create mode 100644 man/plot_match_success_rate.Rd diff --git a/NAMESPACE b/NAMESPACE index 7ce8e65f..04642009 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export("%>%") export(plot_emission_intensity) +export(plot_match_success_rate) export(plot_techmix) export(plot_trajectory) export(prep_emission_intensity) @@ -22,6 +23,7 @@ export(spell_out_technology) export(theme_2dii) export(to_title) import(ggplot2) +importFrom(dplyr,"%>%") importFrom(dplyr,across) importFrom(dplyr,all_of) importFrom(dplyr,arrange) diff --git a/R/plot_match_success_rate.R b/R/plot_match_success_rate.R new file mode 100644 index 00000000..55790cd0 --- /dev/null +++ b/R/plot_match_success_rate.R @@ -0,0 +1,47 @@ +#' Title +#' +#' @param .data A data frame like the outputs of +#' `r2dii.match::calculate_match_success_rate()`. +#' @param ... Additional grouping columns. +#' @param .x The name of the column to be used as the x-axis. +#' +#' @returns An object of class "ggplot". +#' +#' @export +#' +#' @examples +#' coverage <- +#' r2dii.data::loanbook_demo %>% +#' r2dii.match::match_name(r2dii.data::abcd_demo) %>% +#' r2dii.match::prioritize() %>% +#' r2dii.match::calculate_match_success_rate(loanbook = r2dii.data::loanbook_demo) +#' +#' plot_match_success_rate(coverage) +#' +#' @importFrom dplyr %>% + +plot_match_success_rate <- function(.data, ..., .x = NULL) { + if (is.null(.x)) { + .x <- as.symbol("matched") + } else { + .x <- as.symbol(.x) + } + + .data %>% + dplyr::summarize( + loan_size_outstanding = sum(.data$loan_size_outstanding, na.rm = TRUE), + .by = c("matched", ...) + ) %>% + dplyr::arrange(.data[["matched"]], ...) %>% + ggplot2::ggplot() + + ggplot2::geom_col( + mapping = ggplot2::aes( + x = {{.x}}, + y = .data[["loan_size_outstanding"]], + fill = .data[["matched"]] + ) + ) + + theme_2dii() + + scale_fill_r2dii() + + ggplot2::theme(legend.position = "top") +} diff --git a/man/plot_match_success_rate.Rd b/man/plot_match_success_rate.Rd new file mode 100644 index 00000000..80e37217 --- /dev/null +++ b/man/plot_match_success_rate.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_match_success_rate.R +\name{plot_match_success_rate} +\alias{plot_match_success_rate} +\title{Title} +\usage{ +plot_match_success_rate(.data, ..., .x = NULL) +} +\arguments{ +\item{.data}{A data frame like the outputs of +\code{r2dii.match::calculate_match_success_rate()}.} + +\item{...}{Additional grouping columns.} + +\item{.x}{The name of the column to be used as the x-axis.} +} +\value{ +An object of class "ggplot". +} +\description{ +Title +} +\examples{ +coverage <- +r2dii.data::loanbook_demo \%>\% + r2dii.match::match_name(r2dii.data::abcd_demo) \%>\% + r2dii.match::prioritize() \%>\% + r2dii.match::calculate_match_success_rate(loanbook = r2dii.data::loanbook_demo) + +plot_match_success_rate(coverage) + +}