I am working with an R library ‘aweSOM.’ The library has a plot ‘aweSOMplot’ with palettes restricted to RcolorBrewer.
Of course, Viridis + Viridis mako are a subset of the RcolorBrewer library. However, when I try to select a subset or direction for the viridis palettes in the aweSOMplot, it doesn’t seem to work.
I wondered if someone could help me change the source code below (or the script code) such that the mako palette is available, and from the mako palette I can choose a subset of the palette for the ‘palsc’ argument.
Any help would be appreciated.
#############
#AWESOM PLOT
##############
install.packages("aweSOM")
install.packages("kohonen")
install.packages("RColorBrewer")
library(aweSOM)
library(kohonen)
library(RColorBrewer)
#DATA
X1 <- rep(1:3, times = 100)
X1 <- sample(X1)
X2 <- rep(1:3, times = 100)
X2 <- sample(X2)
X3 <- rep(1:3, times = 100)
X3 <- sample(X3)
X4 <- rep(1:3, times = 100)
X4 <- sample(X4)
X5 <- rep(1:3, times = 100)
X5 <- sample(X5)
Dat <- data.frame(X1, X2, X3, X4, X5)
Dat <- data.frame(lapply(Dat, function(x) as.numeric(as.character(x))))
Dat <- as.matrix(Dat)
#SET SEED
set.seed(145)
#SOM INITIALISATION
init <- somInit(Dat, 3, 3)
#SOM OBJECT
SOM <- kohonen::som(Dat, grid = kohonen::somgrid(3, 3, "hexagonal"),
rlen = 100, alpha = c(0.05, 0.01),
dist.fcts = "manhattan", init = init, keep.data = TRUE)
#CLUSTERING ON THE SOM
threeclusters3x3 <- cluster::pam(SOM$codes[[1]], 3)
clus3x3 <- threeclusters3x3$clustering
#PLOTTING THE SOM
plot < - aweSOMplot(som = SOM, type = "Barplot", data = Dat,
variables = c("X1", "X2", "X3", "X4", "X4"),
superclass = clus3x3,
showAxes = FALSE,
values = "median",
palsc = "Blues",
palvar = "Greys")
plot
######################
## SOURCE CODE
#######################
getPalette <- function(pal, n, reverse= F) {
if(pal == "grey") {
res <- grey(1:n / n)
} else if(pal == "rainbow") {
res <- substr(rainbow(n), 1, 7)
} else if(pal == "heat") {
res <- substr(heat.colors(n), 1, 7)
} else if(pal == "terrain") {
res <- substr(terrain.colors(n), 1, 7)
} else if(pal == "topo") {
res <- substr(topo.colors(n), 1, 7)
} else if(pal == "cm") {
res <- substr(cm.colors(n), 1, 7)
} else if (pal == "viridis") {
if (n == 1) {
res <- substr(viridis::viridis(3), 1, 7)[1]
} else if (n == 2) {
res <- substr(viridis::viridis(3), 1, 7)[c(1,3)]
} else
res <- substr(viridis::viridis(n), 1, 7)
} else {
if (n == 1) {
res <- RColorBrewer::brewer.pal(3, pal)[1]
} else if (n == 2) {
res <- RColorBrewer::brewer.pal(3, pal)[c(1,3)]
} else
res <- RColorBrewer::brewer.pal(n, pal)
}
if (length(res) == 1)
res <- list(res)
if (reverse)
res <- rev(res)
res
}