How to create a network graph with DiagrammeR?

好久不见. 提交于 2019-12-11 00:57:54

问题


I have a large dataset but let's put a toy example:

mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))

nodesd=unique(c(mydata$from, mydata$to))
nodes <-   create_node_df(  n=length(nodesd), label=nodesd, type=nodesd)
edges <-   create_edge_df(from = mydata$from, to =  mydata$to,  rel = "leading_to")
graph <-   create_graph(  nodes_df = nodes,     edges_df = edges)
render_graph(graph)

But I get this:

Instead of the expected result:

I got that one using first igraph, but I'd like to avoid that step.

UPDATE:

library(data.table)
mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"), stringsAsFactors = T)

mydata is already using factors. I don't need extra steps converting factors.

I can create the plot with igraph:

library(igraph)
mygraph <- graph_from_data_frame(d=mydata, directed=T)
plot(mygraph)

Or use its object to build a DiagrammeR plot:

V(mygraph)$label = V(mygraph)$name
V(mygraph)$name = factor(V(mygraph)$name, levels=as.character(V(mygraph)$name))
mygraph2 <- from_igraph(mygraph)
render_graph(mygraph2)

But now I try to do it directly from Diagrammer, without igraph:

nodesd = unique(unlist(mydata[,.(from,to)]))
nodes <-   create_node_df(  n=length(nodesd), label=nodesd)
edges <-   create_edge_df(from = mydata$from, to =  mydata$to,  rel = "leading_to")
graph <-   create_graph(  nodes_df = nodes, edges_df = edges)
render_graph(graph)

What's the problem?


回答1:


With your 1st code I got:

> mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))
> nodesd=unique(c(mydata$from, mydata$to))
> nodes <-   create_node_df(  n=length(nodesd), label=nodesd, type=nodesd)
> edges <-   create_edge_df(from = mydata$from, to =  mydata$to,  rel = "leading_to")
Warning messages:
1: In create_edge_df(from = mydata$from, to = mydata$to, rel = "leading_to") :
  NAs introduced by coercion
2: In create_edge_df(from = mydata$from, to = mydata$to, rel = "leading_to") :
  NAs introduced by coercion
> graph <-   create_graph(  nodes_df = nodes,     edges_df = edges)
> render_graph(graph)

As @user20650 said, it is an issue with character and factors. So I make a change.

mydata <- data.frame(from=c("John", "John", "Jim"),
                     to=c("John", "Jim", "Jack"))
mydata$from <- as.character(mydata$from)
mydata$to <- as.character(mydata$to)
nodesd = unique(c(mydata$from, mydata$to))
nodes <- create_node_df(  n=length(nodesd), label=nodesd, type=nodesd)
edges <- create_edge_df(from = factor(mydata$from, levels = nodesd),
                        to =  factor(mydata$to, levels = nodesd),
                        rel = "leading_to")
graph <- create_graph(nodes_df = nodes, edges_df = edges)
render_graph(graph)

I got the result below.

Result:

I hope it can help.



来源:https://stackoverflow.com/questions/44749930/how-to-create-a-network-graph-with-diagrammer

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