Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DiagrammeR: How to add labels beside arrows?

Tags:

I would like to create a flowchart with the DiagrammeR package in R. The graphic should look as in the following example, but I would like to add some labels above the arrows.

Consider the following reproducible example in R:

library("DiagrammeR")

# Create a node data frame (ndf)
ndf <- create_node_df(n = 4,
                      shape = c("rectangle"))

# Create an edge data frame (edf)
edf <- create_edge_df(from = c(1, 2, 3, 3),
                      to = c(4, 3, 1, 4),
                      rel = c("a", "b", "c", "d"))

# Create a graph with the ndf and edf
graph <- create_graph(nodes_df = ndf,
                      edges_df = edf)

# Create a PDF file for the graph (`graph.pdf`)
graph %>%
  export_graph(file_name = "graph.pdf",
               title = "Simple Graph")

With this code, I can create the following graph:

enter image description here

Above the 4 arrows of the graph I would like to add the labels a, b, c and d. Unfortunately, I wasn't able to find anything about that in the documentation. It seems like I am doing something wrong with the rel argument in within the function create_edge_df.

like image 551
Joachim Schork Avatar asked Jan 30 '18 13:01

Joachim Schork


1 Answers

I think you add label = my_vector_of_labels into the definition of create_edges:

# Create an edge data frame (edf) using diagrammer v 0.9.2
edf <- create_edge_df(from = c(1, 2, 3, 3),
                  to = c(4, 3, 1, 4),
                  rel = c("a", "b", "c", "d"),
                  label = c("a", "b", "c", "d"))
like image 198
Russ Hyde Avatar answered Sep 23 '22 12:09

Russ Hyde