Skip to content

Commit

Permalink
tag option added to plotCorr2d funtion
Browse files Browse the repository at this point in the history
  • Loading branch information
emolinab committed Feb 5, 2024
1 parent 820779d commit 8f24e51
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '70931220'
ValidationKey: '71128800'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
Expand Down
28 changes: 15 additions & 13 deletions R/plotCorrHist2D.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = "_")
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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)

}
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Bodirsky <bodirsky@pik-p

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

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.59.0, <URL: https://github.com/pik-piam/luplot>.
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, <URL: https://github.com/pik-piam/luplot>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
```

0 comments on commit 8f24e51

Please sign in to comment.