[R-sig-genetics] How to avoid overlapping pies when plotting haplotype networks when using the packages 'pegas' and 'ape' in R

Emmanuel Paradis emm@nue|@p@r@d|@ @end|ng |rom |rd@|r
Tue Jul 26 18:11:33 CEST 2022


Another solution which works only with pegas: set the lengths of the links between haplotypes equal to the sum of the sizes of their respective symbols:

## extract the size of the haplotype symbols:
size <- attr(net3, "freq")
## make a copy of the network:
mynet <- net3
## change the lengths of the links by changing the 3th column:
mynet[, 3] <- size[net3[, 1]] + size[net3[, 2]]

We now plot the modified network:

o <- plot(mynet, size = size, cex = 0.8, pie = ind.hap3, threshold = 0, show.mutation = 0, scale.ratio = 2)

'o' is a list that includes (among other things) the coordinates; we extract them and adjust the names:

xy <- setNames(o[c("xx", "yy")], c("x", "y"))

We can plot the original network using these coordinates so that the mutations will be correctly displayed

plot(net3, xy = xy, size = size, cex = 0.8, pie = ind.hap3, threshold = 0, scale.ratio = 2, legend = TRUE)

The haplotype labels overlap quite a bit, so adding 'labels = FALSE' gives a nice result too (unless you want print these labels).

Best,

Emmanuel

----- Le 25 Juil 22, à 18:45, Emmanuel Paradis emmanuel.paradis using ird.fr a écrit :

> Dear Steen,
> 
> There is no simple way to handle your problem. One possibility is to use the
> package 'network' which has a nicer algorithm to calculate the layout of nodes
> than pegas has. The good thing is that 'network' returns the coordinates which
> can then be input into plot.haploNet (see ?replot for something similar in
> pegas). Here's a possible solution:
> 
> ## net3 is from your script
> library(network)
> xy <- plot(as.network(net3))
> xy <- list(x = xy[, 1], y = xy[, 2])
> size <- round(sqrt(attr(net3, "freq")))
> plot(net3, size = size, xy = xy, pie = ind.hap3, legend = TRUE, threshold = 0)
> 
> Note the layout returned by network is random, so you can repeat the 2nd line
> above several times if you want, but it seems a single try is enough!
> 
> The second trick is to change the scale of the pie sizes using a square root
> transformation. I'm afraid that when the sizes of symbols are too contrasted,
> plot.haploNet() is really at a pain! I'll try to have a look at it if this can
> be improved.
> 
> In the last command, you'll still need to click on the plot to show where to
> draw the legend, but R will print the coordinates so these may be included in
> the script (or more simply you can delete the 'legend = TRUE' of you don't want
> the legend).
> 
> Note that you did a TCS network (has done by pegas::haploNet) which has a lot of
> alternative links (hence the 'threshold = 0' option). You may try an RMST
> network which has much less alternatives links:
> 
> d3 <- dist.dna(h3, "N", p = TRUE)
> rmst3 <- rmst(d3)
> 
> Plotting can be done with:
> 
> xybis <- plot(as.network(rmst3))
> xybis <- list(x = xybis[, 1], y = xybis[, 2])
> plot(rmst3, size = sz, xy = xybis, pie = ind.hap3, legend = TRUE, threshold = 0)
> 
> Note that, if this is useful for you, you can use 'xy = xy' in the last command
> so that the layout of haplotypes will be identical with the two networks.
> 
> You may also look in more details this help page in network:
> 
> ?network.layout.fruchtermanreingold
> 
> It explains how the change the parameters of the layout calculations.
> 
> HTH
> 
> Best,
> 
> Emmanuel
> 
> ----- Le 2 Juin 22, à 9:07, Steen Wilhelm Knudsen swknudsen using snm.ku.dk a écrit :
> 
>> Dear All subscribers of the R-sig-genetics Archives,
>> 
>> I have a question to plotting haplotype networks using the packages 'pegas' and
>> 'ape' in R.
>> I often end up with overlapping pies for haplotypes, and appear unable to work
>> around it by adjusting the 'scale.ratio' when making the plot.
>> I could abstain from using the frequency of the haplotypes as determining the
>> size of the pies. But I would prefer to keep this setting, so that the diagram
>> ends up having circles that reflects the sample size. Instead I would like to
>> know if I can adjust the length of the lines between the pies, and perhaps
>> determine where the pies are to be positioned in the plot.
>> My understanding of the lines between the pies is that these lines are adjusted
>> in length to reflect the number of differences between the haplotypes. This is
>> fine, but seems to prevent me from avoiding the pies being overlaid. I am happy
>> to have incorrect lengths for the connecting lines, and can instead just use
>> the option that adds a number on such a connecting line, a number that  informs
>> how many differences there are between the haplotypes being connected by the
>> line.
>> I have had a go with the 'replot' function, but for some reason I can only
>> select the same pie over and over again. Also, I would prefer to do the entire
>> plotting by code alone. One of my motivations from switching away from
>> 'Network', 'popart' and 'TCS' was to avoid all the clicking and moving around
>> with pies.
>> 
>> I have placed my input file here
>> https://github.com/monis4567/Mnemiopsis_leidyi_in_NE_Atlantic/blob/main/suppmat01_inp_files/algn_Mnelei_18s_10.aligned.fasta.fas
>> 
>> My R code for making a haplotype network with this input file can be found here
>> https://github.com/monis4567/Mnemiopsis_leidyi_in_NE_Atlantic/blob/main/suppmat03_Rcodes/Rcode_example02_make_haplonet_v01.R
>> 
>> 
>> 
>> Can anyone suggest me how I can adjust this code to get haplotype networks where
>> pies do not overlap, or how I can adjust the length of the connecting lines, to
>> avoid the pies overlapping. Or suggest me a different solution that can sort
>> out my problem, but preferably let me solve the issue of presenting a diagram
>> that is possible to read while performing all the coding in R, and not having
>> to adjust the plot manually afterwards?
>> 
>> Thanks in advance for your time and help,
>> 
>> Steen Knudsen
>> Natural History Museum of Denmark
>> 
>> 
>> 
>>	[[alternative HTML version deleted]]
>> 
>> _______________________________________________
>> R-sig-genetics mailing list
>> R-sig-genetics using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-genetics



More information about the R-sig-genetics mailing list