In this post I will show you how to create simple diagrams in R that can be useful for creating flowcharts and figures using the packages DiagrammeR. So the first thing is to load DiagrammeR package.
DiagrammeR package
This packages works with essentially two types of objects: nodes and edges. Nodes will represent the nodes in a diagram, which usually consist of geometric figures with text. In turn, edges correspond to connections among the nodes that will usually consist of arrows that represent the direction of the workflow. In this example, I will show you how to draw a diagram by adding each node and edge separately; however, for more experienced users it might be easier to use nodes and edge dataframes to define all the nodes and edges in a single data frame.
Example diagram
The first step is to create a graph, using the create_graph function. Then we will use a pipe |> to start creating each node, using add_node. Notice that each node can take the following arguments: label (text that will be shown) and font color of the node, shape of the node, color and fill color of the node. Additionally, you can set if the size of the figure should be set according to the label or a predefined height and width.
The second step is to define each node’s position. Notice that nodes are automatically numbered in the order they were created. Thus, node = 1 refers to the first node that was created using add_node. Each node position can be defined by its x and y coordinates.
Afterward, we will add the edges. In this step, we will use the same numbering of the nodes as in the previous step. Thus, node 1 will be the first created node, while node 2 will be the second. Inside the add_edge function we can set the width of the arrow, as well as its color.
Finally, we can render the graph.
The produced the diagram
Flowchart of a BFAST + ML approach.
If you wish to export the diagram, you can use export_graph.