AdeelK93 / collapsibleTree

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

Colour tree nodes using the first hierarchy ? #47

Closed YonghuiDong closed 5 years ago

YonghuiDong commented 5 years ago

Hi AdeelK93,

How to colour the tree nodes based on only levels of the first hierarchy ?

Thanks.

AdeelK93 commented 5 years ago

That should definitely be possible using a data.tree input, which grants a much greater degree of flexibility than a data frame. Have you tried that?

YonghuiDong commented 5 years ago

Dear Adeel Khan,

Thanks a lot for your prompt reply. My data was generated using data.tree. But I am not familiar with collapsibleTree package, I don't know how to colour tree nodes using the first hierarchy.

I use a simple example here, in which I would like to colour the node based on continent, countries belong to the same continent will have the same colour, and colours for countries belong to different continent will be different.

Could you please help me how to do it?

Thanks a lot.

## prepare the data
library(data.tree)
library(collapsibleTree)
library(treemap)
data(GNI2014)
GNI2014$pathString <- paste("world", 
                            GNI2014$continent, 
                            GNI2014$country, 
                            sep = "/")

## plot
collapsibleTree(
  GNI2014,
  hierarchy = c("continent", "country"),
  width = 800,
  zoomable = FALSE
)
YonghuiDong commented 5 years ago

Thanks, problem solved.