Question: Draw Minimum Spanning Tree (Mst) With Pie Charts In R
2
gravatar for Mathias Walter
7.7 years ago by
Mathias Walter40 wrote:

Hi,

I do some genotyping within R and want to display the results using a minimum spanning tree (MST) and draw some additional data (i. e. species distribution) onto the nodes, like it was done with the picture below obtained from this paper: http://www.pnas.org/content/105/37/14130.full.

I found some packages to draw the MST but was not able to draw the pie charts. Sure, I can reinvent the wheel and use TechingDemos subplot function. But does an R package or function exists which just do this for me?

The ape package has some options to draw pie or stacked bar charts at the nodes of a dendrogram but not at the nodes of an MST (mst function).

A similar question was asked here (How to draw of minimum spanning tree of sequences?) but the answer does not provide an R package.

Minimum Spanning Tree (MST)

ADD COMMENTlink written 7.7 years ago by Mathias Walter40
1
gravatar for Joseph Hughes
7.7 years ago by
Joseph Hughes2.8k
Scotland, UK
Joseph Hughes2.8k wrote:

Hi Mathias,

You will need to use the ape, graph and RBGL packages to do a simple MST from a file of fasta sequence, as follows:

data <- read.dna(("sequences.fasta"), format="fasta")
#generate a distance matrix
dist <- dist.dna(data,model="raw", as.matrix=TRUE)
#creates an undirected graph
dist.g<-as(dist,Class="graphNEL") 
#generates the minimum spanning tree using kruskal algorithm (you can also use mstree.prim for the prim algorithm)
ms<-mstree.kruskal(dist.g)

To make anything pretty with pie charts at the nodes, you will need to delve into graph and Rgraphviz packages. Section 6 in this Rgraphviz tutorial might lead you to a solution.

ADD COMMENTlink modified 5 months ago by RamRS25k • written 7.7 years ago by Joseph Hughes2.8k

Thanks for the answer. Unfortunately, I'm not very familiar with the graph and RBGL package and I stopped at drawing the graph.

How do you convert the ms into a graph and plot it?

I used the following code:

e <- buildEdgeList(dist.g)
n <- buildNodeList(dist.g)
z <- agopen(nodes=n, edges=e, edgeMode="directed", name="")
plot(z)

But R does not draw the graph. It just consumes all the CPU.

--
Mathias

ADD REPLYlink modified 5 months ago by RamRS25k • written 7.7 years ago by Mathias Walter40

There are a number of ways you can plot it out. This is my convoluted way:

fromto<-cbind(ms$edgeList[1,],ms$edgeList[2,],ms$weight[1,])
adjMST<-ftM2adjM(as.matrix(fromto[,1:2]),W=fromto[,3],edgemode="undirected")
am.graph<-new("graphAM", adjMat=adjMST )
plot(am.graph, attrs = list(node = list(fillcolor = "lightblue"),edge = list(arrowsize=0.5)),"neato")
ADD REPLYlink modified 5 months ago by RamRS25k • written 7.7 years ago by Joseph Hughes2.8k

Hi Joseph, I used your code and there is a mistake when running the last line: "cannot coerce type 'S4' to vector of type 'double' ". I'm not very familiar with R, could you give me a hand with this problem? Thank you!

--Du

ADD REPLYlink modified 7.5 years ago • written 7.5 years ago by dupengcheng.icdc0

Hi Joseph ! Thank you for your script, it works very good. However, I would like to know how to add edge weights in the MStree. Thanks!

ADD REPLYlink modified 19 months ago • written 19 months ago by neiradekhil0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1863 users visited in the last hour