Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

branch node labels returned but not set by attribute "labels" #151

Open
rrohwer opened this issue Dec 15, 2024 · 1 comment
Open

branch node labels returned but not set by attribute "labels" #151

rrohwer opened this issue Dec 15, 2024 · 1 comment
Labels

Comments

@rrohwer
Copy link

rrohwer commented Dec 15, 2024

Describe the bug
I am new to dendextend so I hope this is not just a silly mistake, but I am trying to set the internal node labels (the branch labels?) of a dendrogram. When I get the label attributes, those seem to be included. But when I set the label attributes, it only seems to change the leaf labels.

To Reproduce

set.seed(1)
d <- as.dendrogram(hclust(dist(matrix(sample(1:10,50,T),5,dimnames = list(LETTERS[1:5])))))
plot(d) # a simple dendrograms with leaves labelled by capital letters

my.labs <- get_nodes_attr(dend = d, attribute = "label", include_leaves = T, include_branches = T)
my.labs # includes NA's which corresping to the branch node labels
# [1] NA  NA  "A" "C" NA  "E" NA  "B" "D"

my.labs[is.na(my.labs)] <- paste0("b", 1:length(my.labs[is.na(my.labs)]))
my.labs # same order as before but with b1, b2, b3 added for the branch labels
# [1] "b1" "b2" "A"  "C"  "b3" "E"  "b4" "B"  "D" 

d <- dendextend::set(dend = d, what = "labels", value = my.labs)
get_nodes_attr(dend = d, attribute = "label", include_leaves = T, include_branches = T) # still has NA for internal node labels and has renamed leaf labels with the first part of the my.labs vector
# [1] NA   NA   "b1" "b2" NA   "A"  NA   "C"  "b3"
plot(d) # and can see that new branch b1 etc labels are mixed in to the leaf labels

Expected behavior
I expected get_nodes_attr(attribute = "label") to be like the inverse of set(what = "labels").

Additional context
I thought that by setting these internal node labels I would be able to use them as the id as in get_nodes_attr(dend = d, attribute = "label", id = "b1") but maybe I am also misunderstanding what ID means here.

Thank you for supporting this great package!
Robin

@rrohwer rrohwer added the bug label Dec 15, 2024
@rrohwer
Copy link
Author

rrohwer commented Dec 18, 2024

Although the label and labels not matching is still a little confusing, I understand what's going on now and I don't think it's a bug- changing this issue to "enhancement" request.
Seems like dendextend is a convenient wrapper for setting dendrogram attributes in baseR, and it just doesn't include all possible attributes, just the most popular ones :) So it doesn't include edgetext (ie the branch labels) as a possible attribute in set(what = ), and these are a separate base attribute from label which every node could have. It's confusing because plot() will display only leaf labels and branch text, not internal node labels.

So without dendextend you could do this manually, one at a time or to a bunch of nodes with dendrapply:

attr(x = d[[1]], which = "edgetext") <- "branch label"
plot(d)

It would be cool if dendextend could adjust this conveniently too! These branch labels seem handy for annotating visualizations and the basic plot function recognizes them.

p.s. I also mentioned id but I realize now that id= is only numeric- it seems like unlike lists dendrograms cannot have named elements, and any names are only label attributes and can't be used to subset nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant