get vertex xy coords from igraph mst plot
2
0
Entering edit mode
Andrew Box ▴ 10
@andrew-box-13730
Last seen 5.7 years ago
Kansas City, MO - Stowers Institute for…

I'm interested in getting the X and Y coordinates from a minimum spanning tree to use for some non-igraph plotting and export for merging with another data set.  However, I'm having trouble understanding how to obtain the coordinates from the graph object.  It seems that typically, the layout would contain the coordinates for plotting vertices, but plotting of mst graphs doesn't use a separate layout object that I can tell.  I mean, you can specify a layout with vertex coords given, but it would defeat the purpose of allowing the tree to be drawn in a mst style, preventing edge overlaps etc.  

None of the attributes functions (i.e. get.vertex.attributes) seem to return anything but empty lists.  Looking at the mst function isn't terribly informative as it seems to call out to C code.

What am I missing?  Here's what I'm doing to get the mst graph object.  This data is coming from a matrix  (mstmatrix below) where rows are cluster IDs from a clustering operation, columns are mean feature values.

mydist <- dist(mstmatrix, method = 'euclidean', diag =TRUE, upper = TRUE) #generate distance matrix object
distmat <- as.matrix(mydist) #convert to matrix object
g <- graph.adjacency(distmat, weighted = TRUE) #create adjacency matrix
mymst <- mst(g)
plot(mymst)

 

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] RColorBrewer_1.1-2 igraph_1.1.2       pastecs_1.3-18     boot_1.3-20        reshape2_1.4.2     stringr_1.2.0     
 [7] ggplot2_2.2.1      plyr_1.8.4         pheatmap_1.0.8     edgeR_3.16.5       limma_3.30.13      Matrix_1.2-11     

loaded via a namespace (and not attached):
 [1] locfit_1.5-9.1   Rcpp_0.12.13     lattice_0.20-35  grid_3.3.3       gtable_0.2.0     magrittr_1.5    
 [7] scales_0.5.0     stringi_1.1.5    rlang_0.1.2      lazyeval_0.2.0   tools_3.3.3      munsell_0.4.3   
[13] pkgconfig_2.0.1  colorspace_1.3-2 tibble_1.3.4 

igraph • 3.6k views
ADD COMMENT
0
Entering edit mode
Malcolm Cook ★ 1.6k
@malcolm-cook-6293
Last seen 4 months ago
United States

The graph does not holds its own plotting coordinates.  

Look at ?layout_  ?plot.igraph ?igraph.plotting and you will learn how to get multiple different coordinates sets for the same plot depending upon choice of layout algorithm.

To give you a sense: in the following example, the same graph, g, is laid out in different manners:

 g <- sample_gnp(100, 3/100)
 xy<-layout_(g,with_kk())
 plot(g,layout=xy)
 xy<-layout_(g,in_circle())
 plot(g,layout=xy)
ADD COMMENT
0
Entering edit mode
Andrew Box ▴ 10
@andrew-box-13730
Last seen 5.7 years ago
Kansas City, MO - Stowers Institute for…
layout <- layout_with_fr(mymst, dim = 2, niter = 500)

This is what I was looking for.  Part of the mistake I was making was running the layout on my original graph object 'g', not the mst graph object 'mymst'.   Layout now contains the x and y coordinates for the vertices of the minimum spanning tree that draws the tree in a typical way where notes are separated by similarity/distance, and no edges overlap.

ADD COMMENT

Login before adding your answer.

Traffic: 699 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6