阅读R中的有向图 [英] Read a directed graph in R

查看:172
本文介绍了阅读R中的有向图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法阅读/创建有向图。我遵循了我在,查看了我绘制生成树的类似示例。


I have trouble reading/creating a directed graph. I followed the steps I have found here.

This is my text file graph.txt:

1 2
1 3
2 5
3 4
3 5
4 5
5 6
5 10
6 7
7 8
7 9
7 12
8 9
9 10
9 11
9 12
10 11
11 7
11 12

Now I read this graph.txt:

library("igraph")
xlist<-read.graph("graph.txt", format="edgelist")

And then I plot it:

plot(xlist)

But it is not the graph I have read into xlist:

As you can see there is no edge between 1->2, 1->3, 5->10 and so on. How can I read the directed graph correctly?

Having done this, how can I show all shortest paths between two nodes?

解决方案

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.

这篇关于阅读R中的有向图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