-
Notifications
You must be signed in to change notification settings - Fork 0
Add maps for all countries #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@kalashsinghal looks good, thanks! In addition to @AnneSchoenauer 's request on slack, I wonder if it's possible to show France without the islands in the Caribbean? Otherwise, the mainland part of France is so small... |
|
Hi @AnneSchoenauer @Tilmon , It took me a while to understand Linda's code completely on how the colouring works. Let's break down the whole procedure step by step: 1. Selection of sample data library(readr)
library(dplyr)
library(tibble)
library(eurostat)
library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.9.2, PROJ 9.5.0; sf_use_s2() is TRUE
library(ggplot2)
devtools::load_all(".")
#> ℹ Loading tiltPlot
options(width = 500)
example_best_case_worst_case_sample <- example_data_factory(
# styler: off
tribble(
~companies_id, ~country, ~postcode, ~grouping_emission, ~scenario, ~year, ~transition_risk_category, ~transition_risk_profile_best_case,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "low", 1.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "low", 2.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "medium", 3.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "medium", 4.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "high", 5.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "high", 6.0
)
# styler: on
)
example <- example_best_case_worst_case_sample()
example
#> # A tibble: 6 × 8
#> companies_id country postcode grouping_emission scenario year transition_risk_category transition_risk_profile_best_case
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 comp_1 austria 1020 all 1.5C RPS 2050 low 1
#> 2 comp_1 austria 1020 all 1.5C RPS 2050 low 2
#> 3 comp_1 austria 1020 all 1.5C RPS 2050 medium 3
#> 4 comp_1 austria 1020 all 1.5C RPS 2050 medium 4
#> 5 comp_1 austria 1020 all 1.5C RPS 2050 high 5
#> 6 comp_1 austria 1020 all 1.5C RPS 2050 high 62. Create aggregated data from sample data shp_0 <- get_eurostat_geospatial(
resolution = 10,
nuts_level = 3,
year = 2016,
crs = 3035
)
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues
# filter for the specified country
shp_1 <- shp_0 |>
filter(.data$CNTR_CODE == "AT") |>
select(geo = "NUTS_ID", "geometry") |>
arrange(.data$geo) |>
st_as_sf() |>
inner_join(nuts_all, by = "geo")
agg <- example |>
left_join(shp_1, by = "postcode") |>
st_as_sf() |>
group_by(.data$postcode, .data$transition_risk_category) |>
summarise(total_mode = sum(.data$transition_risk_profile_best_case)) |>
group_by(.data$postcode) |>
mutate(proportion = .data$total_mode / sum(.data$total_mode)) |>
ungroup()
#> `summarise()` has grouped output by 'postcode'. You can override using the `.groups` argument.
agg
#> # A tibble: 3 × 5
#> postcode transition_risk_category total_mode geometry proportion
#> <chr> <chr> <dbl> <POLYGON [m]> <dbl>
#> 1 1020 high 11 ((4810026 2802881, 4803838 2803770, 4797684 2799650, 4788742 2798937, 4780682 2803584, 47... 0.524
#> 2 1020 low 3 ((4810026 2802881, 4803838 2803770, 4797684 2799650, 4788742 2798937, 4780682 2803584, 47... 0.143
#> 3 1020 medium 7 ((4810026 2802881, 4803838 2803770, 4797684 2799650, 4788742 2798937, 4780682 2803584, 47... 0.333NOTE: The 3. Pivot aggregated data aggregated_data <- agg |>
pivot_wider(names_from = transition_risk_category , values_from = "proportion", values_fill = 0) |>
filter(.data$total_mode != 0) |>
group_by(.data$postcode) |>
summarise(
total_mode = add(.data$total_mode),
geometry = first(.data$geometry),
low = add(.data$low),
medium = add(.data$medium),
high = add(.data$high)
) |>
mutate(color = pmap(list(.data$high, .data$medium, .data$low), custom_gradient_color))
aggregated_data
#> # A tibble: 1 × 7
#> postcode total_mode geometry low medium high color
#> * <chr> <dbl> <POLYGON [m]> <dbl> <dbl> <dbl> <list>
#> 1 1020 21 ((4810026 2802881, 4803838 2803770, 4797684 2799650, 4788742 2798937, 4780682 2803584, 47... 0.143 0.333 0.524 <chr [1]>NOTE: The final colour for the postcode is derived using this formula: 4. Plot aggregated data ggplot() +
geom_sf(data = aggregated_data, mapping = aes(fill = .data$color)) +
geom_sf(data = shp_1, fill = NA) +
coord_sf() +
theme_tiltplot()Created on 2024-10-14 with reprex v2.0.2 |
|
Here is another example which only has values for library(readr)
library(dplyr)
devtools::load_all(".")
#> ℹ Loading tiltPlot
options(width = 500)
example_best_case_worst_case_sample <- example_data_factory(
# styler: off
tribble(
~companies_id, ~country, ~postcode, ~grouping_emission, ~scenario, ~year, ~transition_risk_category, ~transition_risk_profile_best_case,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "low", 1.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "low", 2.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "medium", 0.0,
"comp_1", "austria", "1020", "all", "1.5C RPS", "2050", "high", 0.0
)
# styler: on
)
example <- example_best_case_worst_case_sample()
example
#> # A tibble: 4 × 8
#> companies_id country postcode grouping_emission scenario year transition_risk_category transition_risk_profile_best_case
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 comp_1 austria 1020 all 1.5C RPS 2050 low 1
#> 2 comp_1 austria 1020 all 1.5C RPS 2050 low 2
#> 3 comp_1 austria 1020 all 1.5C RPS 2050 medium 0
#> 4 comp_1 austria 1020 all 1.5C RPS 2050 high 0
tiltPlot:::map_region_risk(example,
country_code = c("AT"),
grouping_emission = "all",
mode = "transition_risk_profile_best_case",
scenario = "1.5C RPS",
year = 2050,
risk_category = "transition_risk_category")
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issuesCreated on 2024-10-14 with reprex v2.0.2 |
|
Discussion from Sprint @kalashsinghal For a first version, please create maps with the following methodological choices, for emission and transition risk indicator:
|
|
@kalashsinghal regarding the France map: Please try for another two hours to remove the FR islands. But if it doesn't work, just skip it for now. |
|
@kalashsinghal please make sure empty postcodes are still shown as grey (or show them in any other way). |
|
@kalashsinghal this looks good! Thanks for the explanation. |
|
I identified that that the colouring in maps is wrong, because only a single postcode is used to colour the whole NUTS region. This is happening because we have one-to-many mapping between NUTS-postcode mapper. If we choose to colour the maps based on NUTS code instead of postcode then most of the maps made from the real data will have orangish colour. To better understand the case for a single NUTS region, I have taken the NUTS code transition_risk_best_case_NUTS_NL325_netherlands_map.csv This file is the product level data of transition risk indicator for a single NUTS code. Now, I will walk you through the whole process of map creation from the 8 unique postcodes of And then to get colour for each postcode, we get the second colour table like this: The colour Please use this reprex to understand the logic behind the code processing. If you have questions then, please let me know! |
|
Hi @kalashsinghal thanks for the info:
cc' @AnneSchoenauer |
@Tilmon This is the graph of transition risk equal weight values for Germany (for group:
@Tilmon Please have a look at the csv file with NUTS codes, transition risk best case, worst case and equal weight values for the German map! (for group: transition_risk_NUTS_germany_map.csv
@Tilmon Right! This column is not used for the graph. cc' @AnneSchoenauer |
|
NUTS Dataset
Hi @kalashsinghal , this table has multiple rows per NUTS code - is it still for each company? I would need a table that shows one value per NUTS code, i.e. the value that you use to create the colors in the map. I want to see that to be able to understand the distribution of the values - I would expect that values are almost the same across NUTS codes given that the colors are all orange. But maybe that's wrong and values actually vary greatly but only the color codes are all the same. So please share a file with results per NUTS code - thanks! Total mode And @kalashsinghal can you explain again why actually we use as basis for coloring the (total mode)/(sum of total mode)? E.g., why don't we simply use the average transition risk score per company? That's something that came to my mind after our discussion on Monday with Anne about the bar charts... I mean the map is supposed to show the distribution of the transition risk. Then wouldn't it be more useful to use the actual transition risk indicator on company-level? Just thinking out loud... |
Hi @Tilmon, Here is the file you need: These NUTS code are only for country Germany and for group:
@Tilmon I am not aware of the reason because Linda worked on the map code. I guess the methodology is yet to approve from @AnneSchoenauer. @AnneSchoenauer Did you reviewed the methodology of German map created by Linda? |
|
UPDATE: To remove the issue of orangish colour on each NUTS code of the map, I have chosen the following colours for the risk categories: Please have a look at the emission_profile_best_case_AT_all.pdf If the results look satisfactory to you, then I am happy to create maps for Please let me know what you think about the first version! |
|
Hi @kalashsinghal , thanks! The TR Indicator maps look OK I think, but I find it counter intuitive that all the emission indicator maps are mostly red, even though it's best case... Do you have an explanation for that? |
We use the sum of best case values to give weights to the colours. And, the The above image shows that the Does it makes sense? FYI: As you guyz already know that profile ranking of high risk categories will be higher than the medium and low risk categories, it is very obvious that the best case values will be higher too for |
|
@kalashsinghal can you explain where the screenshot comes from? Which results does it show? Also, just to confirm: My understanding is that for the country map, we group by NUTS code and the sum up the results per risk category and the divide the sums per risk category by the total sum across all risk categories. So in the end, we have per NUTS code one value for low, medium and high. Also can you explain this better? Not sure I know what you mean... cc' @AnneSchoenauer |
Hi @Tilmon, After our discussion on the call, we decided to check the emission profile map on company-level data. Do your remember? I replaced the column |
|
Hi @kalashsinghal , please change the country mapping as discussed so that it calculates the colors by counting the products per category and then calculating the proportion as share of products in each category. That way, we avoid the bias of the ranking amounts. |
After changing the calculation by counting the products per category, I created the maps for emission profile and transition risk profile below. If these maps look ok to you then we can consider this change as final. What do you think? Emission profile: library(readr)
library(dplyr)
devtools::load_all(".")
#> ℹ Loading tiltPlot
options(width = 500)
transition_risk_product <- read_csv("transition_risk_profile_at_product_level_23_10_24.csv")
tiltPlot:::map_region_risk(transition_risk_product,
country_code = c("DE"),
grouping_emission = "all",
scenario = NULL,
year = NULL,
risk_category = "emission_category")
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issuesCreated on 2024-12-20 with reprex v2.1.1 Transition risk profile: library(readr)
library(dplyr)
devtools::load_all(".")
#> ℹ Loading tiltPlot
options(width = 500)
transition_risk_product <- read_csv("transition_risk_profile_at_product_level_23_10_24.csv")
tiltPlot:::map_region_risk(transition_risk_product,
country_code = c("DE"),
grouping_emission = "all",
scenario = "1.5C RPS",
year = 2050,
risk_category = "transition_risk_category")
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issuesCreated on 2024-12-20 with reprex v2.1.1 |
|
Hi @kalashsinghal thanks a lot! Could you please create a litte reprex with which we can see the calculations behind the plot? Thanks :) |
|
Here is the reprex which will show the calculations of new maps step by step: 1. Selection of sample data library(readr)
library(dplyr)
library(tibble)
library(eurostat)
library(ggplot2)
devtools::load_all(".")
#> ℹ Loading tiltPlot
options(width = 500)
example_best_case_worst_case_sample <- example_data_factory(
# styler: off
tribble(
~companies_id, ~country, ~postcode, ~grouping_emission, ~scenario, ~year, ~transition_risk_category, ~product,
"comp_1", "austria", "8332", "all", "1.5C RPS", "2050", "low", "a",
"comp_1", "austria", "8332", "all", "1.5C RPS", "2050", "low", "b",
"comp_1", "austria", "8332", "all", "1.5C RPS", "2050", "medium", "c",
"comp_1", "austria", "8332", "all", "1.5C RPS", "2050", "high", "d",
"comp_2", "austria", "7304", "all", "1.5C RPS", "2050", "low", "e",
"comp_2", "austria", "7304", "all", "1.5C RPS", "2050", "medium", "f",
"comp_2", "austria", "7304", "all", "1.5C RPS", "2050", "high", "g",
"comp_2", "austria", "7304", "all", "1.5C RPS", "2050", "high", "h",
"comp_2", "austria", "7304", "all", "1.5C RPS", "2050", "high", "i"
)
# styler: on
)
example <- example_best_case_worst_case_sample()
example
#> # A tibble: 9 × 8
#> companies_id country postcode grouping_emission scenario year transition_risk_category product
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 comp_1 austria 8332 all 1.5C RPS 2050 low a
#> 2 comp_1 austria 8332 all 1.5C RPS 2050 low b
#> 3 comp_1 austria 8332 all 1.5C RPS 2050 medium c
#> 4 comp_1 austria 8332 all 1.5C RPS 2050 high d
#> 5 comp_2 austria 7304 all 1.5C RPS 2050 low e
#> 6 comp_2 austria 7304 all 1.5C RPS 2050 medium f
#> 7 comp_2 austria 7304 all 1.5C RPS 2050 high g
#> 8 comp_2 austria 7304 all 1.5C RPS 2050 high h
#> 9 comp_2 austria 7304 all 1.5C RPS 2050 high i2. Create aggregated data from sample data shp_0 <- eurostat::get_eurostat_geospatial(
resolution = 10,
nuts_level = "all",
year = 2021,
crs = 3035
) |>
filter(!(geo %in% c("FRY10", "FRY20", "FRY30", "FRY40", "FRY50")))
#> Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues
# filter for the specified country
shp_1 <- shp_0 |>
filter(.data$CNTR_CODE == "AT") |>
select(geo = "NUTS_ID", "geometry") |>
arrange(.data$geo) |>
st_as_sf() |>
inner_join(nuts_all, by = "geo")
agg <- example |>
left_join(shp_1, by = "postcode") |>
st_as_sf() |>
filter(country == "austria") |>
group_by(.data$geo, .data$transition_risk_category) |>
summarise(total_mode = n_distinct(.data$product, na.rm = TRUE)) |>
group_by(.data$geo) |>
mutate(proportion = .data$total_mode / sum(.data$total_mode, na.rm = TRUE)) |>
ungroup()
#> `summarise()` has grouped output by 'geo'. You can override using the `.groups` argument.
agg
#> # A tibble: 6 × 5
#> geo transition_risk_category total_mode geometry proportion
#> <chr> <chr> <int> <POLYGON [m]> <dbl>
#> 1 AT111 high 3 ((4821839 2726166, 4809143 2719917, 4806751 2714375, 4803462 2713240, 4802031 2714153, 47... 0.6
#> 2 AT111 low 1 ((4821839 2726166, 4809143 2719917, 4806751 2714375, 4803462 2713240, 4802031 2714153, 47... 0.2
#> 3 AT111 medium 1 ((4821839 2726166, 4809143 2719917, 4806751 2714375, 4803462 2713240, 4802031 2714153, 47... 0.2
#> 4 AT224 high 1 ((4786359 2720266, 4779599 2717444, 4776596 2712017, 4783789 2691337, 4783747 2681535, 47... 0.25
#> 5 AT224 low 2 ((4786359 2720266, 4779599 2717444, 4776596 2712017, 4783789 2691337, 4783747 2681535, 47... 0.5
#> 6 AT224 medium 1 ((4786359 2720266, 4779599 2717444, 4776596 2712017, 4783789 2691337, 4783747 2681535, 47... 0.25NOTE: The 3. Pivot aggregated data aggregated_data <- agg |>
pivot_wider(names_from = transition_risk_category, values_from = "proportion", values_fill = 0) |>
filter(.data$total_mode != 0) |>
group_by(.data$geo) |>
summarise(
total_mode = add(.data$total_mode),
geometry = first(.data$geometry),
low = if ("low" %in% names(pick(everything()))) add(.data$low) else 0.0,
medium = if ("medium" %in% names(pick(everything()))) add(.data$medium) else 0.0,
high = if ("high" %in% names(pick(everything()))) add(.data$high) else 0.0
) |>
mutate(color = pmap(list(.data$high, .data$medium, .data$low), custom_gradient_color))
aggregated_data
#> # A tibble: 2 × 7
#> geo total_mode geometry low medium high color
#> * <chr> <int> <POLYGON [m]> <dbl> <dbl> <dbl> <list>
#> 1 AT111 4 ((4821839 2726166, 4809143 2719917, 4806751 2714375, 4803462 2713240, 4802031 2714153, 47... 0.2 0.2 0.6 <chr [1]>
#> 2 AT224 3 ((4786359 2720266, 4779599 2717444, 4776596 2712017, 4783789 2691337, 4783747 2681535, 47... 0.5 0.25 0.25 <chr [1]>NOTE: The final colour for the NUTS codes are derived using this formula: 4. Plot aggregated data ggplot() +
geom_sf(data = aggregated_data, mapping = aes(fill = .data$color)) +
geom_sf(data = shp_1, fill = NA) +
coord_sf() +
theme_tiltplot()Created on 2025-01-02 with reprex v2.1.1 |
|
Dear @kalashsinghal this sounds perfect°! Well done :) |














Dear @AnneSchoenauer @Tilmon,
This PR adds maps for all four countries (Netherlands, Austria, France, and Spain) other than Germany. Please have a look at the maps for all four countries on real data:
Created on 2024-10-08 with reprex v2.0.2
Created on 2024-10-08 with reprex v2.0.2
Created on 2024-10-08 with reprex v2.0.2
Created on 2024-10-08 with reprex v2.0.2
TODO