mirror of
https://github.com/dchakro/shared_Rscripts.git
synced 2026-05-17 09:12:16 -07:00
simplified
This commit is contained in:
parent
949ee59554
commit
e344473f7d
1 changed files with 41 additions and 51 deletions
|
|
@ -1,28 +1,36 @@
|
||||||
returnPalette <- function(ColorVariable = NULL, GNE.colors = F) {
|
returnPalette <-
|
||||||
# #<---------------------------->
|
function(ColorVariable = NULL,
|
||||||
# # You must include this section when:
|
GNE.colors = F,
|
||||||
# # Distributing, Using and/or Modifying this code.
|
jumble = T) {
|
||||||
# # Please read and abide by the terms of the included LICENSE.
|
# #<---------------------------->
|
||||||
# # Copyright 2023, Deepankar Chakroborty, All rights reserved.
|
# # You must include this section when:
|
||||||
# #
|
# # Distributing, Using and/or Modifying this code.
|
||||||
# # Author : Deepankar Chakroborty (https://github.com/dchakro)
|
# # Please read and abide by the terms of the included LICENSE.
|
||||||
# # Website: https://www.dchakro.com
|
# # Copyright 2023, Deepankar Chakroborty, All rights reserved.
|
||||||
# # Report issues: https://github.com/dchakro/shared_Rscripts/issues
|
# #
|
||||||
# # License: https://github.com/dchakro/shared_Rscripts/blob/master/LICENSE
|
# # 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:
|
# # PURPOSE:
|
||||||
# # Generates an appropriate palette for categorical data based on its length
|
# # Generates an appropriate palette for categorical data based on its length
|
||||||
|
|
||||||
|
|
||||||
if (class(ColorVariable) == "character") {
|
if (class(ColorVariable) == "character") {
|
||||||
colorsNeeded <- length(unique(ColorVariable))
|
colorsNeeded <- length(unique(ColorVariable))
|
||||||
|
}
|
||||||
|
if (class(ColorVariable) == "factor") {
|
||||||
|
colorsNeeded <- length(levels(ColorVariable))
|
||||||
|
}
|
||||||
if (colorsNeeded < 1) {
|
if (colorsNeeded < 1) {
|
||||||
myPalette <- c("#DD0000")
|
myPalette <- c("#DD0000")
|
||||||
warning("Nothing to return color for.")
|
warning("Nothing to return color for.")
|
||||||
}
|
}
|
||||||
if (colorsNeeded > 1 & colorsNeeded < 10 & GNE.colors == T) {
|
if (colorsNeeded > 1 &
|
||||||
|
colorsNeeded < 10 & GNE.colors == T) {
|
||||||
myPalette <-
|
myPalette <-
|
||||||
c(
|
c(
|
||||||
"#003087",
|
"#003087",
|
||||||
|
|
@ -41,38 +49,20 @@ returnPalette <- function(ColorVariable = NULL, GNE.colors = F) {
|
||||||
palette = "Classic Tableau")
|
palette = "Classic Tableau")
|
||||||
} else if (colorsNeeded >= 10 & colorsNeeded < 18) {
|
} else if (colorsNeeded >= 10 & colorsNeeded < 18) {
|
||||||
set.seed(2023)
|
set.seed(2023)
|
||||||
myPalette <- c(palette.colors(n = 10,
|
myPalette <- c(
|
||||||
palette = "Classic Tableau"),
|
palette.colors(n = 10,
|
||||||
palette.colors(n = colorsNeeded - 10,
|
palette = "Classic Tableau"),
|
||||||
palette = "Set3"))
|
palette.colors(n = colorsNeeded - 10,
|
||||||
|
palette = "Set3")
|
||||||
|
)
|
||||||
set.seed(2023)
|
set.seed(2023)
|
||||||
myPalette <- sample(myPalette)
|
myPalette <- sample(myPalette)
|
||||||
} else if (colorsNeeded >= 18) {
|
} else if (colorsNeeded >= 18 & jumble) {
|
||||||
myPalette <- sample(hcl.colors(n = colorsNeeded,
|
myPalette <- sample(hcl.colors(n = colorsNeeded,
|
||||||
palette = "Spectral"))
|
palette = "Spectral"))
|
||||||
|
} else if (colorsNeeded >= 18 & !jumble) {
|
||||||
|
myPalette <- hcl.colors(n = colorsNeeded,
|
||||||
|
palette = "Spectral")
|
||||||
}
|
}
|
||||||
|
return(myPalette)
|
||||||
}
|
}
|
||||||
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)
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue