Tree with barplot

588 views
Skip to first unread message

o.willi...@gmail.com

unread,
Sep 1, 2020, 2:10:58 PM9/1/20
to ggtree
Hello, 

I have been following the manual in order to plot tree with a bar graph alongside.

I can use the facet plot method but ideally I would like to change the relative widths of the plot. 

So I am using the method with aplot outlined here http://yulab-smu.top/treedata-book/chapter7.html in the section for composite plots.

Is there a method to plot only the tree and the barplot side by side, without the heatmap in the middle? I have attached an image of what I am aiming for.

I found a workaround, by adding a blank dummy plot in between. See the code below. However, I was wondering if there was a simpler way of doing this? 

library(ggplot2)
library(ggtree)
library(cowplot)
library(aplot)

set.seed(2019-10-31)
tr <- rtree(10)

# dataset 1
d1 <- data.frame(
  # only some labels match
  label = c(tr$tip.label[sample(5, 5)], "A"),
  value = sample(1:6, 6))

# dataset 2
d2 <- data.frame(
  label = rep(tr$tip.label, 5),
  category = rep(LETTERS[1:5], each=10),
  value = rnorm(50, 0, 3)) 

# tree
g <- ggtree(tr) + geom_tiplab(align=TRUE)

# plot 1
p1 <- ggplot(d1, aes(label, value)) + geom_col(aes(fill=label)) + 
  geom_text(aes(label=label, y= value+.1)) +
  coord_flip() + theme_tree2() + theme(legend.position='none')

# plot 2
p2 <- ggplot(d2, aes(x=category, y=label)) + 
  geom_tile(aes(fill=value)) + scale_fill_viridis_c() + 
  theme_tree2() 

# plot side by side
p2 %>% insert_left(g) %>% insert_right(p1, width=.5) 

# nether work as intended 
g %>% insert_right(p1)
p1 %>% insert_left(g)

# create dummy data to sit in between
d2_dummy <- data.frame(label = tr$tip.label,
                       category = LETTERS[1:5]) 

# dummy plot
p2_dummy <- ggplot(data = d2, aes(x=category, y=label)) + theme_void()

# plot side by side with dummy plot in between
p2_dummy %>% insert_left(g, width = 10) %>% insert_right(p1, width=5) 

Best wishes
Ollie


 example.png




Yu, Guangchuang

unread,
Sep 1, 2020, 9:17:58 PM9/1/20
to o.willi...@gmail.com, ggtree
nice hack.

IMO, facet_plot is very better for this case. If you wan to adjust facet width, there is a facet_widths() function provided in ggtree and was presented in the following paper:

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


--
1. G Yu*. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics. 2020, 69:e96. https://doi.org/10.1002/cpbi.96
2. LG Wang, TTY Lam, S Xu, Z Dai, L Zhou, T Feng, P Guo, CW Dunn, BR Jones, T Bradley, H Zhu, Y Guan, Y Jiang, G Yu*. treeio: an R package for phylogenetic tree input and output with richly annotated and associated data. Molecular Biology and Evolution. 2020, 37(2):599-603. http://dx.doi.org/10.1093/molbev/msz240
3. G Yu*, TTY Lam, H Zhu, Y Guan*. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 2018, 35(2):3041-3043. https://doi.org/10.1093/molbev/msy194
4. G Yu, DK Smith, H Zhu, Y Guan, TTY 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. https://doi.org/10.1111/2041-210X.12628
5. Book: https://yulab-smu.top/treedata-book/
---
You received this message because you are subscribed to the Google Groups "ggtree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/5941902b-2544-4a9f-8b79-8fee765ca05dn%40googlegroups.com.


--
--~--~---------~--~----~------------~-------~--~----~
Guangchuang Yu PhD
Professor, Associate Director
Department of Bioinformatics
School of Basic Medical Sciences
Southern Medical University
Guangzhou, China
-~----------~----~----~----~------~----~------~--~---

o.willi...@gmail.com

unread,
Sep 2, 2020, 1:14:06 PM9/2/20
to ggtree
Hello, 

Thank very much for pointing out the facet_widths function, it is just what I needed, and apologies I hadn't spotted it beforehand

Best wishes
Ollie
Reply all
Reply to author
Forward
0 new messages