简体   繁体   中英

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.

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.

If I just remove the NA row the lonely node is not plotted.

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. 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.

One way to get what you want is to leave out the single node, but then add it using 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) 

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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