简体   繁体   English

使用graph_from_data_frame在igraph中绘制孤立的节点。 失踪

[英]Plotting isolated nodes in igraph with graph_from_data_frame. Missing

I'm working with data.frames with columns "from" and "to" and I would like to create network graphs from them. 我正在使用具有“从”和“到”列的data.frames,我想从它们创建网络图。

For example: 例如:

mydata <- data.table(from=c("John", "John", "Jim", "Jesse"),
   to=c("John", "Jim", "Jack", NA))
mygraph <- graph_from_data_frame(d=mydata, directed=T)
plot(mygraph, vertex.label.dist=2) 

The presence of that NA produces an error. 该NA的存在会产生错误。

If I just remove the NA row the lonely node is not plotted. 如果仅删除NA行,则不会绘制孤独节点。

mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))
mygraph <- graph_from_data_frame(d=mydata, directed=T)
plot(mygraph, vertex.label.dist=2) 

在此处输入图片说明

I would like to get the same result than with: 我希望得到与以下结果相同的结果:

g4 <- graph( c("John", "Jim", "Jim",  "Jack", "John", "John"), isolates=c("Jesse") )  
plot(g4,  vertex.label.dist=2) 

在此处输入图片说明

but working with two columns, from and to. 但要使用两个列,从and到。 How can I get the same result? 如何获得相同的结果? When any of the "from" or "to" is NA then just plot the node without edges and without producing errors. 当“从”或“到”中的任何一个为NA时,只需绘制无边且无错误的节点即可。

One way to get what you want is to leave out the single node, but then add it using add_vertices 一种获得所需内容的方法是忽略单个节点,然后使用add_vertices将其添加

library(igraph)
mydata <- data.frame(from=c("John", "John", "Jim"),
   to=c("John", "Jim", "Jack"))
mygraph <- graph_from_data_frame(d=mydata, directed=T)
mygraph = add_vertices(mygraph, 1, name="Jesse")
plot(mygraph, vertex.label.dist=2) 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM