The muir package allows users to explore a data.frame using the tree data structure with minimal effort by simply providing the data.frame columns to be explored. In addition, the muir package allows for more targeted tree data structures to be created with specific column criteria as a method for documenting and communicating the data structure and relationships within a data set. These data tree structures can be viewed within the RStudio console, standard browers, or saved as HTML for sharing using the htmlwidgets package.

The package legerages the infrastructure provided by DiagrammeR.


Install the development version of muir from GitHub using the devtools package.


Using muir

Basic example

A basic example using muir to explore the mtcars data set showing the ‘cyl’ and ‘carb’ columns and the relationship between them. Providing the “*” qualifier (after the “:” separator) will show the top occuring values for those columns up to the limit indicated in the node.limit value. By default, the node.limit is set to 3 to curb run-away queries and unreadable trees. The value can be set explicitly when calling the muir function.

The resulting tree will be rendered starting with a level 0 node counting all rows in the data set. Each resulting level will be based on the columns provided by the user and will include nodes for each distinct value (up to the limit provided, in ascending order based on occurrences). Subsequent levels will carry the filters from previous parent nodes forward. Percentages will be provided for each node (compared to the level 0 count) by default and can be turned off if not desired.

tree.height and tree.width values control how the tree is rendered and can be adjusted to best fit trees of various depths and widths.

mtTree <- muir(data = mtcars, node.levels = c("cyl:*", "carb:*"), 
               tree.height = 1200, tree.width = 800)

More complicated example

Instead of just returning top counts for columns provided in node.levels, provide custom filter criteria and custom node titles in level.criteria (level.criteria could also be read in from a stored file (e.g., a crtieria.csv) as a data.frame)

The criteria data.frame below includes the column names, operators, and associated values (e.g., “cyl” <= 4), and a node title to accompany each node generated for that filter criteria. Adding a “+” suffix after the column name in the node.levels parameter will add an extra “Other” node that will aggregate all values node already provided in the level.criteria value or for values below the node.limit provided.

Additional label values can be provided with the label.vals parameter using dplyr summary functions. Custom labels for each value can be provided by adding custom text after the “:” separator.

Lastly, the direction the tree is drawn can be changed from the default left-to-right to a top-to-bottom (“TB”) rendering by providing a new value for tree.dir.

criteria <- data.frame(col = c("cyl", "cyl", "carb"),
                       oper = c("<=", ">", "=="),
                       val = c(4, 4, 2),
                       title = c("Up to 4 Cylinders", "More than 4 Cylinders", "2 Carburetors"))

mtTree <- muir(data = mtcars, node.levels = c("cyl", "carb:+"),
               level.criteria = criteria,
               label.vals = c("min(wt):Min Weight", "max(wt):Max Weight"),
               tree.dir = "TB",
               tree.height = 400, tree.width = 800)

More examples

More examples can be found in the examples in the muir function