gl.plot.structure reorder populations?

47 views
Skip to first unread message

Paige M.

unread,
Jun 6, 2025, 8:42:57 PMJun 6
to dartR
Hello,
I am using a mac and running dartRverse 1.6 in Rstudio Version 2025.05.1+513 (2025.05.1+513) with R 4.5.0.

I have successfully run gl.run.structure on a large dataset with many replicates.  It took days to run, but it completed.  I can use the output just fine for the normal gl.plot.structure  and gl.map.structure output and it is great!!

My problem is that I need to reorder the populations in the plot.  I have tried to reorder my populations in the genlight by using gl.sort() for both the ind and pop options. 
for example:
sorted <- gl.sort(result2, sort.by = "ind", order.by = "inds_by_lat.csv")

 I keep getting this error message:
 "Length of order.by does not match length of ind . Check your input parameters and make sure length of your sort.by selection matches the length of the vector provided by order.by"

I have checked and rechecked and the total number of inds (n=350) is exactly the same, which makes sense because I got the original inds list by extracting it from the genlight to make sure it was the same.  

I also tried to do this by using the sort.by = "pop" option and I get the same error.  There are only 18 populations so I can also easily check that I have the total number (and the names) correct. 
for example:  gl.sort(result2, sort.by = "pop", order.by = c("DIAB", "PL_Gina", "PL-Gail", "CIH", "PL-Grace", "PL-Gilda", "VH", "SOL", "PL-HH", "PL-A", "PL-B", "PL-C", "CARP", "MOH", "SW", "MESA", "NAP", "BIRD"))
error message: Error in gl.sort(result2, sort.by = "pop", order.by = c("DIAB", "PL_Gina",  :
  order.by does not contain all levels of sort.by.

I tried to do a workaround and use my gl.run.structure output in pophelper software, but once I got the dartRverse version of the structure output into pophelper it won't  load the q-matrices using readQ() because of some kind of format error in the output files. 

command run: slist <- readQ(files=sfiles2,filetype="structure")
Error in toupper(read1) : invalid multibyte string 4

I also tried:
slist <- readQ(files=sfiles2,filetype="auto")
Error in toupper(read1) : invalid multibyte string 4

I'd really appreciate any advice on how to either get the populations sorted in the genlight using dartRverse so I get the right plot order or an alternative method of changing the plot output from gl.plot.structure. 

Thank you!! For any help and also for the great software!

Paige



Peter Unmack

unread,
Jun 6, 2025, 9:09:54 PMJun 6
to da...@googlegroups.com
The Q file is a comma delimited file, you can open it and plot it in excel.

Cheers
Peter
> --
> 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
> <mailto:dartr+un...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/dartr/
> a3a340c2-52a6-4830-b5a6-e95ca3f6781cn%40googlegroups.com <https://
> groups.google.com/d/msgid/dartr/a3a340c2-52a6-4830-b5a6-
> e95ca3f6781cn%40googlegroups.com?utm_medium=email&utm_source=footer>.

Bernd.Gruber

unread,
Jun 6, 2025, 10:43:16 PMJun 6
to da...@googlegroups.com
Or use ggplot in r. 

The gl.plot.structure returns the qmat

Reorder to your liking and then plot. 


@peter: don’t say the bad word ;-)
---------


On 7 Jun 2025, at 11:10, Peter Unmack <peter...@unmack.net> wrote:

 The Q file is a comma delimited file, you can open it and plot it in excel.
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/5bf89170-243f-4d54...@unmack.net.
Message has been deleted
Message has been deleted
Message has been deleted

Paige M.

unread,
Jun 10, 2025, 7:06:19 PMJun 10
to dartR
Hello, Thank you both for the suggestions.  I'm trying to use Bernd suggestion since I know going to excel might create problems for me (or so google search answers claim), especially since I don't usually plot things in excel so I don't know how.

I know this isn't a dartRverse question but I'm hoping someone has already does this and wouldn't mind sharing to correct Rcode.  I am trying to use ggplot2, but I have little experience.  I reordered the data in the q-matrix and then I had to reshape it to long format to use ggplot2 (this took me a long time to do correctly). I finally got the format correct with the cluster information and ids maintained.  

Now I have used the following Rcode (taken from google search - I've actually tried a bunch of versions but this got me the closest) to attempt to make the barplot:

ggplot(data = q_long, aes(x = population, y = proportion, fill = cluster)) +
  geom_bar(stat = "identity") +  # Use geom_bar with stat="identity" to represent values directly
  labs(title = "Population Structure Plot",  # Add a title
       x = "Individual",
       y = "Structure Proportion") +
  theme_minimal()  # Apply a clean theme

This does give me a barplot, but I can't figure out how to make it have all the individuals from each population represented and delineated lines by population like you get when running gl.plot.structure.

If anyone could share their Rcode to make a structure-like barplot it would be GREATLY appreciated.

Thank you!!

Paige M.

unread,
Jun 10, 2025, 7:07:29 PMJun 10
to dartR
Thank you for the help! I did stay in Rstudio and I finally got a decent barplot.  It took a LONG time because it turns out the output of pivot_longer (the format for the q matrix required for plotting in ggplot) has to be modified in several ways that turned out to be complicated.  But, it does work and I did get a decent plot!! If anyone else has this problem I would be happy to share the Rcode that finally worked.
Best,


On Friday, June 6, 2025 at 7:43:16 PM UTC-7 Bernd. Gruber wrote:

Bernd.Gruber

unread,
Jun 10, 2025, 7:37:11 PMJun 10
to da...@googlegroups.com

HI Paige,

 

Please send me your code. Happy to think if it could be integrated in dartR if it is a more general way to order and plot.

 

Admittedly the current standard version is most often not the way people want it.

 

Or we could use the code (attributed to you) in one of our tutorials..

 

 

 

Cheers, Bernd


> To view this discussion visit https://groups.google.com/d/msgid/dartr/
> a3a340c2-52a6-4830-b5a6-e95ca3f6781cn%40googlegroups.com <https://
> groups.google.com/d/msgid/dartr/a3a340c2-52a6-4830-b5a6-
> e95ca3f6781cn%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
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 http://dartr+un...@googlegroups.com.

--
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.

Paige M.

unread,
Jun 10, 2025, 8:35:04 PMJun 10
to dartR
Hello,
Thank you for the suggestions.  I'm trying to use ggplot as suggested by Bernd.

I have been able to use the q matrix and reorder my populations and generate a barplot using ggplot2 after many attempts and google searches since I don't know how to use ggplot2.  Everyone says its easy but I had to properly change the q matrix to the long format, which I had a lot of trouble with - but finally I got there and ran:

ggplot(data = q_long, aes(x = population, y = proportion, fill = cluster)) +
  geom_bar(stat = "identity", position = "fill") +  # Use geom_bar with stat="identity" to represent values directly
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) #adjust names?

  labs(title = "Population Structure Plot",  # Add a title
       x = "Individual",
       y = "Structure Proportion") +
  theme_minimal()  # Apply a clean theme

I do get a barplot, but it does not look like the gl.plot.structure output because it does not have the individuals data represented in the proportions, only the overall population proportions.  I know this isn't really a dartRverse question but I'm hoping someone has already figured out how to make a plot that looks more like the output of gl.plot.structure using ggplot2 and wouldn't mind sharing the Rcode.  Anybody?  Please?

I would really appreciate any help someone can offer!
Thanks.
On Friday, June 6, 2025 at 7:43:16 PM UTC-7 Bernd. Gruber wrote:

Berry, Olly (NCMI, IOMRC Crawley)

unread,
Jun 10, 2025, 9:36:59 PMJun 10
to da...@googlegroups.com

Have you tried ChatGPT or similar LLM for R coding advice with ggplot? I think you’ll find it a lot more useful than Google searches, though you may need to iterate a few times. E.g. Give it your script and ask it to make the modifications you’re seeking

Cheers,

Olly

 

From: da...@googlegroups.com <da...@googlegroups.com> On Behalf Of Paige M.
Sent: Monday, 9 June 2025 9:58 AM
To: dartR <da...@googlegroups.com>
Subject: Re: [dartR] gl.plot.structure reorder populations?

 

Hello,

Nowicki, Marcin

unread,
Jun 10, 2025, 9:56:23 PMJun 10
to da...@googlegroups.com
heart Nowicki, Marcin reacted to your message:

From: 'Berry, Olly (NCMI, IOMRC Crawley)' via dartR <da...@googlegroups.com>
Sent: Wednesday, June 11, 2025 1:36:51 AM
To: da...@googlegroups.com <da...@googlegroups.com>
Subject: RE: [dartR] gl.plot.structure reorder populations?
 

Paige M.

unread,
Jun 11, 2025, 1:20:31 PMJun 11
to dartR
Hello Bernd,
Here is what I finally got to work for me. I don't need any acknowledgements - I'm just hoping to save other people time.  But thanks for the offer! 

#save copy of q matrix from gl.plot.structure(), in my example I'm just using my best K=3
qmatrix <- q_data
#get rid of extra column(s) that will cause problems with pivot_longer - for example
qmatrix<- qmatrix[, -5]

#reorder columns to match desired format for running plotting - for example
qmatrix <- qmatrix %>% select(Pop, Sample, Cluster1, Cluster2, Cluster3)

#Convert Pop to a factor with your desired order
custom_pop_order <- c("Pop1", "Pop2", "Pop3","", "Pop4")

#apply to Pop column in qmatrix
qmatrix$Pop <- factor(qmatrix$Pop, levels = custom_pop_order)

#Arrange the data: by Pop (your order), then make the Sample stay with correct Pop info.

qmatrix <- qmatrix %>%
  arrange(Pop) %>%
  mutate(Sample = factor(Sample, levels = Sample))

#Now make the correct format for ggplot to work
qmatrix_long <- qmatrix %>%
  pivot_longer(cols = starts_with("Cluster"), names_to = "Cluster", values_to = "Proportion")
 
 
#Below is the R code for a basic plot, but you can play with the palette colors and spacing and labels. Save a version of this code first so you can use it again if any modifications lead to problems.

PCA_plot <- ggplot(qmatrix_long, aes(x = Sample, y = Proportion, fill = Cluster)) +
  geom_bar(stat = "identity", width = 1, color = "#666666", size = 0.05) +
  facet_grid(~Pop, scales = "free_x", space = "free") +  
  scale_y_continuous(labels = scales::percent_format()) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal() +
  theme(
    strip.text.x = element_text(angle = 90),
   axis.text.x = element_blank(),
   #axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1, size = 4),
    axis.ticks.x = element_blank()
  ) +
  labs(
    title = "STRUCTURE Plot (K = 3)",
    x = "",
    y = "Ancestry Proportion"
  )

Best,

Paige M.

unread,
Jun 11, 2025, 1:26:40 PMJun 11
to dartR
Hi Olly,
Thanks for the suggestion and actually I did go to ChatGPT eventually, but for some reason the suggestions never properly worked. Maybe I didn't ask in a way the AI fully understood but the biggest problem it created was the insistence on suggesting the use of the library (reshape2) melt function which led to all kinds of other problems with the data format for plotting.  Maybe I should have tried some other AI option but I did find the solution for attempting to use pivot_longer() properly through a general google search.

Best wishes,
Reply all
Reply to author
Forward
0 new messages