AdeelK93 / collapsibleTree

Create Interactive Collapsible Tree Diagrams in R using D3.js
https://adeelk93.github.io/collapsibleTree/
158 stars 41 forks source link

Feature Request: Different node colors (stroke, fill) at same level in hierarchy? #8

Open NovasTaylor opened 7 years ago

NovasTaylor commented 7 years ago

My understanding is that colors can be applied to nodes within a level of the tree as either a gradient or solid, single color at that level.

I often have nodes of different types within the same level in of the tree. It would be ideal if colors could be defined for individual nodes based on a 'type' assignment determined by another column in the dataframe or regex'ing their name within the collapsibleTree call.

Any thoughts on if this is possible now, or could be part of a future version? In attached example using D3js, the node stroke for the namespace "custom" is orange, the others green. Having the ability to color node stroke and fill individually would open up a lot of functionality. May be difficult to implement?

nodecolors

Great work so far!

AdeelK93 commented 7 years ago

Every row in the data frame corresponds to a leaf in the tree. Mapping a column to color would allow you to define leaf colors, but wouldn't allow you to map colors to their parents in any deterministic way.

Right now you can manually define the color of every node using a vector of colors (which will be filled either horizontally or vertically) but I can understand that being inconvenient. Regexing is certainly an option, but I'm sure there's plenty of use cases where you don't want that extra text in the label.

How about maybe a helper function to more easily convert a vector of nodes into a vector of colors? Vector of nodes can be generated with:

listAllNodes <- function(df, hierarchy, root = deparse(substitute(df))) {
  # preserve this name before evaluating df
  root <- root

  # the hierarchy that will be used to create the tree
  df$pathString <- paste(
    root,
    apply(df[,hierarchy], 1, paste, collapse = "//"),
    sep="//"
  )

  # convert the data frame into a data.tree node
  node <- data.tree::as.Node(df, pathDelimiter = "//")

  # convert all nodes into a character vector
  network <- ToDataFrameNetwork(node)
  c(network[1,1], network$to)
}