library(werptoolkitr)
library(ggplot2)
library(dplyr)
Heatmap
Overview
This notebook provides examples of creating heatmaps to plot outcomes as colors with two driver axes (e.g. temperature and rainfall changes). At present, we only have one axis of change in both the simple 4x demonstration and the flow scaling. Once we have two axes, these heatmaps should come together quickly, including the ability to overplot points and fit kernel densities.
For a 1-d version, with variation in a single driver axis and outcomes on the y (instead of color), see the bar plots and line plots
Demonstration setup
As usual, we need paths to the data.
<- file.path('more_scenarios')
project_dir = file.path(project_dir, 'hydrographs')
hydro_dir <- file.path(project_dir, 'aggregator_output') agg_dir
Scenario information
This should be auto-acquired from the dirs above. But while its format is still up in the air, I’m leaving it a bit more user-editable.
<- jsonlite::read_json(file.path(hydro_dir,
scenarios 'scenario_metadata.json')) |>
::as_tibble() |>
tibble::unnest(cols = everything()) tidyr
Subset for demo
We have a lot of hydrographs, so for this demonstration, we will often use a subset.
<- c('412002', '419001', '422028', '421001') gauges_to_plot
Standard scenario appearance
We want to have a consistent look for the scenarios across the project, with a logical ordering and standard colors. In future, this will potentially be able to be parsed from metadata, but at present we will define these properties manually. They are not included in the {werptoolkitr} package because they are project/analysis- specific.
<- forcats::fct_reorder(scenarios$scenario_name, scenarios$flow_multiplier)
sceneorder <- make_pal(unique(scenarios$scenario_name),
scene_pal palette = 'ggsci::nrc_npg',
refvals = 'base', refcols = 'black')
Plot heatmaps
Once we have data that needs heatmaps, we’d read in those aggregated outcome lists and make the demo plots here.
As an example
<- tidyr::expand_grid(Env_var1 = seq(-1, 4, 0.25), Env_var2 = seq(-30, 10, 2))
variables <- variables %>%
variables ::mutate(Metric = Env_var1^2 + Env_var2) dplyr
ggplot(variables) + geom_raster(aes(x = Env_var1, y = Env_var2, fill = Metric)) +
::scale_fill_viridis() viridis
<- tibble(Env_var1 = sample(seq(-1, 4, 0.25), 15), Env_var2 = sample(seq(-30, 10, 2), 15), scenario = as.character(1:15), isscenario = FALSE)
samples
<- tibble(Env_var1 = rnorm(26, 0.5, 0.1), Env_var2 = rnorm(26, 0, 1), scenario = letters, isscenario = TRUE)
scenariotib
<- bind_rows(samples, scenariotib) scenariosamples
<- ggplot() +
heatexample geom_raster(data = variables,
aes(x = Env_var1, y = Env_var2, fill = Metric)) +
::scale_fill_viridis() +
viridisgeom_point(data = scenariosamples,
aes(x = Env_var1, y = Env_var2, color = isscenario)) +
scale_color_manual(values = c('firebrick', 'magenta4')) +
guides(color = 'none') +
theme_werp_toolkit()
heatexample