Here is how I got edges colored in a dendrogram with ggraph. Use “node.” in front of the node data column you want.
random-code-snippets
visualization
graphing
dendrogram
Author
Robert M Flight
Published
August 3, 2021
I wanted to color the dendrogram edges according to their class in ggraph, and I was getting stuck because of something that isn’t explicitly mentioned in the documentation, but is implied. You must use “node.” to access the data from the Node Data in the call to aes(...).
Lets set it up. We will borrow from the “Edges” vignette in the ggraph package (Pederson 2021).
library(ggraph)
Loading required package: ggplot2
library(tidygraph)
Attaching package: 'tidygraph'
The following object is masked from 'package:stats':
filter
library(purrr)library(rlang)
Attaching package: 'rlang'
The following objects are masked from 'package:purrr':
%@%, as_function, flatten, flatten_chr, flatten_dbl, flatten_int,
flatten_lgl, flatten_raw, invoke, splice
Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
Nice! But what if we want the leaves colored by which “Class” they belong to?
Note the differences in this function call compared to the previous:
Using geom_edge_elbow2 instead of geom_edge_elbow
Using node.Class, not just Class.
The second point is really important! When you look at the hierarchy object printed above, the Class bit is part of the Node Data, which gets identified by ggraph by the prefix “node.”.
Error in `geom_edge_elbow2()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'Class' not found