Skip to content

Commit d8cb60e

Browse files
Add maps for all countries (#142)
* Add maps for all countries * refactor * refactor * refactor * refactor * refactor * refactor * refactor * Resolve test
1 parent 5570277 commit d8cb60e

27 files changed

+36583
-99
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
^renv$
2+
^renv\.lock$
13
^tiltPlot\.Rproj$
24
^\.Rproj\.user$
35
^README\.Rmd$

.Rprofile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source("renv/activate.R")

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ importFrom(dplyr,inner_join)
2626
importFrom(dplyr,left_join)
2727
importFrom(dplyr,mutate)
2828
importFrom(dplyr,n)
29+
importFrom(dplyr,n_distinct)
30+
importFrom(dplyr,pick)
2931
importFrom(dplyr,pull)
32+
importFrom(dplyr,rename)
3033
importFrom(dplyr,select)
3134
importFrom(dplyr,summarise)
3235
importFrom(dplyr,summarize)
@@ -87,6 +90,8 @@ importFrom(tibble,tibble)
8790
importFrom(tibble,tribble)
8891
importFrom(tidyr,drop_na)
8992
importFrom(tidyr,pivot_wider)
93+
importFrom(tidyselect,all_of)
94+
importFrom(tidyselect,everything)
9095
importFrom(tidyselect,matches)
9196
importFrom(tiltIndicator,example_data_factory)
9297
importFrom(tiltIndicatorAfter,profile_emissions)

R/bar_plot_emission_profile.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ prepare_bar_plot_emission_profile <- function(data, grouping_emission, mode, sce
7272
group_by(.data[[risk_category]], .data$grouping_emission) |>
7373
summarise(total_mode = sum(.data[[mode]])) |>
7474
group_by(.data$grouping_emission) |>
75-
mutate(proportion = total_mode / sum(total_mode))
75+
mutate(proportion = .data$total_mode / sum(.data$total_mode))
7676

7777
data
7878
}

R/bar_plot_emission_profile_financial.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
#'
1818
#' @examples
1919
#' grouping_emission <- c("all", "unit", "isic_4digit")
20-
#' bar_plot_emission_profile_financial(financial, grouping_emission, "equal_weight", risk_category = "emission_category")
20+
#' bar_plot_emission_profile_financial(financial, grouping_emission,
21+
#' "equal_weight",
22+
#' risk_category = "emission_category"
23+
#' )
2124
bar_plot_emission_profile_financial <- function(data,
2225
grouping_emission = grouping_emission(),
2326
mode = c(

R/example_data.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ default_without_financial <- function(company_name = "a",
2727
grouping_emission = "all",
2828
scenario = "1.5C RPS",
2929
year = 2030,
30+
product = c("a", "b", "c"),
3031
emissions_profile_equal_weight = 0.1,
3132
emissions_profile_worst_case = 0.1,
3233
emissions_profile_best_case = 0.1) {
@@ -36,6 +37,7 @@ default_without_financial <- function(company_name = "a",
3637
grouping_emission = grouping_emission,
3738
scenario = scenario,
3839
year = year,
40+
product = product,
3941
emissions_profile_equal_weight = emissions_profile_equal_weight,
4042
emissions_profile_worst_case = emissions_profile_worst_case,
4143
emissions_profile_best_case = emissions_profile_best_case

R/map_region_risk.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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.
11-
#' @param mode The mode to plot. It can be one of "equal_weight", "worst_case"
12-
#' or "best_case". If nothing is chosen, "equal_weight" is the default mode.
1311
#' @param scenario A character vector: `r toString(scenarios())`.
1412
#' @param year A character vector: `r toString(years())`.
1513
#' @param risk_category A character vector.
@@ -23,18 +21,15 @@
2321
#' map_region_risk(without_financial, country_code = "DE", benchmark = "unit")
2422
#' })
2523
map_region_risk <- function(data,
26-
# TODO: plot for other countries
27-
country_code = c("DE"),
24+
country_code = c("DE", "AT", "FR", "NL", "ES"),
2825
grouping_emission = grouping_emission(),
29-
mode = modes(),
3026
scenario = scenarios(),
3127
year = years(),
3228
risk_category = risk_category()) {
3329
prepared_data <- prepare_geo_data(
3430
data,
3531
country_code,
3632
grouping_emission,
37-
mode,
3833
scenario,
3934
year,
4035
risk_category
@@ -49,3 +44,4 @@ map_region_risk <- function(data,
4944
coord_sf() +
5045
theme_tiltplot()
5146
}
47+

R/plot_sankey.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#' Create a sankey plot with financial data
22
#'
33
#' @inheritParams map_region_risk
4+
#' @param mode The mode to plot. It can be one of "equal_weight", "worst_case"
5+
#' or "best_case". If nothing is chosen, "equal_weight" is the default mode.
46
#' @param with_company Logical. If TRUE, will plot a node with the company name.
57
#' If FALSE, will plot without the company name node.
68
#'

R/prepare_geo_data.R

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
#' @noRd
99
#'
1010
prepare_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

R/sysdata.rda

68.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)