gl.grm.network 'names' attribute must be same length as vector

15 views
Skip to first unread message

Matthew Holen

unread,
Aug 6, 2025, 7:27:42 PMAug 6
to dartR
Hello, 

I'm attempting to use the gl.grm.network function and continue to get an error of 
Error in names(colors_pops) <- as.character(levels(x$pop)) : 'names' attribute [96] must be the same length as the vector [11] In addition: Warning message: In RColorBrewer::brewer.pal(ncolors, palette) : n too large, allowed maximum for palette PuOr is 11 Returning the palette you asked for with that many colors
with the default as:
palette_discrete = gl.select.colors(x, library = "brewer", palette = "PuOr", ncolors = nPop(x), verbose = 0)

Whenever I plot a PCA it correctly assigns colors (albeit not amazing colors), how should I go about getting gl.grm.network to do something similar?
The large number of populations is the use of accessions rather than species or genus as labels. 

Thank you!

Bernd.Gruber

unread,
Aug 6, 2025, 7:59:48 PMAug 6
to da...@googlegroups.com, Luis.Mijangos Araujo

Hi  Matthew,

 

The brewer pals allows only max 11 colors, and you seem to need 96 colors (as it seems). Hence you need to find another way to create your colors using a different library.

 

@Luis.Mijangos Araujo: not sure if the brewer library is hard coded in gl.grm.network, if so this is an error on our side.

 

Cheers, Bernd

--
You received this message because you are subscribed to the Google Groups "dartR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dartr+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/dartr/178e955f-739e-426e-83ef-d00acadbc7ebn%40googlegroups.com.

Jose Luis Mijangos

unread,
Aug 7, 2025, 12:35:37 AMAug 7
to dartR
Hi,

Please give a try to the below code,

Cheers,
Luis 

# 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.gl
r1 <- 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
)

Matthew Holen

unread,
Aug 7, 2025, 8:07:07 AMAug 7
to dartR
Luis,

Thank you for the quick reply. The listed Palette 4 worked great. 

Best,
Matt

Reply all
Reply to author
Forward
0 new messages