I want to create a flowchart with the DiagrammeR
package in R. The color of the flowchart should be set according to an identifier at the beginning of each variable name.
Consier the following reproducible example:
library("DiagrammeR")
# Create a node data frame (ndf)
ndf <- create_node_df(n = 4,
label = c("^color", "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) %>%
set_global_graph_attrs(
attr = c("style", "fillcolor"),
value = c("filled", "darkolivegreen3"),
attr_type = c("node", "node"))
# Create a PDF file for the graph (`graph.pdf`)
graph %>%
render_graph()
With the set_global_graph_attrs
function, I am able to set the global colors. I would like to color every variable with ^ in the beginning in red. In the example, the variable ^color should be colored red. The rest of the flowchart should be kept green. Unfortunately, I wasn't able to find a solution for this problem.
Question: How could I use a different color for some nodes of my flowchart?
You can set the node attributes individually like this.
ndf$fillcolor = as.character(NA)
ndf$fillcolor[grep('\\^', ndf$label)] = 'red'