From 8f24e51dda85237349e60d6273240b186579216b Mon Sep 17 00:00:00 2001 From: emolinab Date: Mon, 5 Feb 2024 16:33:28 +0100 Subject: [PATCH] tag option added to plotCorr2d funtion --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/plotCorrHist2D.R | 28 +++++++++++++++------------- README.md | 6 +++--- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 45b7537..2d11a05 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '70931220' +ValidationKey: '71128800' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index db351eb..69d5d8d 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,7 +2,7 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'luplot: Landuse Plot Library' -version: 3.59.0 +version: 3.60.0 date-released: '2024-02-05' abstract: Some useful functions to plot data such as a map plot function for MAgPIE objects. diff --git a/DESCRIPTION b/DESCRIPTION index cff9d67..8cfdcee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: luplot Title: Landuse Plot Library -Version: 3.59.0 +Version: 3.60.0 Date: 2024-02-05 Authors@R: c( person("Benjamin Leon", "Bodirsky", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/plotCorrHist2D.R b/R/plotCorrHist2D.R index 50d60d5..4488821 100644 --- a/R/plotCorrHist2D.R +++ b/R/plotCorrHist2D.R @@ -27,6 +27,7 @@ #' @param table Conditional to include table with statistics in the output. TRUE (includes it), FALSE (it doesn't) #' @param stat Conditional to include R2 and MAE on the fiure. TRUE (includes it), FALSE (it doesn't) #' @param palette palette selection for heatd maps based on the RColorBrewer library +#' @param tag for multiple items in the second and third dimensions of the magpie object, should the title include "year", "item", "year-item". #' @author Edna Molina Bacca #' @importFrom grDevices colorRampPalette #' @importFrom ggplot2 ggplot aes_ xlim ylim scale_fill_gradientn coord_fixed geom_abline geom_vline geom_hline @@ -44,7 +45,7 @@ plotCorrHist2D <- function(x, y, title = NULL, xlab = "x", ylab = "y", bins = 40, limx=NULL, limy=NULL, folder = NULL, file = "", breaks = waiver(),nrows=2, ncols=2, axisFont=13, axisTitleFont=13,TitleFontSize=15,legendTitleFont=12,legendTextFont=10, - statFont=4, table=FALSE, stat=TRUE,palette="RdYlBu") { + statFont=4, table=FALSE, stat=TRUE,palette="RdYlBu",tag=NULL) { getNames(x) <- gsub(x = getNames(x), pattern = "\\.", replacement = "_") @@ -65,7 +66,7 @@ plotCorrHist2D <- function(x, y, title = NULL, xlab = "x", ylab = "y", bins = 40 plotMagpie <- reshape(plotMagpie, direction = "wide", idvar = c("Cell", "Region", "Year"), timevar = "Data1") plots <- list() - corr <- c("Year","Variable", "r2", "MAE", "Bias", "error per unit") + corr <- c("Year","Variable", "r2", "MAE", "Bias", "MAEU") rf <- colorRampPalette(rev(brewer.pal(11, palette))) r <- rf(32) # color palette @@ -105,14 +106,15 @@ plotCorrHist2D <- function(x, y, title = NULL, xlab = "x", ylab = "y", bins = 40 corr <- rbind(corr, all) - tag <- paste0(na, "-", year) + tag1 <- paste0(na, "-", year) + tagTitle <- if(tag=="year-item") tag1 else if(tag=="year") year else if(tag=="item") na - plots[[tag]] <- ggplot(data, aes_string(x = valueX, y = valueY)) + theme_bw() - plots[[tag]] <- plots[[tag]] + geom_bin2d(bins = bins) + coord_fixed(ratio = 1) + + plots[[tag1]] <- ggplot(data, aes_string(x = valueX, y = valueY)) + theme_bw() + plots[[tag1]] <- plots[[tag1]] + geom_bin2d(bins = bins) + coord_fixed(ratio = 1) + geom_abline(intercept = 0, slope = 1) + geom_vline(xintercept = 0) + geom_hline(yintercept = 0) - plots[[tag]] <- plots[[tag]] + labs(x = xlab, y = ylab, title = paste0(title, " ", "(", na, "-", year, ")")) + + plots[[tag1]] <- plots[[tag1]] + labs(x = xlab, y = ylab, title = paste0(title, " ", "(", tagTitle, ")")) + scale_fill_gradientn(colours = r, trans = "log", breaks = breaks) - plots[[tag]] <- plots[[tag]] + theme(axis.text.x = element_text(color = "grey20", size = axisFont), + plots[[tag1]] <- plots[[tag1]] + theme(axis.text.x = element_text(color = "grey20", size = axisFont), axis.title.x = element_text(color = "grey20", size = axisTitleFont), axis.text.y = element_text(color = "grey20", size = axisFont), axis.title.y = element_text(color = "grey20", size = axisTitleFont), @@ -121,12 +123,12 @@ plotCorrHist2D <- function(x, y, title = NULL, xlab = "x", ylab = "y", bins = 40 legend.title = element_text(size = legendTitleFont), legend.background = element_blank()) - plots[[tag]] <- if(stat) plots[[tag]] + annotate("label", size = statFont, x = labelX, y = labelY, - label = paste0("R2 = ", r2), hjust = 1) else plots[[tag]] - plots[[tag]] <- if(stat) plots[[tag]] + annotate("label", size = statFont, x = labelX, y = labelY2, - label = paste0("MAE = ", mae), hjust = 1) else plots[[tag]] - plots[[tag]] <- if (!is.null(limx)) plots[[tag]] + scale_x_continuous(limits = limx) else plots[[tag]] + scale_x_continuous(limits = limx1) - plots[[tag]] <- if (!is.null(limy)) plots[[tag]] + scale_y_continuous(limits = limy) else plots[[tag]] + scale_y_continuous(limits = limy1) + plots[[tag1]] <- if(stat) plots[[tag1]] + annotate("label", size = statFont, x = labelX, y = labelY, + label = paste0("R2 = ", r2), hjust = 1) else plots[[tag1]] + plots[[tag1]] <- if(stat) plots[[tag1]] + annotate("label", size = statFont, x = labelX, y = labelY2, + label = paste0("MAE = ", mae), hjust = 1) else plots[[tag1]] + plots[[tag1]] <- if (!is.null(limx)) plots[[tag1]] + scale_x_continuous(limits = limx) else plots[[tag1]] + scale_x_continuous(limits = limx1) + plots[[tag1]] <- if (!is.null(limy)) plots[[tag1]] + scale_y_continuous(limits = limy) else plots[[tag1]] + scale_y_continuous(limits = limy1) } } diff --git a/README.md b/README.md index c20d40b..7e46362 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Landuse Plot Library -R package **luplot**, version **3.59.0** +R package **luplot**, version **3.60.0** [![CRAN status](https://www.r-pkg.org/badges/version/luplot)](https://cran.r-project.org/package=luplot) [![R build status](https://github.com/pik-piam/luplot/workflows/check/badge.svg)](https://github.com/pik-piam/luplot/actions) [![codecov](https://codecov.io/gh/pik-piam/luplot/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/luplot) [![r-universe](https://pik-piam.r-universe.dev/badges/luplot)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Bodirsky . +Bodirsky B, Dietrich J, Krause M, Stevanovic M, Humpenoeder F, Weindl I, Baumstark L, Klein D, Rolinski S, Wang X (2024). _luplot: Landuse Plot Library_. R package version 3.60.0, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {luplot: Landuse Plot Library}, author = {Benjamin Leon Bodirsky and Jan Philipp Dietrich and Michael Krause and Miodrag Stevanovic and Florian Humpenoeder and Isabelle Weindl and Lavinia Baumstark and David Klein and Susanne Rolinski and Xiaoxi Wang}, year = {2024}, - note = {R package version 3.59.0}, + note = {R package version 3.60.0}, url = {https://github.com/pik-piam/luplot}, } ```