88# ' @noRd
99# '
1010prepare_geo_data <- function (data ,
11- country_code = c(" DE" ),
11+ country_code = c(" DE" , " AT " , " FR " , " NL " , " ES " ),
1212 grouping_emission = grouping_emission(),
13- mode = modes(),
1413 scenario = scenarios(),
1514 year = years(),
1615 risk_category = risk_category()) {
17- grouping_emission <- arg_match(grouping_emission )
1816 risk_category <- arg_match(risk_category )
1917 country_code <- arg_match(country_code )
20- scenario <- arg_match(scenario )
21- year <- year
2218
2319 crucial <- c(
2420 aka(" risk_category" ),
@@ -32,10 +28,11 @@ prepare_geo_data <- function(data,
3228
3329 shp_0 <- get_eurostat_geospatial(
3430 resolution = 10 ,
35- nuts_level = 3 ,
36- year = 2016 ,
31+ nuts_level = " all " ,
32+ year = 2021 ,
3733 crs = 3035
38- )
34+ ) | >
35+ filter(! (geo %in% c(" FRY10" , " FRY20" , " FRY30" , " FRY40" , " FRY50" )))
3936
4037 # filter for the specified country
4138 shp_1 <- shp_0 | >
@@ -46,29 +43,41 @@ prepare_geo_data <- function(data,
4643
4744 # merge to have zip codes with NUTS file
4845 shp_1 <- shp_1 | >
49- inner_join(nuts_de , by = " geo" ) | >
50- mutate(postcode = as.character(postcode ))
46+ inner_join(nuts_all , by = " geo" )
47+
48+ filtered_geo_data <- data
49+
50+ # Apply filtering only if the corresponding argument is not NULL
51+ if (! is.null(grouping_emission )) {
52+ filtered_geo_data <- filtered_geo_data | >
53+ filter(.data $ grouping_emission == .env $ grouping_emission )
54+ }
55+
56+ if (! is.null(scenario )) {
57+ filtered_geo_data <- filtered_geo_data | >
58+ filter(.data $ scenario == .env $ scenario )
59+ }
60+
61+ if (! is.null(year )) {
62+ filtered_geo_data <- filtered_geo_data | >
63+ filter(.data $ year == .env $ year )
64+ }
5165
5266 # merge shapefile with financial data
53- geo <- data | >
54- filter(
55- .data $ grouping_emission == .env $ grouping_emission ,
56- .data $ scenario == .env $ scenario ,
57- .data $ year == .env $ year
58- ) | >
67+ geo <- filtered_geo_data | >
68+ distinct() | >
5969 left_join(shp_1 , by = " postcode" ) | >
6070 st_as_sf()
6171
62- aggregated_data <- aggregate_geo(geo , mode , risk_category )
72+ aggregated_data <- aggregate_geo(geo , risk_category )
6373
6474 list (shp_1 , aggregated_data )
6575}
6676
6777# ' Aggregate Geo Data
6878# '
69- # ' @param geo A data frame containing geographical data.
70- # ' @param mode The mode to plot. It can be one of "equal_weight", "worst_case"
71- # ' or "best_case". If nothing is chosen, "equal_weight" is the default mode.
79+ # ' @param geo_example A data frame containing geographical data.
80+ # ' @param risk_category The risk category.
7281# '
7382# ' @return A data frame with aggregated data, with the colors proportional to
7483# ' the risks.
@@ -78,21 +87,21 @@ prepare_geo_data <- function(data,
7887# ' library(tibble)
7988# '
8089# ' # Create a sample geo_data tibble
81- # ' geo <- tibble(
82- # ' postcode = c("1", "2", "3"),
90+ # ' geo_example <- tibble(
91+ # ' geo = c("1", "2", "3"),
8392# ' company_name = c("A", "B", "C"),
8493# ' emission_profile = factor(c("low", "medium", "high"),
8594# ' levels = c("low", "medium", "high")
8695# ' )
8796# ' )
8897# '
89- # ' aggregate_geo(geo, mode = "emissions_profile_worst_case" , risk_category = "emission_category")
90- aggregate_geo <- function (geo , mode , risk_category ) {
91- aggregated_data <- geo | >
92- group_by(.data $ postcode , .data [[risk_category ]]) | >
93- summarise(total_mode = sum (.data [[ mode ]] )) | >
94- group_by(.data $ postcode ) | >
95- mutate(proportion = total_mode / sum(total_mode )) | >
98+ # ' aggregate_geo(geo_example , risk_category = "emission_category")
99+ aggregate_geo <- function (geo_data , risk_category ) {
100+ aggregated_data <- geo_data | >
101+ group_by(.data $ geo , .data [[risk_category ]]) | >
102+ summarise(total_mode = n_distinct (.data $ product , na.rm = TRUE )) | >
103+ group_by(.data $ geo ) | >
104+ mutate(proportion = .data $ total_mode / sum(.data $ total_mode , na.rm = TRUE )) | >
96105 ungroup()
97106
98107 # Pivot
@@ -102,13 +111,13 @@ aggregate_geo <- function(geo, mode, risk_category) {
102111
103112 # Calculate color row by row
104113 aggregated_data <- aggregated_data | >
105- group_by(.data $ postcode ) | >
114+ group_by(.data $ geo ) | >
106115 summarise(
107116 total_mode = add(.data $ total_mode ),
108117 geometry = first(.data $ geometry ),
109- low = add(.data $ low ),
110- medium = add(.data $ medium ),
111- high = add(.data $ high )
118+ low = if ( " low " %in% names(pick(everything()))) add(.data $ low ) else 0.0 ,
119+ medium = if ( " medium " %in% names(pick(everything()))) add(.data $ medium ) else 0.0 ,
120+ high = if ( " high " %in% names(pick(everything()))) add(.data $ high ) else 0.0
112121 ) | >
113122 mutate(color = pmap(list (.data $ high , .data $ medium , .data $ low ), custom_gradient_color ))
114123 aggregated_data
0 commit comments