Read a directed graph in R

心已入冬 提交于 2019-12-03 08:18:36
user1317221_G

This seems to work fine for me:

 xlist<-read.table("graph.txt")
 xlist <- graph.data.frame(xlist)
 plot(xlist)

Note R changes nodes and indexes them from zero upwards (not in the most recent igraph update as @Sacha Epskamp comments below). Using:

plot(xlist, vertex.label= V(xlist)$name)

you will see the names that you want. i.e. edges between 1 and 2.

One way to plot shortest paths is use get.all.shortest.paths and then use this to subset your graph and overplot it. See my answer to this question for similar example where I plot a spanning tree.

The reason for the error is that edge list files (i.e. format "edgelist") number the vertices from zero. This is the same in all igraph versions, even if recent R igraph packages number the vertices from one.

The reason for this is that is we want R igraph to be consistent with Python igraph and C igraph, as far as file formats go. I.e. a file (in this case an edge list file) written by Python igraph is interpreted the same way by R igraph and Python igraph. The zero-based numbering is a property of the file format.

If you already have an edge list file that starts numbering the vertices with one, that is not the igraph edgelist file format, and you need to convert it, by simply subtracting one from each vertex id.

A workaround is to read in the file with scan() or read.table() (scan() is much faster, which might be important if you have large files), and then call graph() directly. In this case you don't need to subtract one, because in R igraph vertex ids are numbered from one.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!