我想从两个具有不同长度的不同节点集生成一个图形。您可以在下面找到代码。
如您所见,我收到一个错误:错误:length(from)== length(to)不是TRUE !! 有解决方案吗?
感谢您的帮助!
library(DiagrammeR)
g1<- c("a","b","c")
g2<-c("d","f")
#creating a node data frame
nodes1<- create_node_df(n=length(g1),
style= "filled",
color= "lightblue",
shape= "box")
nodes2<- create_node_df(n=length(g2),
style= "filled",
color= "lightblue",
shape= "box")
edges<-create_edge_df(from = g1,
to= g2,
rel= "related",
color= "black")
Error: length(from) == length(to) is not TRUE
all_nodes<- combine_ndfs(nodes1, nodes2)
create_graph(nodes_df = all_nodes,
edges_df = edges,
directed = TRUE)
答案 0 :(得分:2)
我怀疑您的意思是将g1
的每个顶点与g2
的每个顶点相连。定义nodes1
和nodes2
之后,让
(all_nodes <- combine_ndfs(nodes1, nodes2))
# id type label style color shape
# 1 1 <NA> <NA> filled lightblue box
# 2 2 <NA> <NA> filled lightblue box
# 3 3 <NA> <NA> filled lightblue box
# 4 4 <NA> <NA> filled lightblue box
# 5 5 <NA> <NA> filled lightblue box
因为稍后我们将在create_graph
中使用此变量,所以我们希望将1、2、3、4和5分别连接起来。要为create_edge_df
构造适当的参数,我们将使用rep
;正如?create_edge_df
所说,
from-边缘出站的节点ID值的向量。 向量长度必须等于to向量的长度。
to-边缘进入的节点ID值的向量。 向量长度必须等于from向量的长度。
所以
edges <- create_edge_df(from = rep(1:3, 2), to = rep(4:5, each = 3),
rel = "related", color = "black")
create_graph(nodes_df = all_nodes, edges_df = edges, directed = TRUE)