Skip to content

Commit

Permalink
Format tables for descriptive analysis of co2 footprint per benchmark (
Browse files Browse the repository at this point in the history
…#37)

* Format tables

* refactor

* refactor
  • Loading branch information
kalashsinghal authored Aug 21, 2024
1 parent 1bdc16f commit 8ab42f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 74 deletions.
18 changes: 8 additions & 10 deletions R/calculate_co2_descriptive_analysis_per_benchmark.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ calculate_co2_descriptive_analysis_per_benchmark <- function(emission_product, b
select_benchmarks <- c("unit", "isic_4digit", "benchmark", "co2_footprint", "ep_product")
group_benchmark <- c("unit", "isic_4digit")
} else if (benchmark == "unit_tilt_subsector") {
select_benchmarks <- c("unit", "tilt_subsector", "benchmark", "co2_footprint", "ep_product")
select_benchmarks <- c("tilt_sector", "unit", "tilt_subsector", "benchmark", "co2_footprint", "ep_product")
group_benchmark <- c("unit", "tilt_subsector")
} else {
select_benchmarks <- c(benchmark, "benchmark", "co2_footprint", "ep_product")
select_benchmarks <- c("tilt_sector", benchmark, "benchmark", "co2_footprint", "ep_product")
group_benchmark <- benchmark
}

Expand All @@ -39,15 +39,13 @@ calculate_co2_descriptive_analysis_per_benchmark <- function(emission_product, b
distinct() |>
filter(benchmark == benchmark) |>
select(-c("benchmark")) |>
mutate(co2_min = min(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate(co2_max = max(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate(amount_of_unique_products = n_distinct(.data$ep_product, na.rm = TRUE), .by = group_benchmark) |>
mutate(co2_median = median(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate(co2_average = mean(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate(co2_sd = sd(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate(co2_cov = ((.data$co2_sd / .data$co2_average) * 100)) |>
mutate("# of distinct products" = n_distinct(.data$ep_product, na.rm = TRUE), .by = group_benchmark) |>
mutate("min CO2e" = min(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate("max CO2e" = max(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate("mean CO2e" = mean(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
mutate("median CO2e" = median(.data$co2_footprint, na.rm = TRUE), .by = group_benchmark) |>
select(-all_of(c("ep_product", "co2_footprint"))) |>
distinct()

output[order(output[[1]]), ]
output[order(output[[1]], output[[2]]), ]
}
18 changes: 9 additions & 9 deletions R/example_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
example_emission_product_co2_des_analysis <- function() {
tribble(
# styler: off
~benchmark, ~tilt_subsector, ~unit, ~isic_4digit, ~co2_footprint, ~ep_product,
"tilt_subsector", "chemicals", "kg", "'5555'", 1, "water",
"tilt_subsector", "chemicals", "kg", "'5555'", 4, "television",
"tilt_subsector", "chemicals", "kg", "'5555'", 9, "tables",
"tilt_subsector", "chemicals", "kg", "'5555'", 8, "electricity",
"tilt_subsector", "chemicals", "kg", "'5555'", NA_real_, "meat",
"tilt_subsector", "iron & steel", "m3", "'6666'", 1, "tables",
"tilt_subsector", "iron & steel", "m3", "'6666'", 2, "water",
"tilt_subsector", "iron & steel", "m3", "'6666'", 4, "tent"
~benchmark, ~tilt_sector, ~tilt_subsector, ~unit, ~isic_4digit, ~co2_footprint, ~ep_product,
"tilt_subsector", "group", "chemicals", "kg", "'5555'", 1, "water",
"tilt_subsector", "group", "chemicals", "kg", "'5555'", 4, "television",
"tilt_subsector", "group", "chemicals", "kg", "'5555'", 9, "tables",
"tilt_subsector", "group", "chemicals", "kg", "'5555'", 8, "electricity",
"tilt_subsector", "group", "chemicals", "kg", "'5555'", NA_real_, "meat",
"tilt_subsector", "group", "iron & steel", "m3", "'6666'", 1, "tables",
"tilt_subsector", "group", "iron & steel", "m3", "'6666'", 2, "water",
"tilt_subsector", "group", "iron & steel", "m3", "'6666'", 4, "tent"
# styler: on
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ knitr::opts_chunk$set(
)
```

```{css, echo=FALSE}
@import url('https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@400;700&display=swap');
table {
font-family: 'Roboto Condensed', sans-serif;
}
```

This article shows how to calculate the descriptive analysis of co2_footprint per
benchmark using product-level output of Emissions profile indicator.

```{r setup, warning=FALSE, echo = FALSE, message=FALSE}
library(tiltDataAnalysis)
library(dplyr)
library(tibble)
library(kableExtra)
library(knitr)
library(stringr)
options(readr.show_col_types = FALSE)
```

Expand All @@ -34,35 +44,10 @@ co2_all <- calculate_co2_descriptive_analysis_per_benchmark(
emission_product_example,
"all"
)
kable(co2_all, align = "ccccccc", caption = "Descriptive analysis for benchmark `all`", escape = F) |>
kable_classic(full_width = F, html_font = "Cambria") |>
column_spec(1:7, width = "2.5cm")
```

### Descriptive analysis for benchmark "unit"

```{r, echo=FALSE}
co2_unit <- calculate_co2_descriptive_analysis_per_benchmark(
emission_product_example,
"unit"
)
kable(co2_unit, align = "lccccccc", caption = "Descriptive analysis for benchmark `unit`", escape = F) |>
kable_classic(full_width = F, html_font = "Cambria") |>
column_spec(1:8, width = "2.5cm")
```

### Descriptive analysis for benchmark "isic_4digit"

```{r, echo=FALSE}
co2_isic_4digit <- calculate_co2_descriptive_analysis_per_benchmark(
emission_product_example,
"isic_4digit"
)
kable(co2_isic_4digit, align = "lccccccc", caption = "Descriptive analysis for benchmark `isic_4digit`", escape = F) |>
kable_classic(full_width = F, html_font = "Cambria") |>
column_spec(1:8, width = "2.5cm")
kable(co2_all, align = "ccccc", caption = "Descriptive analysis for benchmark `all`", escape = F) |>
kable_classic(full_width = F, html_font = "Roboto Condensed") |>
column_spec(1, width = "4cm") |>
column_spec(2:5, width = "2.5cm")
```

### Descriptive analysis for benchmark "tilt_subsector"
Expand All @@ -71,35 +56,41 @@ kable(co2_isic_4digit, align = "lccccccc", caption = "Descriptive analysis for b
co2_tilt_subsector <- calculate_co2_descriptive_analysis_per_benchmark(
emission_product_example,
"tilt_subsector"
)
kable(co2_tilt_subsector, align = "lccccccc", caption = "Descriptive analysis for benchmark `tilt_subsector`", escape = F) |>
kable_classic(full_width = F, html_font = "Cambria") |>
column_spec(1:8, width = "2.5cm")
) |>
filter(!str_detect(tilt_subsector, "NA"))
kable(co2_tilt_subsector, align = "llccccc", caption = "Descriptive analysis for benchmark `tilt_subsector`", escape = F) |>
kable_classic(full_width = F, html_font = "Roboto Condensed") |>
column_spec(2, width = "3cm") |>
column_spec(3, width = "4cm") |>
column_spec(4:7, width = "2.5cm") |>
collapse_rows(columns = 1, valign = "top")
```

### Descriptive analysis for benchmark "unit_tilt_subsector"

```{r, echo=FALSE}
co2_unit_tilt_subsector <- calculate_co2_descriptive_analysis_per_benchmark(
emission_product_example,
"unit_tilt_subsector"
)
### Five products with lowest CO2e values

kable(co2_unit_tilt_subsector, align = "llccccccc", caption = "Descriptive analysis for benchmark `unit_tilt_subsector`", escape = F) |>
kable_classic(full_width = F, html_font = "Cambria") |>
column_spec(1:9, width = "2.5cm")
```{r, echo=FALSE}
lowest_co2 <- emission_product_example |>
select(all_of(c("ep_product", "co2_footprint"))) |>
distinct() |>
arrange(.data$co2_footprint) |>
head(5)
kable(lowest_co2, align = "lc", caption = "Five products with lowest CO2e values", escape = F) |>
kable_classic(full_width = F, html_font = "Roboto Condensed") |>
column_spec(2, width = "3cm")
```

### Descriptive analysis for benchmark "unit_isic_4digit"

```{r, echo=FALSE}
co2_unit_isic_4digit <- calculate_co2_descriptive_analysis_per_benchmark(
emission_product_example,
"unit_isic_4digit"
)
### Five products with highest CO2e values

kable(co2_unit_isic_4digit, align = "llccccccc", caption = "Descriptive analysis for benchmark `unit_isic_4digit`", escape = F) |>
kable_classic(full_width = F, html_font = "Cambria") |>
column_spec(1:9, width = "2.5cm")
```{r, echo=FALSE}
highest_co2 <- emission_product_example |>
select(all_of(c("ep_product", "co2_footprint"))) |>
distinct() |>
arrange(desc(.data$co2_footprint)) |>
head(5)
kable(highest_co2, align = "lc", caption = "Five products with highest CO2e values", escape = F) |>
kable_classic(full_width = F, html_font = "Roboto Condensed") |>
column_spec(2, width = "3cm")
```
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ result <- show_reduction_targets_for_tilt_subsectors_per_grouping_sector(
kable(result, align = "llcccc", caption = "Reduction targets for all `tilt_subsectors` per grouping_sector", escape = F) |>
kable_classic(full_width = F, html_font = "Roboto Condensed") |>
column_spec(2, width = "6cm") |>
column_spec(2, width = "4cm") |>
column_spec(3:6, width = "2.5cm") |>
collapse_rows(columns = 1, valign = "top")
```

0 comments on commit 8ab42f6

Please sign in to comment.