Skip to content

Commit

Permalink
Merge pull request #100 from robinhasse/oneRegion
Browse files Browse the repository at this point in the history
Allow one-region plots with showAreaAndBarPlots
  • Loading branch information
robinhasse authored May 7, 2024
2 parents 4c1daf6 + d6d911d commit 3b9295b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '293549221'
ValidationKey: '294057900'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exclude: '^tests/testthat/_snaps/.*$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.0
rev: 7910e0323d7213f34275a7a562b9ef0fde8ce1b9 # frozen: v0.4.2
hooks:
- id: parsable-R
- id: deps-in-desc
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mip: Comparison of multi-model runs'
version: 0.148.13
date-released: '2024-04-04'
version: 0.148.14
date-released: '2024-05-07'
abstract: Package contains generic functions to produce comparison plots of multi-model
runs.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mip
Title: Comparison of multi-model runs
Version: 0.148.13
Date: 2024-04-04
Version: 0.148.14
Date: 2024-05-07
Authors@R: c(
person("David", "Klein", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
7 changes: 5 additions & 2 deletions R/mipBarYearData.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#'
#' @importFrom magclass is.magpie
#' @importFrom ggplot2 ggplot aes_ guides guide_legend scale_x_continuous
#' ggtitle geom_col scale_shape_manual
#' ggtitle geom_col scale_shape_manual theme_minimal theme
#' @importFrom dplyr %>% mutate filter inner_join group_by summarise select n sym arrange
#' @importFrom tidyr crossing unite
#' @importFrom quitte order.levels
Expand Down Expand Up @@ -110,6 +110,7 @@ mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = N
guide = guide_legend(reverse = TRUE)) +
facet_wrap(~region, scales = "free_y") +
labs(x = xlab, y = ylab, title = title) +
theme_minimal() +
theme(legend.position = "bottom")

# add markers
Expand All @@ -122,7 +123,9 @@ mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = N
shape = !!sym("identifier")),
size = 1.5) +
scale_shape_manual(values = scenarioMarkers, name = NULL) +
theme(legend.box = "vertical")
theme(legend.box = "vertical",
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
} else {
p <- p +
scale_x_continuous(breaks = xpos$xpos,
Expand Down
32 changes: 21 additions & 11 deletions R/showAreaAndBarPlots.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#' }
#' @export
#' @importFrom rlang .data .env
#' @importFrom dplyr rename left_join summarize group_by arrange
#' @importFrom dplyr rename left_join summarize group_by arrange filter
showAreaAndBarPlots <- function(
data, vars, tot = NULL, fill = FALSE,
orderVars = c("mean", "user", "userRev"),
Expand Down Expand Up @@ -87,7 +87,7 @@ showAreaAndBarPlots <- function(
dnohist <- data %>%
filter(.data$variable %in% c(.env$vars, tot), .data$scenario != "historical") %>%
droplevels()
if (! "identifier" %in% names(dnohist)) dnohist$identifier <- identifierModelScen(dnohist)
if (!"identifier" %in% names(dnohist)) dnohist$identifier <- identifierModelScen(dnohist)
d <- dnohist %>%
filter(.data$variable %in% .env$vars, .data$scenario != "historical") %>%
droplevels()
Expand Down Expand Up @@ -138,21 +138,31 @@ showAreaAndBarPlots <- function(
filter(.data$region == .env$mainReg, .data$period %in% .env$yearsBarPlot) %>%
droplevels() %>%
mipBarYearData(ylab = lcp) +
ylab(NULL) +
{ if (length(unique(data$region)) > 1) theme(legend.position = "none") } # nolint
if (length(unique(data$region)) > 1) {
p3 <- d %>%
filter(.data$region != .env$mainReg, .data$period %in% .env$yearsBarPlot) %>%
ylab(NULL)

d3 <- d %>%
filter(.data$region != .env$mainReg,
.data$period %in% .env$yearsBarPlot)
d4 <- d %>%
filter(.data$region != .env$mainReg)
showNonMainRegs <- nrow(d3) > 0 && nrow(d4) > 0

if (showNonMainRegs) {
p2 <- p2 +
theme(legend.position = "none")
p3 <- d3 %>%
droplevels() %>%
mipBarYearData(ylab = lcp) +
ylab(NULL) +
guides(fill = guide_legend(reverse = TRUE, ncol = 3))
p4 <- d %>%
filter(.data$region != .env$mainReg) %>%
p4 <- d4 %>%
droplevels() %>%
mipArea(scales = scales, total = is.null(tot), ylab = lcp,
stack_priority = if (is.null(tot)) c("variable", "region") else "variable") +
guides(fill = guide_legend(reverse = TRUE))
} else {
p2 <- p2 +
guides(fill = guide_legend(reverse = TRUE, ncol = 3))
}

# Add black lines in area plots from variable tot if provided.
Expand All @@ -168,7 +178,7 @@ showAreaAndBarPlots <- function(
mapping = aes(.data$period, .data$value),
size = 1.3
)
if (length(unique(data$region)) > 1) {
if (showNonMainRegs) {
dRegiTot <- dnohist %>%
filter(
.data$region != .env$mainReg,
Expand All @@ -185,7 +195,7 @@ showAreaAndBarPlots <- function(
}

# Show plots.
if (length(unique(data$region)) > 1) {
if (showNonMainRegs) {
grid.arrange(p1, p2, p3, layout_matrix = rbind(c(1, 3), c(2, 3)), left = label)
cat("\n\n")
print(p4)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Comparison of multi-model runs

R package **mip**, version **0.148.13**
R package **mip**, version **0.148.14**

[![CRAN status](https://www.r-pkg.org/badges/version/mip)](https://cran.r-project.org/package=mip) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158586.svg)](https://doi.org/10.5281/zenodo.1158586) [![R build status](https://github.com/pik-piam/mip/workflows/check/badge.svg)](https://github.com/pik-piam/mip/actions) [![codecov](https://codecov.io/gh/pik-piam/mip/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mip) [![r-universe](https://pik-piam.r-universe.dev/badges/mip)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -47,7 +47,7 @@ In case of questions / problems please contact David Klein <[email protected]

To cite package **mip** in publications use:

Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 <https://doi.org/10.5281/zenodo.1158586>, R package version 0.148.13, <https://github.com/pik-piam/mip>.
Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 <https://doi.org/10.5281/zenodo.1158586>, R package version 0.148.14, <https://github.com/pik-piam/mip>.

A BibTeX entry for LaTeX users is

Expand All @@ -56,8 +56,8 @@ A BibTeX entry for LaTeX users is
title = {mip: Comparison of multi-model runs},
author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters and Tonn Rüter},
year = {2024},
note = {R package version 0.148.13},
doi = {10.5281/zenodo.1158586},
note = {R package version 0.148.14},
url = {https://github.com/pik-piam/mip},
doi = {10.5281/zenodo.1158586},
}
```
33 changes: 33 additions & 0 deletions inst/extdata/plotstyle.csv
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,36 @@ DRI-NG-EAF; DRI-NG-EAF;#999959;;
DRI-NG-EAF-CCS; DRI-NG-EAF-CCS;#e5e5b2;;
DRI-H2-EAF; DRI-H2-EAF;#66cccc;;
SCRAP-EAF; SCRAP-EAF;#ffb200;;
Before 1945;;#D53E4F;;
1945 - 1969;;#E65849;;
1970 - 1979;;#F57245;;
1980 - 1989;;#FA9555;;
1990 - 1999;;#FEB567;;
2000 - 2010;;#FECF7D;;
2011 - 2020;;#FFE695;;
2021 - 2030;;#FFF7B1;;
2031 - 2040;;#F8FCB5;;
2041 - 2050;;#EBF7A0;;
2051 - 2060;;#D3ED9C;;
2061 - 2070;;#B3E0A3;;
2071 - 2080;;#91D2A5;;
2081 - 2090;;#6BC4A5;;
2091 - 2100;;#55A6B1;;
After 2100;;#3288BD;;
Urban;;"#e6194B";;
Rural;;#3cb44b;;
SFH;;#4363d8;;
MFH;;#f58231;;
Residential;;#911eb4;;
Stock|Residential;Residential;#911eb4;;
Construction|Residential;Residential;#911eb4;;
Renovation|Residential;Residential;#911eb4;;
Demolition|Residential;Residential;#911eb4;;
Commercial;;#469990;;
Stock|Commercial;Commercial;#469990;;
Construction|Commercial;Commercial;#469990;;
Renovation|Commercial;Commercial;#469990;;
Demolition|Commercial;Commercial;#469990;;
Heat pump;;#3cb44b;;
Direct electric;;#f58231;;
Disctrict heat;;#cc0000;;

0 comments on commit 3b9295b

Please sign in to comment.