Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider exporting plot_match_success_rate from r2dii.plot (or r2dii.match?) #386

Open
jdhoffa opened this issue Jan 20, 2025 · 1 comment
Labels
upkeep maintenance, infrastructure, and similar

Comments

@jdhoffa
Copy link
Member

jdhoffa commented Jan 20, 2025

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.

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
}

@jdhoffa
Copy link
Member Author

jdhoffa commented Jan 20, 2025

Since this package already depends on r2dii.plot and r2dii.match we could still just call it from here.

@jdhoffa jdhoffa added the upkeep maintenance, infrastructure, and similar label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upkeep maintenance, infrastructure, and similar
Projects
None yet
Development

No branches or pull requests

1 participant