generated from RMI-PACTA/workflow.template.pacta
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
upkeepmaintenance, infrastructure, and similarmaintenance, infrastructure, and similar
Description
This function plots the matching success rate, which is useful to all users that want to run matching (not just multi.loanbook).
We might consider exporting it from a different repo so that it can be easily accessed by these users.
pacta.multi.loanbook/R/plots.R
Lines 1 to 94 in cab8d28
| plot_match_success_rate <- function(data, | |
| metric_type = c("absolute", "relative"), | |
| match_success_type = c("n", "outstanding", "credit_limit"), | |
| currency, | |
| by_group) { | |
| # validate inputs | |
| assert_inherits(data, "data.frame") | |
| expected_cols <- c( | |
| by_group, | |
| "sector", | |
| "matched", | |
| "match_success_type", | |
| "match_success_rate", | |
| "metric_type" | |
| ) | |
| assert_expected_columns(data, expected_cols, desc = "Input") | |
| assert_inherits(metric_type, "character") | |
| assert_inherits(match_success_type, "character") | |
| assert_length(currency, 1L) | |
| assert_inherits(currency, "character") | |
| data <- data %>% | |
| dplyr::filter(.data[["sector"]] != "not in scope") %>% | |
| dplyr::filter(.data[["metric_type"]] == .env[["metric_type"]]) %>% | |
| dplyr::filter(.data[["match_success_type"]] == .env[["match_success_type"]]) | |
| # plot design | |
| fill_scale <- c( | |
| "Matched" = "#00c082", | |
| "Not matched" = "#a63d57" | |
| ) | |
| theme_match_success <- ggplot2::theme( | |
| legend.position = "top", | |
| legend.title = ggplot2::element_blank(), | |
| axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust=1) | |
| ) | |
| # plot description | |
| title <- r2dii.plot::to_title(glue::glue("{metric_type} Match Success Rate")) | |
| if (match_success_type == "n") { | |
| subtitle <- "number of loans by sector" | |
| } else if (match_success_type == "outstanding") { | |
| subtitle <- "loan size outstanding by sector" | |
| } else { | |
| subtitle <- "credit limit by sector" | |
| } | |
| subtitle <- r2dii.plot::to_title(glue::glue("{subtitle} and {by_group}")) | |
| if (match_success_type == "n") { | |
| y_label <- "Match success rate (n)" | |
| } else if (match_success_type == "outstanding") { | |
| y_label <- "Match success rate: loan size outstanding" | |
| } else { | |
| y_label <- "Match success rate: credit limit" | |
| } | |
| if (metric_type == "absolute") { | |
| y_label <- glue::glue("{y_label} (in {currency})") | |
| } else { | |
| y_label <- glue::glue("{y_label} (share of total)") | |
| } | |
| # plot | |
| plot <- data %>% | |
| ggplot2::ggplot( | |
| ggplot2::aes( | |
| x = r2dii.plot::to_title(.data[["sector"]]), | |
| y = .data[["match_success_rate"]], | |
| fill = .data[["matched"]] | |
| ) | |
| ) + | |
| ggplot2::geom_col( | |
| position = ggplot2::position_stack(reverse = TRUE) | |
| ) + | |
| ggplot2::scale_fill_manual(values = fill_scale) + | |
| ggplot2::labs( | |
| x = "Sector", | |
| y = y_label, | |
| title = title, | |
| subtitle = subtitle | |
| ) + | |
| ggplot2::theme_bw() + | |
| theme_match_success + | |
| ggplot2::facet_wrap(ggplot2::vars(r2dii.plot::to_title(.data[[by_group]]))) | |
| plot | |
| } |
Metadata
Metadata
Assignees
Labels
upkeepmaintenance, infrastructure, and similarmaintenance, infrastructure, and similar