# Load core packages
library(dartRverse) # dartR tools and data structures for genetic analyses
library(colorspace) # advanced color palettes (e.g., HCL-based)
# Compute genomic relationship matrix from a genlight object
t1 <-
testset.glr1 <- gl.grm(t1)
# Number of populations (to determine how many colors we need)
n_cols <- nPop(t1)
# Custom “polychrome” palette: an arbitrary base set of 36 colors
# • darken() applies a slight darkening so colors remain distinct
# • rep(...)[1:n] repeats the palette infinitely, so you can request any n
polychrome <- function(n) {
p <- darken(
setNames(
c(
"#3283FE","#FEAF16","#B00068","#1CFFCE","#90AD1C","#2ED9FF",
"#DEA0FD","#AA0DFE","#F8A19F","#325A9B","#C4451C","#1C8356",
"#85660D","#B10DA1","#FBE426","#1CBE4F","#FA0087","#FC1CBF",
"#F7E1A0","#C075A6","#782AB6","#AAF400","#BDCDFF","#822E1C",
"#B5EFB5","#7ED7D1","#1C7F93","#D85FF7","#683B79","#66B0FF",
"#5A5156","#E4E1E3","#F6222E","#FE00FA","#16FF32","#3B00FB"
),
c(
"Dark_Purplish_Gray","Purplish_White","Vivid_Red","Vivid_Purple",
"Vivid_Yellowish_Green","Strong_Purplish_Blue","Vivid_Orange_Yellow",
"Vivid_Purplish_Red","Brilliant_Green","Vivid_Yellow_Green",
"Vivid_Blue","Brilliant_Purple","Vivid_Violet","Strong_Pink",
"Strong_Blue","Strong_Reddish_Orange","Vivid_Green","Light_Olive_Brown",
"Vivid_Reddish_Purple","Vivid_Greenish_Yellow","Vivid_Yellowish_Green",
"Vivid_Red","Vivid_Purplish_Red","Pale_Yellow","Strong_Reddish_Purple",
"Vivid_Violet","Vivid_Yellow_Green","Very_Light_Blue","Strong_Reddish_Brown",
"Very_Light_Yellowish_Green","Very_Light_Bluish_Green","Deep_Greenish_Blue",
"Vivid_Purple","Deep_Purple","Brilliant_Blue","Vivid_Violet"
)
),
amount = 0.2
)
# Return exactly n colors by recycling the base palette
rep(p, ceiling(n/length(p)))[1:n]
}
# Palette 1: our custom “polychrome” – truly unlimited via recycling
pal1 <- polychrome(n_cols)
# Palette 2: HCL-based rainbow – smooth hues around the circle
# rainbow_hcl() can generate any number of distinct hues
pal2 <- rainbow_hcl(n_cols)
# Palette 3: Predefined HCL sequence (“Dark 3”) – unlimited length
# Good for qualitative displays with controlled contrast
pal3 <- hcl.colors(n_cols, palette = "Dark 3")
# Palette 4: Base-R rainbow via dartR helper – unlimited
# gl.select.colors() wraps base R’s palette functions
pal4 <- gl.select.colors(library = 'baseR', palette = 'rainbow', ncolors = n_cols)
# Build and plot a GRM network, coloring populations with an unlimited palette
r2 <- gl.grm.network(
G = r1,
x = t1,
standardise = TRUE,
palette_discrete = pal4 # you can swap in pal1, pal2, or pal3 as needed
)