Biostar Beta. Not for public use.
Minimum spanning tree - coloring the nodes
0
Entering edit mode
14 months ago
tpaisie • 70
University of Florida

Hi everyone,

I'm trying to plot a minimum spanning tree with the Rgraphviz package. I am able to make the tree, but the nodes fillcolor keeps plotting as transparent, instead of the colors I designated. Here is the R code I'm currently using to plot the MST. Any help with edits to the script to fill in the node colors according the colors I designate in the script would be greatly appreciated!!

library(R6)
library(digest)
library(htmltools)
library(Rcpp)
library(httpuv)
library(mime)
library(xtable)
library(RJSONIO)
library(shiny)
library(gtable)
library(plyr)
library(proto)
library(stringr)
library(reshape2)
library(colorspace)
library(munsell)
library(scales)
library(ggplot2)
library(ape)
library(BiocGenerics)
library(graph)
library(RBGL)
library(Rgraphviz)

data <- read.dna(file = "SNP_Phage_38seq.fasta", format = "fasta")
#generate a distance matrix
dist <- dist.dna(data,model="N", 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)

#one method to plot mst
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 )
nel.graph <-as(adjMST, Class="graphNEL")


#edgeNames can be used to obtain a vector of all edge names, and it takes the 
#argument recipEdges so that the output correctly matches which edges will be used by Rgraphviz
edgeNames(nel.graph)
edgeNames(nel.graph, recipEdges = "distinct")

#Using edge wheights for labels
ew <- as.character(unlist(edgeWeights(nel.graph)))
ew <- ew[setdiff(seq(along = ew), removedEdges(nel.graph))]
names(ew) <- edgeNames(nel.graph)
eAttrs <- list()
eAttrs$label <- ew


#Creating a list of all nodes and edges for further control
nodes <- buildNodeList(nel.graph)
edges <- buildEdgeList(nel.graph)
edges <- buildEdgeList(nel.graph, edgeAttrs = eAttrs)
nodes

#edgeA <- list(color = "black")

nAttrs <- list()
nAttrs$color <- c(`BS988p_HT12` = "magenta3",
                      `BS975p_HT02` = "magenta3",
                      `BS982p_08` = "gray",
                      `BS1046p_DM08` = "green",
                      `BS1047p_HT08` = "magenta3",
                      `SH199p_HT14` = "magenta3",
                      `BS979p_DM05` = "green",
                      `BS1074p_HT14` = "magenta3",
                      `BS1057p_HT13` = "magenta3",
                      `BS1044p_DM05` = "green",
                      `BS989p_13` = "gray",
                      `BS1039p_HT13` = "magenta3",
                      `BS980p_05` = "gray",
                      `BS1024p_GF05` = "gray",
                      `BS938p_12` = "gray",
                      `BS1025p_HT08` = "magenta3",
                      `BS971p_HT11` = "magenta3",
                      `BS937p_HT10` = "magenta3",
                      `BS984p_HT10` = "magenta3",
                      `BS968p_HT10` = "magenta3",
                      `BS976p_DM14` = "green",
                      `BS1042p_DM05` = "green",
                      `BS1022p_DM04` = "green",
                      `BS1023p_DM05` = "green",
                      `BS1059p_HT14` = "magenta3",
                      `BS1043p_HT05` = "magenta3",
                      `BS1045p_DM07` = "green",
                      `BS942p_10` = "gray",
                      `BS1041p_DM99` = "green",
                      `BS986p_DM10` = "green",
                      `Shigella_phagePOCJ13` = "gray",
                      `BS981p_DM05` = "green",
                      `BS951p_05` = "gray",
                      `SH200p_14` = "gray",
                      `BS974p_HT01` = "magenta3",
                      `BS1021p_HT03` = "magenta3",
                      `BS1060p_HT14` = "magenta3",
                      `BS972p_12` = "gray")


plot(nel.graph, attrs = list(node = list(label="foo", fontsize=40), 
                             edge = list(color="black", weight=1.5, fontsize=40), 
                             graph = list(overlap=FALSE)),edgeAttrs = eAttrs, nodeAttrs = nAttrs, "neato")



