1010prepare_geo_data <- function (data ,
1111 country_code = c(" DE" ),
1212 benchmark = benchmarks(),
13- mode = c(
14- " equal_weight" ,
15- " worst_case" ,
16- " best_case"
17- )) {
13+ mode = modes(),
14+ scenario = scenarios(),
15+ year = years()) {
1816 benchmark <- arg_match(benchmark )
19- mode <- arg_match(mode )
17+ mode <- mode | >
18+ arg_match() | >
19+ switch_mode_emission_profile()
2020 country_code <- arg_match(country_code )
21+ scenario <- arg_match(scenario )
22+ year <- year
2123
2224 crucial <- c(
2325 aka(" risk_category" ),
24- " company_name " ,
26+ aka( " companies_id " ) ,
2527 " postcode" ,
26- " benchmark"
28+ " benchmark" ,
29+ " scenario" ,
30+ aka(" year" )
2731 )
2832 data | > check_crucial_names(names_matching(data , crucial ))
29- risk_var <- names_matching (data , aka(" risk_category" ))
33+ risk_var <- get_colname (data , aka(" risk_category" ))
3034 data <- data | >
3135 mutate(risk_category_var = as_risk_category(data [[risk_var ]]))
3236
@@ -50,7 +54,11 @@ prepare_geo_data <- function(data,
5054
5155 # merge shapefile with financial data
5256 geo <- data | >
53- filter(benchmark == .env $ benchmark ) | >
57+ filter(
58+ .data $ benchmark == .env $ benchmark ,
59+ .data $ scenario == .env $ scenario ,
60+ .data $ year == .env $ year
61+ ) | >
5462 left_join(shp_1 , by = " postcode" ) | >
5563 st_as_sf()
5664
@@ -59,7 +67,6 @@ prepare_geo_data <- function(data,
5967 list (shp_1 , aggregated_data )
6068}
6169
62-
6370# ' Aggregate Geo Data
6471# '
6572# ' @param geo A data frame containing geographical data.
@@ -84,52 +91,28 @@ prepare_geo_data <- function(data,
8491# '
8592# ' aggregate_geo(geo, mode = "worst_case")
8693aggregate_geo <- function (geo , mode ) {
87- if (mode %in% c(" worst_case" , " best_case" )) {
88- aggregated_data <- geo | >
89- group_by(.data $ postcode , .data $ company_name ) | >
90- mutate(
91- # Choose the worst or best risk category and set the others to 0.
92- proportion = calculate_case_proportions(.data $ risk_category_var , mode )
93- ) | >
94- group_by(.data $ postcode , .data $ risk_category_var ) | >
95- summarize(proportion = sum(.data $ proportion )) | >
96- ungroup()
97- } else if (mode == " equal_weight" ) {
98- aggregated_data <- geo | >
99- group_by(.data $ postcode , .data $ risk_category_var ) | >
100- summarize(count = n()) | >
101- # Do not group by company here since all of them have equal weights.
102- group_by(.data $ postcode ) | >
103- mutate(proportion = .data $ count / sum(.data $ count )) | >
104- ungroup()
105- }
94+ aggregated_data <- geo | >
95+ group_by(.data $ postcode , .data $ risk_category_var ) | >
96+ summarise(total_mode = sum(.data [[mode ]])) | >
97+ group_by(.data $ postcode ) | >
98+ mutate(proportion = total_mode / sum(total_mode )) | >
99+ ungroup()
106100
107- # apply custom_gradient_color to each row
101+ # Pivot
108102 aggregated_data <- aggregated_data | >
109103 pivot_wider(names_from = " risk_category_var" , values_from = " proportion" , values_fill = 0 ) | >
110- mutate(color = pmap(list (.data $ high , .data $ medium , .data $ low ), custom_gradient_color ))
111- }
104+ filter(.data $ total_mode != 0 )
112105
113- # ' Calculate Proportions for Worst or Best Case Scenarios
114- # '
115- # ' @param categories A factor vector of risk categories.
116- # ' @param mode A character string specifying the mode.
117- # '
118- # ' @return A numeric vector representing the calculated proportions for each
119- # ' category.
120- # '
121- # ' @examples
122- # ' categories <- as_risk_category(c("low", "medium", "medium", "high"))
123- # ' calculate_case_proportions(categories, mode = "worst_case")
124- # ' @noRd
125- calculate_case_proportions <- function (categories , mode ) {
126- if (mode == " worst_case" ) {
127- extreme_risk <- levels(categories )[max(as.integer(categories ))]
128- } else if (mode == " best_case" ) {
129- extreme_risk <- levels(categories )[min(as.integer(categories ))]
130- }
131-
132- is_extreme <- categories == extreme_risk
133- proportions <- ifelse(is_extreme , 1 / sum(is_extreme ), 0 )
134- proportions
106+ # Calculate color row by row
107+ aggregated_data <- aggregated_data | >
108+ group_by(.data $ postcode ) | >
109+ summarise(
110+ total_mode = add(.data $ total_mode ),
111+ geometry = first(.data $ geometry ),
112+ low = add(.data $ low ),
113+ medium = add(.data $ medium ),
114+ high = add(.data $ high )
115+ ) | >
116+ mutate(color = pmap(list (.data $ high , .data $ medium , .data $ low ), custom_gradient_color ))
117+ aggregated_data
135118}
0 commit comments