Skip to content

Commit f93b60b

Browse files
Adapt German map function to latest version of tiltIndicatorAfter (#140)
* Refactor * refactor * refactor * refactor * refactor * Add tests
1 parent fb7a172 commit f93b60b

32 files changed

+854
-772
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ Config/testthat/edition: 3
4747
Encoding: UTF-8
4848
LazyData: true
4949
Roxygen: list(markdown = TRUE)
50-
RoxygenNote: 7.3.1
50+
RoxygenNote: 7.3.2
5151
BugReports: https://github.com/2DegreesInvesting/tiltPlot/issues

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ export(bar_plot_emission_profile_financial)
55
export(benchmarks)
66
export(example_financial)
77
export(example_without_financial)
8+
export(grouping_emission)
89
export(map_region_risk)
910
export(modes)
1011
export(plot_sankey)
12+
export(risk_category)
1113
export(scatter_plot_financial)
1214
export(scenarios)
1315
export(scenarios_financial)

R/bar_plot_emission_profile.R

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
#' the emission risk profiles risks for one or several benchmarks.
55
#'
66
#' @param data A data frame like [without_financial].
7-
#' @param benchmarks A character vector specifying the benchmarks for which the
7+
#' @param grouping_emission A character vector specifying the benchmarks for which the
88
#' emission profiles will be plotted. The user can choose from one to several
99
#' benchmark(s) to be plotted.
1010
#' @param mode A character vector: `r toString(modes())`.
1111
#' @param scenario A character vector: `r toString(scenarios())`.
1212
#' @param year A character vector: `r toString(years())`.
13+
#' @param risk_category A character vector.
1314
#'
1415
#' @return A [ggplot] object.
1516
#'
1617
#' @export
1718
#'
1819
#' @examples
19-
#' benchmarks <- c("all", "unit", "isic_4digit")
20-
#' bar_plot_emission_profile(without_financial, benchmarks)
20+
#' grouping_emission <- c("unit")
21+
#' bar_plot_emission_profile(without_financial, grouping_emission, risk_category = "emission_category")
2122
bar_plot_emission_profile <- function(data,
22-
benchmarks = benchmarks(),
23+
grouping_emission = grouping_emission(),
2324
mode = modes(),
2425
scenario = scenarios(),
25-
year = years()) {
26-
benchmarks <- arg_match(benchmarks, multiple = TRUE)
26+
year = years(),
27+
risk_category = risk_category()) {
28+
grouping_emission <- arg_match(grouping_emission, multiple = TRUE)
2729
mode <- mode |>
2830
arg_match() |>
2931
switch_mode_emission_profile()
@@ -32,8 +34,9 @@ bar_plot_emission_profile <- function(data,
3234

3335
data |>
3436
check_bar_plot_emission_profile(mode) |>
35-
prepare_bar_plot_emission_profile(benchmarks = benchmarks, mode = mode, scenario = scenario, year = year) |>
36-
plot_bar_plot_emission_profile_impl()
37+
prepare_bar_plot_emission_profile(grouping_emission = grouping_emission, mode = mode,
38+
scenario = scenario, year = year, risk_category = risk_category) |>
39+
plot_bar_plot_emission_profile_impl(risk_category = risk_category)
3740
}
3841

3942
#' Check bar plot plot without financial data
@@ -44,35 +47,31 @@ bar_plot_emission_profile <- function(data,
4447
#' @noRd
4548
check_bar_plot_emission_profile <- function(data, mode) {
4649
crucial <- c(
47-
"benchmark",
50+
"grouping_emission",
4851
mode,
4952
aka("risk_category")
5053
)
5154
data |> check_crucial_names(names_matching(data, crucial))
5255
}
5356

54-
#' Prepare emission profile proportions for specific benchmarks
57+
#' Prepare emission profile proportions for specific grouping_emission
5558
#'
5659
#' @param data A data frame.
57-
#' @param benchmarks A character vector.
60+
#' @param grouping_emission A character vector.
5861
#' @param mode A character vector.
62+
#' @param risk_category A character vector.
5963
#'
6064
#' @return A data frame.
6165
#'
6266
#' @noRd
63-
prepare_bar_plot_emission_profile <- function(data, benchmarks, mode, scenario, year) {
64-
risk_var <- get_colname(data, aka("risk_category"))
65-
66-
data <- data |>
67-
mutate(risk_category_var = as_risk_category(.data[[risk_var]]))
68-
67+
prepare_bar_plot_emission_profile <- function(data, grouping_emission, mode, scenario, year, risk_category) {
6968
data <- data |>
70-
filter((.data$benchmark %in% .env$benchmarks &
69+
filter((.data$grouping_emission %in% .env$grouping_emission &
7170
.data$scenario == .env$scenario &
7271
.data$year == .env$year)) |>
73-
group_by(.data$risk_category_var, .data$benchmark) |>
72+
group_by(.data[[risk_category]], .data$grouping_emission) |>
7473
summarise(total_mode = sum(.data[[mode]])) |>
75-
group_by(.data$benchmark) |>
74+
group_by(.data$grouping_emission) |>
7675
mutate(proportion = total_mode / sum(total_mode))
7776

7877
data
@@ -81,11 +80,12 @@ prepare_bar_plot_emission_profile <- function(data, benchmarks, mode, scenario,
8180
#' Implementation of the emission profile bar plot
8281
#'
8382
#' @param data A data frame.
83+
#' @param risk_category A character vector.
8484
#'
8585
#' @return A [ggplot] object.
8686
#' @noRd
87-
plot_bar_plot_emission_profile_impl <- function(data) {
88-
ggplot(data, aes(x = .data$proportion, y = .data$benchmark, fill = .data$risk_category_var)) +
87+
plot_bar_plot_emission_profile_impl <- function(data, risk_category) {
88+
ggplot(data, aes(x = .data$proportion, y = .data$grouping_emission, fill = .data[[risk_category]])) +
8989
geom_col(position = position_stack(reverse = TRUE), width = width_bar()) +
9090
fill_score_colors() +
9191
theme_tiltplot() +

R/bar_plot_emission_profile_financial.R

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,53 @@
44
#' the emission risk profiles risks for one or several benchmarks.
55
#'
66
#' @param data A data frame like [financial].
7-
#' @param benchmarks A character vector specifying the benchmarks for which the
7+
#' @param grouping_emission A character vector specifying the benchmarks for which the
88
#' emission profiles will be plotted. The user can choose from one to several
99
#' benchmark(s) to be plotted.
1010
#' @param mode The mode of financial data to plot.
1111
#' It can be one of "equal_weight", "worst_case" or "best_case".
1212
#' If nothing is chosen, "equal_weight" is the default case.
13+
#' @param risk_category A character vector.
1314
#'
1415
#' @return A [ggplot] object.
1516
#' @export
1617
#'
1718
#' @examples
18-
#' benchmarks <- c("all", "unit", "isic_4digit")
19-
#' bar_plot_emission_profile_financial(financial, benchmarks, "equal_weight")
19+
#' grouping_emission <- c("all", "unit", "isic_4digit")
20+
#' bar_plot_emission_profile_financial(financial, grouping_emission, "equal_weight", risk_category = "emission_category")
2021
bar_plot_emission_profile_financial <- function(data,
21-
benchmarks = benchmarks(),
22+
grouping_emission = grouping_emission(),
2223
mode = c(
2324
"equal_weight",
2425
"worst_case",
2526
"best_case"
26-
)) {
27-
benchmarks <- arg_match(benchmarks, multiple = TRUE)
27+
),
28+
risk_category = risk_category()) {
29+
grouping_emission <- arg_match(grouping_emission, multiple = TRUE)
2830
mode <- arg_match(mode)
2931

3032
crucial <- c(
3133
"amount_total",
3234
"bank_id",
3335
"company_name",
3436
aka("risk_category"),
35-
"benchmark",
37+
"grouping_emission",
3638
"equal_weight_finance",
3739
"worst_case_finance",
3840
"best_case_finance"
3941
)
4042
data |> check_crucial_names(names_matching(data, crucial))
4143

42-
risk_var <- names_matching(data, aka("risk_category"))
43-
44-
data <- data |>
45-
mutate(risk_category_var = as_risk_category(data[[risk_var]]))
46-
4744
mode_var <- switch_mode(mode)
4845

4946
data <- data |>
5047
calc_benchmark_emission_profile_financial(
51-
risk_var,
52-
benchmarks,
48+
risk_category,
49+
grouping_emission,
5350
mode_var
5451
)
5552

56-
ggplot(data, aes(x = .data$percentage_total, y = .data$benchmark, fill = .data$risk_category_var)) +
53+
ggplot(data, aes(x = .data$percentage_total, y = .data$grouping_emission, fill = .data[[risk_category]])) +
5754
geom_col(position = position_stack(reverse = TRUE), width = width_bar()) +
5855
fill_score_colors() +
5956
theme_tiltplot() +
@@ -64,28 +61,28 @@ bar_plot_emission_profile_financial <- function(data,
6461
#' data
6562
#'
6663
#' @param data A data frame.
67-
#' @param risk_var A character vector.
64+
#' @param risk_category A character vector.
6865
#' @param benchmarks A character vector.
6966
#' @param mode_var A character vector.
7067
#'
7168
#' @return A data frame.
7269
#'
7370
#' @noRd
7471
calc_benchmark_emission_profile_financial <- function(data,
75-
risk_var,
72+
risk_category,
7673
benchmarks,
7774
mode_var) {
7875
total_amount_portfolio <- data |>
79-
filter(.data$benchmark %in% benchmarks) |>
76+
filter(.data$grouping_emission %in% benchmarks) |>
8077
distinct(.data$bank_id, .data$company_name, .keep_all = TRUE) |>
8178
summarise(total_amount_portfolio = sum(.data$amount_total, na.rm = TRUE)) |>
8279
pull()
8380

8481
data <- data |>
85-
filter(.data$benchmark %in% benchmarks) |>
86-
distinct(.data$bank_id, .data$company_name, .data$ep_product, .data$benchmark, .keep_all = TRUE) |>
82+
filter(.data$grouping_emission %in% benchmarks) |>
83+
distinct(.data$bank_id, .data$company_name, .data$ep_product, .data$grouping_emission, .keep_all = TRUE) |>
8784
mutate(proportion = .data[[mode_var]] / total_amount_portfolio) |>
88-
group_by(.data$risk_category_var, .data$company_name, .data$benchmark) |>
85+
group_by(.data[[risk_category]], .data$company_name, .data$grouping_emission) |>
8986
summarise(percentage = sum(.data$proportion, na.rm = TRUE), .groups = "keep") |>
9087
summarise(percentage_total = sum(.data$percentage, na.rm = TRUE))
9188

R/example_data.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
default_financial <- function(bank_id = "a",
22
amount_total = 10,
33
company_name = "b",
4-
emission_profile = "medium",
5-
benchmark = "all",
4+
emission_category = "medium",
5+
grouping_emission = "all",
66
profile_ranking = 0.1,
77
transition_risk_score = 0.1,
88
equal_weight_finance = 10,
@@ -12,8 +12,8 @@ default_financial <- function(bank_id = "a",
1212
bank_id = bank_id,
1313
amount_total = amount_total,
1414
company_name = company_name,
15-
emission_profile = emission_profile,
16-
benchmark = benchmark,
15+
emission_category = emission_category,
16+
grouping_emission = grouping_emission,
1717
profile_ranking = profile_ranking,
1818
transition_risk_score = transition_risk_score,
1919
equal_weight_finance = equal_weight_finance,
@@ -23,22 +23,22 @@ default_financial <- function(bank_id = "a",
2323
}
2424

2525
default_without_financial <- function(company_name = "a",
26-
emission_profile = "medium",
27-
benchmark = "all",
26+
emission_category = c("low", "medium", "high"),
27+
grouping_emission = "all",
2828
scenario = "1.5C RPS",
2929
year = 2030,
30-
equal_weight_emission_profile = 0.1,
31-
worst_case_emission_profile = 0.1,
32-
best_case_emission_profile = 0.1) {
30+
emissions_profile_equal_weight = 0.1,
31+
emissions_profile_worst_case = 0.1,
32+
emissions_profile_best_case = 0.1) {
3333
tibble(
3434
company_name = company_name,
35-
emission_profile = emission_profile,
36-
benchmark = benchmark,
35+
emission_category = emission_category,
36+
grouping_emission = grouping_emission,
3737
scenario = scenario,
3838
year = year,
39-
equal_weight_emission_profile = equal_weight_emission_profile,
40-
worst_case_emission_profile = worst_case_emission_profile,
41-
best_case_emission_profile = best_case_emission_profile
39+
emissions_profile_equal_weight = emissions_profile_equal_weight,
40+
emissions_profile_worst_case = emissions_profile_worst_case,
41+
emissions_profile_best_case = emissions_profile_best_case
4242
)
4343
}
4444

R/map_region_risk.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
#' @param data A data frame like [without_financial]
55
#' @param country_code Country code (ISO 3166 alpha-2) for which the map will be
66
#' plotted.
7-
#' @param benchmark The mode of benchmark to plot.
7+
#' @param grouping_emission The mode of benchmark to plot.
88
#' It can be one of "all", "unit" or "tilt_sector", "unit_tilt_sector",
99
#' "isic_4digit" or "unit_isic_4digit". If nothing is chosen, "all" is the
1010
#' default mode.
1111
#' @param mode The mode to plot. It can be one of "equal_weight", "worst_case"
1212
#' or "best_case". If nothing is chosen, "equal_weight" is the default mode.
1313
#' @param scenario A character vector: `r toString(scenarios())`.
1414
#' @param year A character vector: `r toString(years())`.
15+
#' @param risk_category A character vector.
1516
#'
1617
#' @return A ggplot2 object representing the country data plot.
1718
#' @export
@@ -24,17 +25,19 @@
2425
map_region_risk <- function(data,
2526
# TODO: plot for other countries
2627
country_code = c("DE"),
27-
benchmark = benchmarks(),
28+
grouping_emission = grouping_emission(),
2829
mode = modes(),
2930
scenario = scenarios(),
30-
year = years()) {
31+
year = years(),
32+
risk_category = risk_category()) {
3133
prepared_data <- prepare_geo_data(
3234
data,
3335
country_code,
34-
benchmark,
36+
grouping_emission,
3537
mode,
3638
scenario,
37-
year
39+
year,
40+
risk_category
3841
)
3942
shp_1 <- prepared_data[[1]]
4043
aggregated_data <- prepared_data[[2]]

R/plot_sankey.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
#'
1010
#' @examples
1111
#' # Plot with equal weight and with company name
12-
#' plot_sankey(financial)
12+
#' plot_sankey(financial, grouping_emission = "all")
1313
#'
14-
#' # Plot with best_case weight and benchmark "all".
15-
#' plot_sankey(financial, benchmark = "all", mode = "best_case")
14+
#' # Plot with best_case weight and grouping_emission "all".
15+
#' plot_sankey(financial, grouping_emission = "all", mode = "best_case")
1616
plot_sankey <- function(data,
1717
with_company = TRUE,
18-
benchmark = benchmarks(),
18+
grouping_emission = grouping_emission(),
1919
mode = c("equal_weight", "worst_case", "best_case")) {
2020
mode <- arg_match(mode)
21-
benchmark <- arg_match(benchmark)
21+
grouping_emission <- arg_match(grouping_emission)
2222

2323
crucial <- c(
2424
aka("risk_category"),
@@ -30,7 +30,7 @@ plot_sankey <- function(data,
3030
risk_var <- names_matching(data, aka("risk_category"))
3131

3232
data <- data |>
33-
filter(.data$benchmark == .env$benchmark) |>
33+
filter(.data$grouping_emission == .env$grouping_emission) |>
3434
distinct(.data$bank_id,
3535
.data$company_name,
3636
.data$ep_product,

0 commit comments

Comments
 (0)