I'd like to generate a graph from two different sets of nodes which they have different lengths. You can find the codes below.
As you can see I receive an error : Error: length(from) == length(to) is not TRUE !! Is there a solution to this one?
I'd appreciate your help !!
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)
I suspect that you mean connecting each vertex of g1
with each of g2
. After defining nodes1
and nodes2
, let
(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
Since later we are going to use this variable in create_graph
, we want to connect each of 1, 2, 3 with each of 4 and 5. To construct proper arguments for create_edge_df
we are going to use rep
; that is because, as ?create_edge_df
says,
from - a vector of node ID values from which edges are outbound. The vector length must equal that of the to vector.
to - a vector of node ID values to which edges are incoming. The vector length must equal that of the from vector.
So,
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)