ToDiagrammeRGraph: Plot a graph, or get a graphviz dot representation of the...

View source: R/node_plot.R

plot.NodeR Documentation

Plot a graph, or get a graphviz dot representation of the tree


Use these methods to style your graph, and to plot it. The functionality is built around the DiagrammeR package, so for anything that goes beyond simple plotting, it is recommended to read its documentation at Note that DiagrammeR is only suggested by data.tree, so 'plot' only works if you have installed it on your system.


## S3 method for class 'Node'
  direction = c("climb", "descend"),
  pruneFun = NULL,
  output = "graph"

ToDiagrammeRGraph(root, direction = c("climb", "descend"), pruneFun = NULL)

SetNodeStyle(node, inherit = TRUE, keepExisting = FALSE, ...)

SetEdgeStyle(node, inherit = TRUE, keepExisting = FALSE, ...)

SetGraphStyle(root, keepExisting = FALSE, ...)




The root node of the data.tree structure to plot


For the SetStyle methods, this can be any stlyeName / value pair. See for details. For the plot.Node generic method, this is not used.


when converting to a network, should the edges point from root to children ("climb") or from child to parent ("descend")?


allows providing a prune criteria, i.e. a function taking a Node as an input, and returning TRUE or FALSE. If the pruneFun returns FALSE for a Node, then the Node and its entire sub-tree will not be considered.


A string specifying the output type; graph (the default) renders the graph using the grViz() function and visNetwork renders the graph using the visnetwork() function.


The root Node of the data.tree structure to visualize.


The Node of the data.tree structure on which you would like to set style attributes.


If TRUE, then children will inherit this node's style. Otherwise they inherit from this node's parent. Note that the inherit always applies to the node, i.e. all style attributes of a node and not to a single style attribute.


If TRUE, then style attributes are added to possibly existing style attributes on the node.


Use SetNodeStyle and SetEdgeStyle to define the style of your plot. Use plot to display a graphical representation of your tree.

The most common styles that can be set on the nodes are:

  • color

  • fillcolor

  • fixedsize true or false

  • fontcolor

  • fontname

  • fontsize

  • height

  • penwidth

  • shape box, ellipse, polygon, circle, box, etc.

  • style

  • tooltip

  • width

The most common styles that can be set on the edges are:

  • arrowhead e.g. normal, dot, vee

  • arrowsize

  • arrowtail

  • color

  • dir forward, back, both, none

  • fontcolor

  • fontname

  • fontsize

  • headport

  • label

  • minlen

  • penwidth

  • tailport

  • tooltip

A good source to understand the attributes is Another good source is the DiagrammeR package documentation, or more specifically:

In addition to the standard GraphViz functionality, the data.tree plotting infrastructure takes advantage of the fact that data.tree structure are always hierarchic. Thus, style attributes are inherited from parents to children on an individual basis. For example, you can set the fontcolor to red on a parent, and then all children will also have red font, except if you specifically disallow inheritance. Labels and tooltips are never inherited.

Another feature concerns functions: Instead of setting a fixed value (e.g. SetNodeStyle(acme, label = "Acme. Inc"), you can set a function (e.g. SetNodeStyle(acme, label = function(x) x$name)). The function must take a Node as its single argument. Together with inheritance, this becomes a very powerful tool.

The GetDefaultTooltip method is a utility method that can be used to print all attributes of a Node.

There are some more examples in the 'applications' vignette, see vignette('applications', package = "data.tree")


SetGraphStyle(acme, rankdir = "TB")
SetEdgeStyle(acme, arrowhead = "vee", color = "blue", penwidth = 2)
#per default, Node style attributes will be inherited:
SetNodeStyle(acme, style = "filled,rounded", shape = "box", fillcolor = "GreenYellow", 
             fontname = "helvetica", tooltip = GetDefaultTooltip)
SetNodeStyle(acme$IT, fillcolor = "LightBlue", penwidth = "5px")
#inheritance can be avoided:
SetNodeStyle(acme$Accounting, inherit = FALSE, fillcolor = "Thistle", 
             fontcolor = "Firebrick", tooltip = "This is the accounting department")
SetEdgeStyle(acme$Research$`New Labs`, 
             color = "red", 
             label = "Focus!", 
             penwidth = 3, 
             fontcolor = "red")
#use Do to set style on specific nodes:
Do(acme$leaves, function(node) SetNodeStyle(node, shape = "egg"))

#print p as label, where available:
SetNodeStyle(acme, label = function(node) node$p)

data.tree documentation built on Nov. 13, 2023, 1:08 a.m.