emmanuelparadis / pegas

Population and Evolutionary Genetics Analysis System
GNU General Public License v2.0
28 stars 10 forks source link

Fixing node scale across pegas haplotype network plots #48

Closed ksw9 closed 4 years ago

ksw9 commented 4 years ago

0

I've made haplotype networks in pegas according to: How to plot Pie charts in haploNet Haplotype Networks {pegas}

I am making multiple haplotype networks and would like the node sizes to be consistent across plots. Is there a way to fix this so that, for instance, a specific size node always means a single individual has that haplotype?

Thank you in advance!

The following is the code I use for each network.

 h <- haplotype(msa)
 h <- sort(h, what = "label")
 net <- haploNet(h)

  # Get status
  ind.hap <- with(
        utils::stack(setNames(attr(h, "index"), rownames(h))),
        table(h = ind, pop = attr(msa, "name")) )

# Plot network
plot(net, bty = 'L', xpd = FALSE, labels = FALSE, pie = ind.hap,show.mutation = 2,scale.ratio = 1.5,cex = .5,
       size = attr(net, "freq"), bg = inc_col_pal[dimnames(ind.hap)$pop])

haplotype_network_tmp

emmanuelparadis commented 4 years ago

Hi,

You can set xlimand ylim to be the same for all plots, something like:

xl <- c(-10, 10)
yl <- c(-15, 16)
plot(net, xlim = xl, ylim = yl, .....) # for all nets

That will work nicely if all plots use the same space on your graphical device. Otherwise, for instance if you use:

layout(matrix(1:6, 2, 3, byrow = TRUE), widths = c(1, 1, 2))

to split your device you'll need to rescale xl and yl with respect to the size of the plots (but apparently this is not your case from the pic you included).

Cheers,

Emmanuel

ksw9 commented 4 years ago

Thank you! This is exactly what I was looking for.

All the best,

naurasd commented 1 year ago

@emmanuelparadis:

Could you give an example of how exactly the rescaling of xl and yl would look like with these 6 networks when using layout()?

Thanks a lot Nauras

emmanuelparadis commented 1 year ago

Let's say you want to plot 3 networks on a single row (side by side), then find their respective values for 'xlim', store them in xl1, xl2, and xl3:

widths <- c(diff(xl1), diff(xl1), diff(xl3)) / diff(xl1)
layout(matrix(1:6, 2, 3, byrow = TRUE), widths = widths)

If you want to plot them on a single column (stacked), you do the same but with 'ylim' and then setting 'heights' in layout(). When combining both, you have to be careful that 'heights' and 'widths' will (obviously) be the same for each row and column, respectively. So if the values of 'ylim' and 'xlim' are not the same within each row and column, that may not have the desired effect.

naurasd commented 1 year ago

Merci ;-)