-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a German Map showing regional risks (#73)
* added postcodes into the data * first trial of creating simple german map without any polygons * tried europstat * NUTS2 Level germany * merged financial data with geometry of map file * succeded map but have to modify the financial data in order to see something * modified financial data to see all the risks categories * added data set and documented + added CC-BY-SA license * updated code with new financial data set * updated sankey plot with new financial data * use snake-case for shapefiles * modified license and imported package functions * created gradient color for each combination of low, medium and high risk categories * fixed function argument * documented functions * documented unknown functions * added import of the new function of the germap_map function * corrected nuts_de documentation and added german_map function doc * added a little test * added test color * passed all the checks * updated README file * added copyrighted to the map * styled * styled arguments * create custom gradient color separatly * added test to the test custom gradient func * styled plot sankey and xctr arguments * replaced germap_map by map_country * added prepare_geo_data.R function * added test to prepare geo data and rename map_country * removed csv and wrote tsv file for nuts_de * rescaled the labs of the title * modified test to add risks names to the color lists * used tidy verse style
- Loading branch information
1 parent
173964b
commit e92b65a
Showing
36 changed files
with
9,594 additions
and
516 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Custom Gradient Color | ||
#' | ||
#' This function generates a custom gradient color based on the input values for | ||
#' "high," "medium," and "low. | ||
#' | ||
#' @param high A numeric value representing the intensity of the "high" color | ||
#' (0 to 1). | ||
#' @param medium A numeric value representing the intensity of the "medium" | ||
#' color (0 to 1). | ||
#' @param low A numeric value representing the intensity of the "low" color | ||
#' (0 to 1). | ||
#' | ||
#' @return A character string representing the combined RGB color code. | ||
#' @noRd | ||
#' | ||
#' @examples | ||
#' custom_gradient_color(1, 0.5, 0.2) | ||
custom_gradient_color <- function(high, medium, low) { | ||
# define RGB values for "high," "medium," and "low" | ||
high_color <- red <- c(1, 0, 0) | ||
medium_color <- orange <- c(1, 0.5, 0) | ||
low_color <- green <- c(0, 1, 0) | ||
|
||
# interpolate the colors based on proportions : 1 is highest intensity | ||
final_color <- high_color * high + medium_color * medium + low_color * low | ||
|
||
final_color <- do.call(rgb, as.list(final_color)) | ||
|
||
return(final_color) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Create a map with the risk color of each region (NUTS3 granularity) | ||
#' | ||
#' @param data A data frame like [financial] | ||
#' @param country_code Country code (ISO 3166 alpha-2) for which the map will be | ||
#' plotted. | ||
#' @param benchmark The mode of benchmark to plot. | ||
#' It can be one of "all", "unit" or "tilt_sec", "unit_tilt_sec", "isic_sec" | ||
#' or "unit_isic_sec". If nothing is chosen, "all" is the default mode. | ||
#' | ||
#' @param finance_weight The mode of financial data to plot (#TODO : fix financial columns). | ||
#' It can be one of "equal_weight", "worst_case" or "best_case". If nothing is | ||
#' chosen, "equal_weight" is the default mode. | ||
#' | ||
#' @return A ggplot2 object representing the country data plot. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' # Plot a German with a "unit" benchmark and equal_weight finance | ||
#' map_region_risk(financial, country_code = "DE", benchmark = "unit") | ||
map_region_risk <- function(data, | ||
# TODO : Plot for other countries | ||
country_code = c("DE"), | ||
benchmark = c( | ||
"all", | ||
"unit", | ||
"tilt_sec", | ||
"unit_tilt_sec", | ||
"isic_sec", | ||
"unit_isic_sec" | ||
), | ||
finance_weight = c("equal_weight", "worst_case", "best_case")) { | ||
prepared_data <- prepare_geo_data( | ||
data, | ||
country_code, | ||
benchmark, | ||
finance_weight | ||
) | ||
shp_1 <- prepared_data[[1]] | ||
aggregated_data <- prepared_data[[2]] | ||
|
||
# create map based on financial geo with two layers; financial data and map | ||
ggplot() + | ||
geom_sf(data = aggregated_data, mapping = aes(fill = .data$color)) + | ||
geom_sf(data = shp_1, fill = NA) + | ||
coord_sf() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#' Correspondence table between German postcodes and NUTS level 3 codes | ||
#' | ||
#' @source "© European Union - GISCO, 2021, postal code point dataset, | ||
#' Licence CC-BY-SA 4.0 available at https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data." | ||
#' @examples | ||
#' nuts_de | ||
#' @keywords internal | ||
"nuts_de" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#' Prepare Geo Data for a Specific Country | ||
#' | ||
#' @inheritParams map_region_risk | ||
#' | ||
#' @return A list containing the following components: | ||
#' - \code{shp_1}: Spatial data for the specified country. | ||
#' - \code{aggregated_data}: Aggregated financial data. | ||
#' @noRd | ||
#' | ||
#' @examples | ||
#' prepare_geo_data(financial_data) | ||
prepare_geo_data <- function(data, | ||
country_code = c("DE"), | ||
benchmark = c( | ||
"all", | ||
"unit", | ||
"tilt_sec", | ||
"unit_tilt_sec", | ||
"isic_sec", | ||
"unit_isic_sec" | ||
), | ||
finance_weight = c("equal_weight", "worst_case", "best_case")) { | ||
benchmark_arg <- arg_match(benchmark) | ||
finance_weight <- arg_match(finance_weight) | ||
|
||
crucial <- c( | ||
"_risk_category", | ||
"equal_weight_finance", | ||
"worst_case_finance", | ||
"best_case_finance" | ||
) | ||
data |> check_crucial_names(names_matching(data, crucial)) | ||
risk_var <- names_matching(data, "_risk_category") | ||
|
||
# get shapefile of European countries | ||
shp_0 <- get_eurostat_geospatial( | ||
resolution = 10, | ||
nuts_level = 3, | ||
year = 2016, | ||
crs = 3035, | ||
make_valid = TRUE | ||
) | ||
|
||
# filter for the specified country | ||
shp_1 <- shp_0 |> | ||
filter(.data$CNTR_CODE == country_code) |> | ||
select(geo = "NUTS_ID", "geometry") |> | ||
arrange(.data$geo) |> | ||
st_as_sf() | ||
|
||
# merge to have zip codes with NUTS file | ||
shp_1 <- shp_1 |> | ||
inner_join(nuts_de, by = "geo") | ||
|
||
# merge shapefile with financial data | ||
financial_geo <- data |> | ||
filter(benchmark == benchmark_arg) |> | ||
left_join(shp_1, by = "postcode") |> | ||
st_as_sf() | ||
|
||
# Add the code for data aggregation and color mapping | ||
aggregated_data <- financial_geo |> | ||
group_by(.data$postcode, .data$xctr_risk_category) |> | ||
summarize(count = n()) |> | ||
group_by(.data$postcode) |> | ||
mutate(proportion = .data$count / sum(.data$count)) |> | ||
ungroup() | ||
|
||
# apply custom_gradient_color to each row | ||
aggregated_data <- aggregated_data |> | ||
pivot_wider(names_from = "xctr_risk_category", values_from = "proportion", values_fill = 0) |> | ||
mutate(color = pmap(list(.data$high, .data$medium, .data$low), custom_gradient_color)) | ||
|
||
return(list(shp_1, aggregated_data)) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.