I'm trying to make a haplotype network in R. I have been able to plot it without issue but am having some problems with overlapping nodes despite playing with the scale.ratio values. So I'd like to use tkplot and manually make the changes I need, but am not having any luck introducing the pie and size values that I used under the regular plot function.
I'm fairly new to R so I'm hoping that I just don't know what I'm doing rather than trying something that can't possibly work :)
Thanks in advance for any help!
Here is the code I have so far:
library(pegas)
library(plyr)
library(reshape)
library(igraph)
# Load the alignment
alignment<-read.dna("ON-A-SNP-data - Copy2.fas", format="fasta")
# Load the sites
sites<-read.csv("SITES.csv")
# Calculate haplotype frequencies
h<-haplotype(alignment)
# Build the haplotype network based on the haplotype frequencies.
net<-haploNet(h)
# Add column for haplotypes
sites<-cbind(sites,haplotype=rep(NA, nrow(sites)))
# Assign haplotypes to the samples automatically using a loop
for (i in 1:length(labels(h))){
sites$haplotype[attr(h, "index")[[i]]]<-i
}
# Build a matrix of frequencies that will tell R how to fill pies on haplotype network. #Requires the packages 'plyr' and 'reshape'.
dfcount<-ddply(sites,.(site,haplotype),summarise,freq=length(site))
dc <- cast(haplotype ~ site, data = dfcount, value = "freq", fill = 0)
dc$haplotype<-NULL
dc<-as.matrix(as.data.frame(dc))
# Plot the haplotype network
plot(net, size=attr(net, "freq"), scale.ratio = 3, pie = dc, fast=TRUE, labels=FALSE)
Here is the output of 'net' if this is helpful?
> net
step Prob
[1,] 10 9 1 0.9939576
[2,] 13 12 1 0.9939576
[3,] 14 12 1 0.9939576
[4,] 15 14 1 0.9939576
[5,] 16 14 1 0.9939576
[6,] 17 14 1 0.9939576
[7,] 18 14 1 0.9939576
[8,] 30 29 1 0.9939576
[9,] 31 29 1 0.9939576
[10,] 32 29 1 0.9939576
[11,] 11 9 2 0.9819270
[12,] 19 14 2 0.9819270
[13,] 20 14 2 0.9819270
[14,] 21 14 2 0.9819270
[15,] 22 14 2 0.9819270
[16,] 23 14 2 0.9819270
[17,] 24 14 2 0.9819270
[18,] 25 14 2 0.9819270
[19,] 12 7 3 0.9640739
[20,] 12 9 3 0.9640739
[21,] 34 12 3 0.9640739
[22,] 26 14 3 0.9640739
[23,] 8 7 4 0.9406588
[24,] 29 12 4 0.9406588
[25,] 27 14 4 0.9406588
[26,] 28 14 4 0.9406588
[27,] 33 29 4 0.9406588
[28,] 7 1 5 0.9120671
[29,] 7 2 5 0.9120671
[30,] 5 4 5 0.9120671
[31,] 7 3 6 0.8787358
[32,] 7 4 7 0.8412104
[33,] 7 6 9 0.7560227
attr(,"freq")
[1] 1 1 1 1 1 3 1 1 3 1 1 3 1 9 2 1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 1 1
attr(,"labels")
[1] "I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X"
[11] "XI" "XII" "XIII" "XIV" "XV" "XVI" "XVII" "XVIII" "XIX" "XX"
[21] "XXI" "XXII" "XXIII" "XXIV" "XXV" "XXVI" "XXVII" "XXVIII" "XXIX" "XXX"
[31] "XXXI" "XXXII" "XXXIII" "XXXIV"
attr(,"class")
[1] "haploNet"