From 9b4c9c37e850341740478be70785da322ebbf205 Mon Sep 17 00:00:00 2001 From: pwwang Date: Thu, 21 Nov 2024 10:07:43 -0700 Subject: [PATCH] feat: support named palcolor and keep the order (pwwang/scplotter#12) --- DESCRIPTION | 1 + R/theming.R | 6 +++++- man/LinePlot.Rd | 2 +- man/ScatterPlot.Rd | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d43cdbf..1a3ce8e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,6 +40,7 @@ Imports: cowplot, zoo Suggests: + testthat, ComplexHeatmap, clustree, gglogger, diff --git a/R/theming.R b/R/theming.R index 0c2c706..d4a0f13 100644 --- a/R/theming.R +++ b/R/theming.R @@ -197,13 +197,17 @@ palette_this <- function( n_x <- nlevels(x) if (isTRUE(attr(palcolor, "type") == "continuous")) { color <- colorRampPalette(palcolor)(n_x) + names(color) <- levels(x) + } else if (!is.null(names(palcolor))) { + color <- palcolor[intersect(names(palcolor), levels(x))] } else { color <- ifelse(rep(n_x, n_x) <= pal_n, palcolor[1:n_x], colorRampPalette(palcolor)(n_x) ) + names(color) <- levels(x) } - names(color) <- levels(x) + if (any(is.na(x))) { color <- c(color, setNames(NA_color, "NA")) } diff --git a/man/LinePlot.Rd b/man/LinePlot.Rd index 2e858ab..1d62bf4 100644 --- a/man/LinePlot.Rd +++ b/man/LinePlot.Rd @@ -202,5 +202,5 @@ LinePlot(data, x = "x", y = "y", group_by = "group", add_bg = TRUE) LinePlot(data, x = "x", y = "y", group_by = "group", facet_by = "facet") LinePlot(data, x = "x", y = "y", group_by = "group", split_by = "facet") LinePlot(data, x = "x", y = "y", split_by = "group", - palcolor = list("G1" = c("red","blue"), "G2" = c("green", "black"))) + palcolor = list(G1 = c("red", "blue"), G2 = c("green", "black"))) } diff --git a/man/ScatterPlot.Rd b/man/ScatterPlot.Rd index 74e4b18..7fe4456 100644 --- a/man/ScatterPlot.Rd +++ b/man/ScatterPlot.Rd @@ -186,5 +186,5 @@ ScatterPlot(data, x = "x", y = "y", size_by = "w", color_by = "t", # Change color per plot ScatterPlot(data, x = "x", y = "y", split_by = "t", - palcolor = c(A = "blue", B = "red")) + palcolor = list(A = "blue", B = "red")) }