我有一个大数据集,但让我们举一个玩具示例:
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)
但我明白了:
我使用第一个 igraph 得到了那个,但我想避免这一步。
更新:
library(data.table)
mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"), stringsAsFactors = T)
mydata 已经在使用因子。我不需要额外的步骤转换因子。
我可以用 igraph 创建绘图:
library(igraph)
mygraph <- graph_from_data_frame(d=mydata, directed=T)
plot(mygraph)
或者使用它的对象来构建一个 DiagrammeR 图:
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)
但现在我尝试直接从 Diagrammer 做,没有 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)
有什么问题?