YuLab-SMU / ggtreeExtra

:lemon: Add Geom Layers On Circular Or Other Layout Tree Of “ggtree”
https://doi.org/10.1093/molbev/msab166
GNU General Public License v3.0
86 stars 15 forks source link

Adding and adjusting bar height based on abundance #21

Closed pdhrati02 closed 2 years ago

pdhrati02 commented 2 years ago
tree_part

Hi all, I am trying to add a layer of abundance on my phylogenetic tree. I made this tree using ggtree. Abundance column in my metadata file is a continuous variable. Is it possible to add bars/histogram to the tree with each bar height varying based on the abundance column?

The code I am using to make the tree is as follows: (here dd is my metadata file which has user_genome has a column which is all the MAGs and abundance as a column which gives abundance of the gene of interest in each MAG)

p1 <- gheatmap(x, ee, width=0.08, offset=0.03, color = NA, colnames = F)+ scale_fill_viridis_d(option="D", name="Phylum", na.translate = FALSE) + theme(legend.title=element_text(size=24), legend.text=element_text(size=22)) p1

p10 <- p1 + new_scale_fill() + geom_fruit(data=dd, geom=geom_tile, mapping=aes(y=user_genome, fill=abundance), offset = 1.1, pwidth=0.1,

orientation="x",

              stat="identity")

p10 p10 gives me the blue lines/too thin tiles as shown in the plot (see attached), however I wish to obtain bars instead as shown in red. Can anyone suggest a way to do this? Note: There is no variable based on which I wish to fill/color these bars, single color bars with differing height is all I am trying to obtain.

Any help would be greatly appreciated. Thank you in advance DP

xiangpin commented 2 years ago

The geom_tile is a layer to build a heatmap-like layer, if you want to display the abundance using a bar plot, you might need to use geom_col. You can refer to the following codes.

> library(ggtree)
ggtree v3.4.0 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, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods
for mapping and visualizing associated data on phylogeny using ggtree.
Molecular Biology and Evolution. 2018, 35(12):3041-3043.
doi:10.1093/molbev/msy194

G Yu. Data Integration, Manipulation and Visualization of Phylogenetic
Trees (1st ed.). Chapman and Hall/CRC. 2022. ISBN: 9781032233574
> library(ggtreeExtra)
ggtreeExtra v1.6.0 For help:
https://yulab-smu.top/treedata-book/

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

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
> library(ggplot2)
> set.seed(1024)
> tr <- rtree(100)
> dd = data.frame(id=tr$tip.label, value=abs(rnorm(100)))
> dt = data.frame(id=tr$tip.label, group=c(rep("A",50),rep("B",50)))
> p <- ggtree(tr, layout='circular')
> p1 <- p + geom_fruit(data=dt, geom=geom_tile, mapping=aes(y=id, fill=group), width=.5, offset=.1)
> p2 <- p1 + geom_fruit(data=dd, geom=geom_col, mapping=aes(x=value, y=id), orientation='y', offset=.05, axis.params=list(axis='x', text.size = 1.5, nbreak=2), grid.params=list())
> p2

xx

pdhrati02 commented 2 years ago

Hi @xiangpin Thank you very much for your quick response, it worked well. Really grateful to you for your help. Best DP

pdhrati02 commented 2 years ago

HI @xiangpin, My apologies that I am re-opening this issue. The code you gave worked very well. However I just have one question. How would one add legend for such bars? These are varying just by height. So it would mean adding a single sentence to the legends, but I am unable to add legend using legend or other functions. They either over-write the legends I already have or don't work at all. Would you have any suggestions for the same?

Thank you once again Best DP

xiangpin commented 2 years ago

The legend can be added automatically if you specify a variable name for fill character of geom_col. But if fill had been used in other layers. you can use new_scale('fill') of ggnewscale.

library(ggtree)
library(ggtreeExtra)
library(ggplot2)
set.seed(1024)
tr <- rtree(100)
dd = data.frame(id=tr$tip.label, value=abs(rnorm(100)))
dt = data.frame(id=tr$tip.label, group=c(rep("A",50),rep("B",50)))
p <- ggtree(tr, layout = 'circular')
p1 <- p + geom_fruit(data=dt, geom=geom_tile, mapping=aes(y=id, fill=group), width=.5, offset=.1)
p2 <- p1 + geom_fruit(data=dd, geom=geom_col, mapping=aes(x=value, y=id, fill='test'), orientation='y', offset=.05, axis.params=list(axis='x', text.size = 1.5, nbreak=2), grid.params=list())
p2

xx

ddt <- dd %>% dplyr::left_join(dt, by='id')
p3 <- p1 + ggnewscale::new_scale("fill") + geom_fruit(data=ddt, geom=geom_col, mapping=aes(x=value, y=id, fill=group), orientation='y', offset=.05, axis.params=list(axis='x', text.size = 1.5, nbreak=2), grid.p
arams=list())
p3

xx1

pdhrati02 commented 2 years ago

Thank you very much, I will ty this. Many thanks.