YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/contribution-tree-data/
839 stars 173 forks source link

ggtree collapsed clade+images #484

Open grenaud opened 2 years ago

grenaud commented 2 years ago

I posted this on biostars but I am not sure if it is a feature request or it is already possible. In ggtree you can add images to tips as such:

library(ape) tree <- read.nexus("https://raw.githubusercontent.com/rgriff23/Dissertation/master/Chapter_2/data/tree.nex") phylopic_info <- data.frame(node = c(124, 113, 110, 96, 89, 70), phylopic = c("7fb9bea8-e758-4986-afb2-95a2c3bf983d", "bac25f49-97a4-4aec-beb6-f542158ebd23", "f598fb39-facf-43ea-a576-1861304b2fe4", "aceb287d-84cf-46f1-868c-4797c4ac54a8", "0174801d-15a6-4668-bfe0-4c421fbe51e8", "72f2f854-f3cd-4666-887c-35d5c256ab0f"), species = c("galagoids", "lemurs", "tarsiers", "cebids", "hominoids", "cercopithecoids")) pg <- ggtree(tree) pg %<+% phylopic_info + geom_nodelab(aes(image=phylopic), geom="phylopic", alpha=.5, color='steelblue')

d <- data.frame(node = c("70","89","96","110","113","124"), images = c("https://i.imgur.com/8VA9cYw.png", "https://i.imgur.com/XYM1T2x.png", "https://i.imgur.com/EQs5ZZe.png", "https://i.imgur.com/2xin0UK.png", "https://i.imgur.com/hbftayl.png", "https://i.imgur.com/3wDHW8n.png")) pg %<+% d + geom_nodelab(aes(image=images), geom="image")

However, is there anyone who has gotten it to work with collapsed clades using: tree+collapse(node=nodeid)+geom_cladelabel(node=nodeid, "New clade", align=TRUE) for example? I know there is a geom image in geom_cladelabel but can that be used? If so, how?

ammaraziz commented 2 years ago

In both examples you are annotating the branches, not the tips. I'm not sure what you're trying to do. I think you're trying to collapse and annotate the branches. If so, collapse first then annotate, eg:

phylopic_info <- data.frame(node = c(124, 113, 110, 96, 89, 70),
                            phylopic = c("7fb9bea8-e758-4986-afb2-95a2c3bf983d",
                                         "bac25f49-97a4-4aec-beb6-f542158ebd23",
                                         "f598fb39-facf-43ea-a576-1861304b2fe4",
                                         "aceb287d-84cf-46f1-868c-4797c4ac54a8",
                                         "0174801d-15a6-4668-bfe0-4c421fbe51e8",
                                         "72f2f854-f3cd-4666-887c-35d5c256ab0f"),
                            species = c("galagoids", "lemurs", "tarsiers",
                                        "cebids", "hominoids", "cercopithecoids"))
pg <- ggtree(tree)
p = 70
pg = pg %>% collapse(node = p, 'min')
pg = pg %<+% phylopic_info + 
  geom_nodelab(aes(image=phylopic), geom="phylopic", alpha=.5, color='steelblue') + 
  geom_cladelabel(node=p, "New clade", align=TRUE)

Note the %>% instead of + because collapse is not a ggplot* function.