2

I want to create a flowchart with the R package DiagrammeR. The text of some of the nodes should have a line break.

Consider the following reproducible example in R:

library("DiagrammeR")

# Create a node data frame (ndf)
ndf <- create_node_df(n = 4,
                      label = c("hi stacko", "aaa", "bbb", "ccc"))

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

# 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 %>%
  render_graph()

enter image description here

In this flowchart, I would like to add a line break between "hi" and "stacko" in the lower left node. I found some sources that suggested <br> or \n. Unfortunately, both did not work.

Question: How could I insert a line break in DiagrammeR?

Joachim Schork
  • 2,025
  • 3
  • 25
  • 48

1 Answers1

3

This works for me:

ndf <- create_node_df(n = 4,label = c("hi\nstacko", "aaa", "bbb", "ccc"))

and, when run with the remainder of the code, produces the following diagram:

enter image description here

Constantinos
  • 1,327
  • 7
  • 17
  • Thanks a lot! For some reason, I was not able to do it with /n before, but with your code it works. I also applied your code to my real data and that works as well. – Joachim Schork Feb 05 '18 at 08:35