YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
830 stars 172 forks source link

Unable to choose "encircle" for unrooted tree #530

Open soneill93237 opened 2 years ago

soneill93237 commented 2 years ago

I posted my problem on stack overflow and a user suggested that it was because of a bug. https://stackoverflow.com/questions/73517862/is-there-a-way-to-highlight-multiple-clades-of-an-unrooted-phylogenetic-tree-usi

I want to plot an unrooted phylogenetic tree that neatly highlights selected clades.

I like the results of using geom_hilight() from ggtree with type = 'encircle', but I do not like having to individually edit the node and color values for every input. (see method 1)

method 2 is close to what I want, but with the wrong highlight type (roundrect)

method 3 uses the correct highlight type (encircle) but returns an error.

# I don't think all of these packages are needed to reproduce this problem
library(ape)
library(dplyr)
library(GGally)
library(ggalt)
library(ggforce)
library(ggplot2)
library(ggtree)
library(tidyr)
library(tidytree)
library(treeio)

#my pipeline uses the output of RAxML. I made this simpler tree for discussion purposes.
sink("test.tree")
cat("((((((t24:0.8024311261,t11:0.7828436729):0.3048173019,(t21:0.4867131179,t18:0.2167164627):0.7519672168):0.5776117099,t5:0.4223263576):0.5963104749,(t17:0.1558260066,t20:0.41109852):0.09447153704):0.2661841849,((((t6:0.009324073093,t12:0.2732205035):0.7790091021,t10:0.08588226303):0.3282297731,t9:0.2075715209):0.664191803,(((t15:0.5832811284,t14:0.8461383074):0.6081165755,t19:0.5950602938):0.7095833826,t8:0.7146228608):0.7801561591):0.6674923887):0.654328516,(((t13:0.6356930537,t3:0.8536336934):0.8644152461,t2:0.1784738901):0.7129137593,t23:0.8907998055):0.3618239218,((t16:0.1825823467,t7:0.8856151809):0.4720220205,(t22:0.672613536,(t1:0.9215354125,(t4:0.9248593273,t25:0.5937075356):0.3007316259):0.6941311779):0.6789765966):0.2112918347);")
sink()
#import tree
tree1 <- read.tree("test.tree")
#choose root nodes and colors for highlighting clades
group.roots <- c(34, 28, 44, 41)
group.colors <- c("#fd00fe", "#62ce75", "#9a1073", "#4ad9e1")
#write a data frame
g <- data.frame(gnode = group.roots, gfill = group.colors)

tree1unrooted <- ggtree(tree1,layout = 'unrooted')
#method 1: I want my plot to look like this, but I do not want to use so many instances of "geom_hilight()"
tree1unrooted + geom_label(aes(label = node)) +
geom_hilight(
node = 34,
alpha = 1,
fill = "#fd00fe",
type = "encircle",
to.bottom = TRUE
) +
geom_hilight(
node = 28,
alpha = 1,
fill = "#62ce75",
type = "encircle",
to.bottom = TRUE
) +
geom_hilight(
node = 44,
alpha = 1,
fill = "#9a1073",
type = "encircle",
to.bottom = TRUE
) +
geom_hilight(
node = 41,
alpha = 1,
fill = "#4ad9e1",
type = "encircle",
to.bottom = TRUE
)

#method 2: I have used this method to highlight multiple clades successfully with "type = 'roundrect'", but the highlighed regions overlap.
tree1unrooted +
geom_hilight(
data = g,
mapping = aes(node = gnode, fill = gfill),
alpha = 1,
type='roundrect',
to.bottom = TRUE
)

#method 3: I need "type = 'encircle'" for my plot. This gives the error: "Error in FUN(X[[i]], ...) : object 'x' not found"
tree1unrooted +
geom_hilight(
data = g,
mapping = aes(node = gnode, fill = gfill),
alpha = 1,
type='encircle',
to.bottom = TRUE
)
xiangpin commented 1 year ago

In fact, the geom_hilight can use the type automatically according to the layout of ggtree. In default, the unrooted will use encircle type. I can not reproduce the issue. which version of ggtree are you using? How about re-installing ggtree by remotes::install_github("YuLab-SMU/ggtree").

> library(ggtree)
ggtree v3.5.3 For help: https://yulab-smu.top/treedata-book/

If you use the ggtree package suite in published research, please cite
the appropriate paper(s):

Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam.
ggtree: an R package for visualization and annotation of phylogenetic
trees with their covariates and other associated data. Methods in
Ecology and Evolution. 2017, 8(1):28-36. doi:10.1111/2041-210X.12628

Guangchuang Yu. Using ggtree to visualize data on tree-like structures.
Current Protocols in Bioinformatics. 2020, 69:e96. doi:10.1002/cpbi.96

S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L
Zhan, T Wu, E Hu, Y Jiang, X Bo, G Yu. ggtreeExtra: Compact
visualization of richly annotated phylogenetic data. Molecular Biology
and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166
> text = "((((((t24:0.8024311261,t11:0.7828436729):0.3048173019,(t21:0.4867131179,t18:0.2167164627):0.7519672168):0.5776117099,t5:0.4223263576):0.5963104749,(t17:0.1558260066,t20:0.41109852):0.09447153704):0.2661841849,((((t6:0.009324073093,t12:0.2732205035):0.7790091021,t10:0.08588226303):0.3282297731,t9:0.2075715209):0.664191803,(((t15:0.5832811284,t14:0.8461383074):0.6081165755,t19:0.5950602938):0.7095833826,t8:0.7146228608):0.7801561591):0.6674923887):0.654328516,(((t13:0.6356930537,t3:0.8536336934):0.8644152461,t2:0.1784738901):0.7129137593,t23:0.8907998055):0.3618239218,((t16:0.1825823467,t7:0.8856151809):0.4720220205,(t22:0.672613536,(t1:0.9215354125,(t4:0.9248593273,t25:0.5937075356):0.3007316259):0.6941311779):0.6789765966):0.2112918347);"
> tr <- read.tree(text=text)
> tr

Phylogenetic tree with 25 tips and 23 internal nodes.

Tip labels:
  t24, t11, t21, t18, t5, t17, ...

Unrooted; includes branch lengths.
> group.roots <- c(34, 28, 44, 41)
> group.colors <- c("#fd00fe",  "#62ce75",  "#9a1073", "#4ad9e1")
> g <- data.frame(gnode = group.roots, gfill = group.colors)
> ggtree(tr,layout = 'unrooted') -> p1
"daylight" method was used as default layout for unrooted tree.
Average angle change [1] 0.145156417070165
Average angle change [2] 0.0537331668735638
Average angle change [3] 0.0242388100277642
> p1 + geom_hilight(data = g, mapping=aes(node =  gnode, fill = I(gfill)), to.bottom=T, alpha=1)

xx