From e344473f7daf965a9e6c374a45d0b7aa60878f3b Mon Sep 17 00:00:00 2001 From: dchakro <35454738+dchakro@users.noreply.github.com> Date: Tue, 22 Aug 2023 13:46:49 -0700 Subject: [PATCH] simplified --- returnPalette.R | 92 ++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 51 deletions(-) diff --git a/returnPalette.R b/returnPalette.R index 4c2866e..8e7eb63 100644 --- a/returnPalette.R +++ b/returnPalette.R @@ -1,28 +1,36 @@ -returnPalette <- function(ColorVariable = NULL, GNE.colors = F) { - # #<----------------------------> - # # You must include this section when: - # # Distributing, Using and/or Modifying this code. - # # Please read and abide by the terms of the included LICENSE. - # # Copyright 2023, Deepankar Chakroborty, All rights reserved. - # # - # # Author : Deepankar Chakroborty (https://github.com/dchakro) - # # Website: https://www.dchakro.com - # # Report issues: https://github.com/dchakro/shared_Rscripts/issues - # # License: https://github.com/dchakro/shared_Rscripts/blob/master/LICENSE - # #<----------------------------> - - - # # PURPOSE: - # # Generates an appropriate palette for categorical data based on its length - - - if (class(ColorVariable) == "character") { - colorsNeeded <- length(unique(ColorVariable)) +returnPalette <- + function(ColorVariable = NULL, + GNE.colors = F, + jumble = T) { + # #<----------------------------> + # # You must include this section when: + # # Distributing, Using and/or Modifying this code. + # # Please read and abide by the terms of the included LICENSE. + # # Copyright 2023, Deepankar Chakroborty, All rights reserved. + # # + # # Author : Deepankar Chakroborty (https://github.com/dchakro) + # # Website: https://www.dchakro.com + # # Report issues: https://github.com/dchakro/shared_Rscripts/issues + # # License: https://github.com/dchakro/shared_Rscripts/blob/master/LICENSE + # #<----------------------------> + + + # # PURPOSE: + # # Generates an appropriate palette for categorical data based on its length + + + if (class(ColorVariable) == "character") { + colorsNeeded <- length(unique(ColorVariable)) + } + if (class(ColorVariable) == "factor") { + colorsNeeded <- length(levels(ColorVariable)) + } if (colorsNeeded < 1) { myPalette <- c("#DD0000") warning("Nothing to return color for.") } - if (colorsNeeded > 1 & colorsNeeded < 10 & GNE.colors == T) { + if (colorsNeeded > 1 & + colorsNeeded < 10 & GNE.colors == T) { myPalette <- c( "#003087", @@ -41,38 +49,20 @@ returnPalette <- function(ColorVariable = NULL, GNE.colors = F) { palette = "Classic Tableau") } else if (colorsNeeded >= 10 & colorsNeeded < 18) { set.seed(2023) - myPalette <- c(palette.colors(n = 10, - palette = "Classic Tableau"), - palette.colors(n = colorsNeeded - 10, - palette = "Set3")) + myPalette <- c( + palette.colors(n = 10, + palette = "Classic Tableau"), + palette.colors(n = colorsNeeded - 10, + palette = "Set3") + ) set.seed(2023) myPalette <- sample(myPalette) - } else if (colorsNeeded >= 18) { + } else if (colorsNeeded >= 18 & jumble) { myPalette <- sample(hcl.colors(n = colorsNeeded, palette = "Spectral")) + } else if (colorsNeeded >= 18 & !jumble) { + myPalette <- hcl.colors(n = colorsNeeded, + palette = "Spectral") } - } - if (class(ColorVariable) == "factor") { - colorsNeeded <- length(levels(ColorVariable)) - if (colorsNeeded < 1) { - myPalette <- c("#DD0000") - warning("Nothing to return color for.") - } - if (colorsNeeded > 1 & colorsNeeded < 10) { - myPalette <- palette.colors(n = colorsNeeded, - palette = "Classic Tableau") - } else if (colorsNeeded >= 10 & colorsNeeded < 18) { - set.seed(2023) - myPalette <- c(palette.colors(n = 10, - palette = "Classic Tableau"), - palette.colors(n = colorsNeeded - 10, - palette = "Set2")) - set.seed(2023) - myPalette <- sample(myPalette) - } else if (colorsNeeded >= 18) { - myPalette <- sample(hcl.colors(n = colorsNeeded, - palette = "Spectral")) - } - } - return(myPalette) -} \ No newline at end of file + return(myPalette) + } \ No newline at end of file