Handling x axis in facet_plot

384 views
Skip to first unread message

Hovhannes Sahakyan

unread,
Oct 7, 2020, 11:07:08 AM10/7/20
to ggtree
Hi

I draw a facet plot - a tree combined to the frequency information for tip branches.

I would like to change x axis labels so that 1000 will be 1,000. If I use scale_x_continuous with breaks and labels in the tree section labels emerge, which are equal to the labels of the frequencies' section.

How is it possible to handle x axis so to use comma in thousands?

The reproducible example is below.

Best,
Hovhannes

tdf1
South East West Northwest Southeast Sample_ID
20   19   20        20        21        t1
22   21   19        20        18        t2
20   20   19        21        20        t3
19   19   21        21        20        t4

tdf1_melted <- melt(tdf1, id="Sample_ID")

test_tree <- rtree(4)
test_tree$edge.length <- test_tree$edge.length*10000
test_tree <- ggtree(test_tree) + geom_tiplab(size=8)  + theme_tree2()
test_tree <- revts(test_tree) +
  scale_x_continuous(breaks = c(-15000,-10000,-5000,0,25,50,75,100), labels = c("15000","10000","5000","0","25","50","75","100"))

facet_plot(test_tree, panel = 'Stacked Barplot', data = tdf1_melted, geom = geom_barh, 
           mapping = aes(x = value, fill = as.factor(variable)), size=10, 
           width = 1.0, stat='identity')

Hovhannes Sahakyan

unread,
Oct 7, 2020, 11:16:44 AM10/7/20
to ggtree
Forgot to mention that the melt() function is from reshape2 library.

Yu, Guangchuang

unread,
Oct 10, 2020, 11:03:17 AM10/10/20
to Hovhannes Sahakyan, ggtree

In most cases, this is very simple. Just +scale_x_continuous(labels = comma) should work.

In your case, as the labels and breaks are different numbers, you can just add commas in labels.


image.png




--
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/038e1ec3-8c1f-427c-966e-ecdcd086f1b7n%40googlegroups.com.


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

Hovhannes Sahakyan

unread,
Oct 10, 2020, 11:22:37 AM10/10/20
to ggtree
hello,

adding commas in   scale_x_continuous(labels=) is OK, I have done it, but the problem is in the addition of redundant labels - meant to be in the "Stacked barplot" part -  in the tree part. I marked the labels with the green square in the figure.

best,
Hovhannesgreen_square.png

Yu, Guangchuang

unread,
Oct 10, 2020, 10:41:46 PM10/10/20
to Hovhannes Sahakyan, ggtree
set.seed(2020-10-11)
test_tree <- rtree(4)
test_tree$edge.length <- test_tree$edge.length*10000

test_tree <- ggtree(test_tree) + geom_tiplab(size=8
)  + theme_tree2()
test_tree <- revts(test_tree) +
  scale_x_continuous(labels=function(x) scales::comma(abs(x))) # <-- what you need is actually a function.

facet_plot(test_tree, panel = 'Stacked Barplot', data = tdf1_melted, geom = geom_barh, 
           mapping = aes(x = value, fill = as.factor(variable)), size=10, 
           width = 1.0, stat='identity')

image.png

Hovhannes Sahakyan

unread,
Oct 13, 2020, 7:16:23 AM10/13/20
to ggtree
Hi,

Thank you very much for the solution. 

Best,
Hovhannes
Reply all
Reply to author
Forward
0 new messages