Create graph function

Create graph function in diagramR package may be useful to do graph in a simple and an easy way. Three examples are given to show the simplicity and brightness of this function. The examples show how to make four nodes and connect node 1 with nodes 3 and 4. Also, connect node 2 with nodes 3 and 4.

Example 1: standard setting

This example uses the standard tools to make the nodes or edges without any detals.

# library(dplyr)
library(DiagrammeR)

ndf1 <- create_node_df( n=4) # make 4 nodes
  
edf1 <- create_edge_df( # Edges to connect node
  from = c(1, 1, 2,2),
  to   = c(3, 4, 3,4))

graph1 <- create_graph(nodes_df=ndf1,edges_df=edf1) # make graph
graph1 %>% render_graph() # show graph

Example 2: general setting

This example uses more tools to make general setting for all nodes and edges.

library(DiagrammeR)

ndf2 <- create_node_df( # general setting (shape,...) for all nodes
  n = 4,
  shape = 'circle',
  style = 'filled', 
  color = 'green',
  fillcolor = 'yellow',
  fontsize = 11,
  fontcolor = 'black'
)

edf2 <- create_edge_df(# general setting (style,...) for all edges
  from = c(1, 1, 2, 2),
  to   = c(3, 4, 3, 4),
  color = 'blue',
  arrowhead = 'vee'
)

graph2 <- create_graph(nodes_df=ndf2,edges_df=edf2,attr_theme = 'lr')
graph2 %>% render_graph()

Example 3: detailed setting

This example uses more tools to make detailed setting for each node and edge.

library(DiagrammeR)

ndf3 <- create_node_df( # control (shape, color,...) for each node
  n = 4,
  label = c('A1', 'A2', 'A3','A4'),
  shape = c('circle', 'square', 'diamond', 'triangle'),
  style = c('filled', 'dashed', 'solid', 'diagonals'), 
  color = c('green', 'black', 'red', 'blue'),
  fillcolor = c('yellow','orange','gray', 'green'),
  fontsize = c(10,11,12,14),
  fontcolor = c('black','blue','black','blue')
)

edf3 <- create_edge_df( # control ((shape, color,...) for each edge
  from = c(1, 1, 2,2),
  to   = c(3, 4, 3,4),
  style = c('bold', 'dashed', 'dotted','dashed'),
  penwidth = c(1,2,3,4),
  color = c('blue','green','red', 'red'),
  arrowsize = c(0.2,0.3,0.4,0.5),
  arrowhead = c('vee','curve','dot', 'tee'),
  label = c('A','B','C', 'D')
)

graph3 <- create_graph(nodes_df=ndf3,edges_df=edf3,attr_theme = 'lr')

graph3 %>% render_graph()