legend("bottomleft", inset=.01, col=c("magenta3","green","gray"), pch=19, legend=c("Haiti", "Dominican Republic", "Other"), 
       box.col="white", box.lwd=1, text.col="black", title=expression(bold("Location")), text.font=1, cex=1, trace=TRUE)

Thanks for the help!!!

Taylor

ADD COMMENTlink
0
Entering edit mode

Can't be sure but, in igraph, one changes vertex colour with:

V(g)$color

[g is the graph object]

Take a look at my tutorial: Network plot from expression data in R using igraph

ADD REPLYlink
0
Entering edit mode

Thanks for the tip! I figured out how to color code the different nodes with the following edits to my R script:

data <- read.dna(file = "SNP_Phage_38seq.fasta", format = "fasta")
dist <- dist.dna(data,model="N", as.matrix=TRUE)
dist.g<-as(dist,Class="graphNEL")
ms<-mstree.kruskal(dist.g)

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 )
nel.graph <-as(adjMST, Class="graphNEL")

edgeNames(nel.graph)
edgeNames(nel.graph, recipEdges = "distinct")

#Using edge wheights for labels
ew <- as.character(unlist(edgeWeights(nel.graph)))
ew <- ew[setdiff(seq(along = ew), removedEdges(nel.graph))]
names(ew) <- edgeNames(nel.graph)
eAttrs <- list()
eAttrs$label <- ew


#Creating a list of all nodes and edges for further control
nodes <- buildNodeList(nel.graph)
edges <- buildEdgeList(nel.graph)
edges <- buildEdgeList(nel.graph, edgeAttrs = eAttrs)
nodes


nAttrs <- makeNodeAttrs(nel.graph, fillcolor = c(`BS988p_HT12` = "magenta3",
                                                 `BS975p_HT02` = "magenta3",
                                                 `BS982p_08` = "gray",
                                                 `BS1046p_DM08` = "green",
                                                 `BS1047p_HT08` = "magenta3",
                                                 `SH199p_HT14` = "magenta3",
                                                 `BS979p_DM05` = "green",
                                                 `BS1074p_HT14` = "magenta3",
                                                 `BS1057p_HT13` = "magenta3",
                                                 `BS1044p_DM05` = "green",
                                                 `BS989p_13` = "gray",
                                                 `BS1039p_HT13` = "magenta3",
                                                 `BS980p_05` = "gray",
                                                 `BS1024p_GF05` = "gray",
                                                 `BS938p_12` = "gray",
                                                 `BS1025p_HT08` = "magenta3",
                                                 `BS971p_HT11` = "magenta3",
                                                 `BS937p_HT10` = "magenta3",
                                                 `BS984p_HT10` = "magenta3",
                                                 `BS968p_HT10` = "magenta3",
                                                 `BS976p_DM14` = "green",
                                                 `BS1042p_DM05` = "green",
                                                 `BS1022p_DM04` = "green",
                                                 `BS1023p_DM05` = "green",
                                                 `BS1059p_HT14` = "magenta3",
                                                 `BS1043p_HT05` = "magenta3",
                                                 `BS1045p_DM07` = "green",
                                                 `BS942p_10` = "gray",
                                                 `BS1041p_DM99` = "green",
                                                 `BS986p_DM10` = "green",
                                                 `Shigella_phagePOCJ13` = "gray",
                                                 `BS981p_DM05` = "green",
                                                 `BS951p_05` = "gray",
                                                 `SH200p_14` = "gray",
                                                 `BS974p_HT01` = "magenta3",
                                                 `BS1021p_HT03` = "magenta3",
                                                 `BS1060p_HT14` = "magenta3",
                                                 `BS972p_12` = "gray"), shape = "circle")



plot(nel.graph, attrs = list(node = list(label="foo", fontsize=40), 
                             edge = list(color="black", weight=1.5, fontsize=50), 
                             graph = list(overlap=FALSE)),edgeAttrs = eAttrs, nodeAttrs = nAttrs, "neato")



legend("bottomleft", inset=.01, col=c("magenta3","green","gray"), pch=19, legend=c("Haiti", "Dominican Republic", "Other"), 
       box.col="white", box.lwd=1, text.col="black", title=expression(bold("Location")), text.font=1, cex=1, trace=TRUE)
ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.3