From 2396c0a9d48338772ac1d19476c55d7b2b068424 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Tue, 23 Aug 2022 16:57:21 +0200 Subject: [PATCH 1/7] shared legend Added option to draw one shared legend for the bi-plots. --- R/pairsplot.R | 62 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/R/pairsplot.R b/R/pairsplot.R index 7877f01..4d0a6f5 100644 --- a/R/pairsplot.R +++ b/R/pairsplot.R @@ -33,6 +33,7 @@ #' @param shapekey Vector of name-value pairs relating to value passed to #' 'shape', e.g., c(A=10, B=21). #' @param pointSize Size of plotted points. +#' @param shared.legend Logical, indicating whether to draw a one shared legend for the bi-plots. #' @param legendPosition Position of legend ('top', 'bottom', 'left', 'right', #' 'none'). #' @param legendLabSize Size of plot legend text. @@ -139,6 +140,7 @@ pairsplot <- function( shape = NULL, shapekey = NULL, pointSize = 1.0, + shared.legend = FALSE, legendPosition = 'none', legendLabSize = 6, legendIconSize = 1.5, @@ -336,15 +338,29 @@ pairsplot <- function( # return plot? if (returnPlot) { - return(plot_grid(title, - do.call(plot_grid, c(biplots.final, ncol = ncol, nrow = nrow)), - ncol = 1, - rel_heights = c(0.1, 1.0))) + if(shared.legend) { + legend <- get_legend(biplots.final[[2]] + theme(legend.box.margin = margin(0, 0, 0, 12))) + bigrid <- plot_grid(title, + do.call(plot_grid, c(lapply(biplots.final, "+", theme(legend.position = "none")), ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1)) + return(plot_grid(legend, bigrid, rel_widths = c(0.5, ncol))) + } else { + return(plot_grid(title, + do.call(plot_grid, c(biplots.final, ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1))) + } } else if (!returnPlot) { - plot_grid(title, - do.call(plot_grid, c(biplots.final, ncol = ncol, nrow = nrow)), - ncol = 1, - rel_heights = c(0.1, 1.0)) + if(shared.legend) { + legend <- get_legend(biplots.final[[2]] + theme(legend.box.margin = margin(0, 0, 0, 12))) + bigrid <- plot_grid(title, + do.call(plot_grid, c(lapply(biplots.final, "+", theme(legend.position = "none")), ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1)) + plot_grid(legend, bigrid, rel_widths = c(0.5, ncol)) + } else { + plot_grid(title, + do.call(plot_grid, c(biplots.final, ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1)) + } } # triangular layout? @@ -378,15 +394,29 @@ pairsplot <- function( # return plot? if (returnPlot) { - return(plot_grid(title, - do.call(plot_grid, c(biplots, ncol = ncol, nrow = nrow)), - ncol = 1, - rel_heights = c(0.1, 1.0))) + if(shared.legend) { + legend <- get_legend(biplots[[2]] + theme(legend.box.margin = margin(0, 0, 0, 12))) + bigrid <- plot_grid(title, + do.call(plot_grid, c(lapply(biplots, "+", theme(legend.position = "none")), ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1)) + return(plot_grid(legend, bigrid, rel_widths = c(0.5, ncol))) + } else { + return(plot_grid(title, + do.call(plot_grid, c(biplots, ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1))) + } } else if (!returnPlot) { - plot_grid(title, - do.call(plot_grid, c(biplots, ncol = ncol, nrow = nrow)), - ncol = 1, - rel_heights = c(0.1, 1.0)) + if(shared.legend) { + legend <- get_legend(biplots[[2]] + theme(legend.box.margin = margin(0, 0, 0, 12))) + bigrid <- plot_grid(title, + do.call(plot_grid, c(lapply(biplots, "+", theme(legend.position = "none")), ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1)) + plot_grid(legend, bigrid, rel_widths = c(0.5, ncol)) + } else { + plot_grid(title, + do.call(plot_grid, c(biplots, ncol = ncol, nrow = nrow)), + ncol = 1, rel_heights = c(0.1, 1)) + } } } } From 5007fb686f1790dbbd8b384085bd67baf390c7b0 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Tue, 23 Aug 2022 17:02:12 +0200 Subject: [PATCH 2/7] Update pairsplot.Rd --- man/pairsplot.Rd | 3 +++ 1 file changed, 3 insertions(+) diff --git a/man/pairsplot.Rd b/man/pairsplot.Rd index e9cc2ba..d5b2f3d 100644 --- a/man/pairsplot.Rd +++ b/man/pairsplot.Rd @@ -21,6 +21,7 @@ pairsplot( shape = NULL, shapekey = NULL, pointSize = 1, + shared.legend = FALSE, legendPosition = "none", legendLabSize = 6, legendIconSize = 1.5, @@ -107,6 +108,8 @@ grouping/categorical variable.} 'shape', e.g., c(A=10, B=21).} \item{pointSize}{Size of plotted points.} + +\item{shared.legend}{Logical, indicating whether to draw a one shared legend for the bi-plots.} \item{legendPosition}{Position of legend ('top', 'bottom', 'left', 'right', 'none').} From cbe2ef905784b78adb00a0216651775f44e40548 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Wed, 24 Aug 2022 17:01:07 +0200 Subject: [PATCH 3/7] Add legendTitleSize argument --- R/pairsplot.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/pairsplot.R b/R/pairsplot.R index 4d0a6f5..e6f1beb 100644 --- a/R/pairsplot.R +++ b/R/pairsplot.R @@ -143,6 +143,7 @@ pairsplot <- function( shared.legend = FALSE, legendPosition = 'none', legendLabSize = 6, + legendTitleSize = 14, legendIconSize = 1.5, xlim = NULL, ylim = NULL, @@ -245,6 +246,7 @@ pairsplot <- function( labSize = labSize, legendPosition = legendPosition, legendLabSize = legendLabSize, + legendTitleSize = legendTitleSize, legendIconSize = legendIconSize, drawConnectors = drawConnectors, widthConnectors = widthConnectors, From a7961971acc5412d10d70dca059fa6127f803706 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Wed, 24 Aug 2022 17:05:31 +0200 Subject: [PATCH 4/7] Update pairsplot.Rd --- man/pairsplot.Rd | 3 +++ 1 file changed, 3 insertions(+) diff --git a/man/pairsplot.Rd b/man/pairsplot.Rd index d5b2f3d..f2d6908 100644 --- a/man/pairsplot.Rd +++ b/man/pairsplot.Rd @@ -24,6 +24,7 @@ pairsplot( shared.legend = FALSE, legendPosition = "none", legendLabSize = 6, + legendTitleSize = 14, legendIconSize = 1.5, xlim = NULL, ylim = NULL, @@ -115,6 +116,8 @@ grouping/categorical variable.} 'none').} \item{legendLabSize}{Size of plot legend text.} + +\item{legendTitleSize}{Size of plot legend title text.} \item{legendIconSize}{Size of plot legend icons / symbols.} From db6a2906e2f89bd0f13fcee612644c99add17b29 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Thu, 25 Aug 2022 09:00:31 +0200 Subject: [PATCH 5/7] Update pairsplot.R --- R/pairsplot.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/pairsplot.R b/R/pairsplot.R index e6f1beb..83af591 100644 --- a/R/pairsplot.R +++ b/R/pairsplot.R @@ -184,7 +184,12 @@ pairsplot <- function( # counter necessary for layout of objects in plot space nplots <- 0 - + + # the shared legend will be on the left + if (shared.legend) { + legendPosition <- 'left' + } + # beginning of the master loop (contains nested loop) # biplots will be created on a pairwise basis for (i in seq_along(components)) { From e4096f44b5544c4fd175c37410c3f4d10f75d601 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Thu, 25 Aug 2022 09:06:05 +0200 Subject: [PATCH 6/7] Update pairsplot.R --- R/pairsplot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/pairsplot.R b/R/pairsplot.R index 83af591..37cf256 100644 --- a/R/pairsplot.R +++ b/R/pairsplot.R @@ -33,7 +33,7 @@ #' @param shapekey Vector of name-value pairs relating to value passed to #' 'shape', e.g., c(A=10, B=21). #' @param pointSize Size of plotted points. -#' @param shared.legend Logical, indicating whether to draw a one shared legend for the bi-plots. +#' @param shared.legend Logical, indicating whether to draw a shared legend for the bi-plots. #' @param legendPosition Position of legend ('top', 'bottom', 'left', 'right', #' 'none'). #' @param legendLabSize Size of plot legend text. From a5bf33b92bdfcca217677589a7acf4c1c80ff238 Mon Sep 17 00:00:00 2001 From: Roy Lardenoije <39619971+lardenoije@users.noreply.github.com> Date: Thu, 25 Aug 2022 09:06:40 +0200 Subject: [PATCH 7/7] Update pairsplot.Rd --- man/pairsplot.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/pairsplot.Rd b/man/pairsplot.Rd index f2d6908..84d4dc4 100644 --- a/man/pairsplot.Rd +++ b/man/pairsplot.Rd @@ -110,7 +110,7 @@ grouping/categorical variable.} \item{pointSize}{Size of plotted points.} -\item{shared.legend}{Logical, indicating whether to draw a one shared legend for the bi-plots.} +\item{shared.legend}{Logical, indicating whether to draw a shared legend for the bi-plots.} \item{legendPosition}{Position of legend ('top', 'bottom', 'left', 'right', 'none').}