Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problems with facet_plot #120

Closed
pedres opened this issue Mar 17, 2017 · 7 comments
Closed

problems with facet_plot #120

pedres opened this issue Mar 17, 2017 · 7 comments

Comments

@pedres
Copy link

pedres commented Mar 17, 2017

I have the most recent version of ggtree (1.6.11), but I still have problems to reproduce the output of example from facet_plot for stacked barplots. This is what I get, which is strange, if I type the code from http://guangchuangyu.github.io/2016/10/facet_plot-a-general-solution-to-associate-data-with-phylogenetic-tree/

example

I am trying to join a tree (a hc object formatted as ggtree object) with an stacked barplot of abundances. My tree has 30 tips, and my dataset to stack as 180 rows, each tip has 6 values,like

 id          filos abundancias

1 AB10C Actinobacteria 2.228632578
2 AB10C Bacteroidetes 3.730753128
3 AB10C Firmicutes 3.904868286
4 AB10C Proteobacteria 3.734883721
5 AB10C Verrucomicrobia 4.807334326
6 AB10C lowabundance 3.043275942

When I type the code below I obtain the example2, in which appears the tree but not the barplot
arbolbarplot<-facet_plot(arbolbien, panel = 'Stacked Barplot', data = meltedfilosXXX, geom = geom_bar, mapping = aes(x = abundancias, fill = as.factor(filos)), stat='identity' )

example2

The barplot I would like to join to the tree is
ggplot(meltedfilosXXX, aes(x=id, y=abundancias, fill=filos)) + geom_bar(stat="identity") + coord_flip() + ylab("Relative abundance")

example3

Thank you very much for your help

Below I paste R session info

R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252 LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C LC_TIME=Spanish_Spain.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets methods base

other attached packages:
[1] car_2.1-4 phangorn_2.1.1 data.table_1.10.4 multcomp_1.4-6 TH.data_1.0-8 MASS_7.3-45 survival_2.40-1 mvtnorm_1.0-6 nortest_1.0-4 picante_1.6-2 ape_4.1
[12] nlme_3.1-131 vegan_2.4-2 lattice_0.20-34 permute_0.9-4 plyr_1.8.4 phyloseq_1.14.0 tidyr_0.6.1 gridExtra_2.2.1 betapart_1.4-1 reshape2_1.4.2 cowplot_0.7.0
[23] ggtree_1.6.11 ggplot2_2.2.1 phylobase_0.8.2

loaded via a namespace (and not attached):
[1] minqa_1.2.4 colorspace_1.3-2 htmlTable_1.9 futile.logger_1.4.3 XVector_0.10.0 RcppArmadillo_0.7.700.0.0 GenomicRanges_1.22.4
[8] base64enc_0.1-3 MatrixModels_0.4-1 AnnotationDbi_1.32.3 xml2_1.1.1 codetools_0.2-15 splines_3.3.3 bold_0.4.0
[15] geneplotter_1.48.0 knitr_1.15.1 ade4_1.7-5 Formula_1.2-1 jsonlite_1.3 nloptr_1.0.4 pbkrtest_0.4-7
[22] annotate_1.48.0 cluster_2.0.5 httr_1.2.1 backports_1.0.5 assertthat_0.1 Matrix_1.2-8 lazyeval_0.2.0
[29] acepack_1.4.1 htmltools_0.3.5 quantreg_5.29 prettyunits_1.0.2 tools_3.3.3 igraph_1.0.1 gtable_0.2.0
[36] taxize_0.8.4 dplyr_0.5.0 fastmatch_1.1-0 Rcpp_0.12.9 Biobase_2.30.0 Biostrings_2.38.4 RJSONIO_1.3-0
[43] multtest_2.26.0 biom_0.3.12 iterators_1.0.8 stringr_1.2.0 lme4_1.1-12 XML_3.98-1.5 zlibbioc_1.16.0
[50] zoo_1.7-14 scales_0.4.1 rcdd_1.1-11 parallel_3.3.3 SummarizedExperiment_1.0.2 sandwich_2.3-4 SparseM_1.76
[57] lambda.r_1.1.9 RColorBrewer_1.1-2 memoise_1.0.0 rpart_4.1-10 reshape_0.8.6 latticeExtra_0.6-28 stringi_1.1.2
[64] RSQLite_1.1-2 genefilter_1.52.1 S4Vectors_0.8.11 foreach_1.4.3 checkmate_1.8.2 BiocGenerics_0.16.1 BiocParallel_1.4.3
[71] geometry_0.3-6 GenomeInfoDb_1.6.3 rncl_0.8.2 labeling_0.3 htmlwidgets_0.8 magrittr_1.5 DESeq2_1.10.1
[78] R6_2.2.0 IRanges_2.4.8 Hmisc_4.0-2 DBI_0.6 foreign_0.8-67 mgcv_1.8-17 nnet_7.3-12
[85] tibble_1.2 futile.options_1.0.0 uuid_0.1-2 progress_1.1.2 RNeXML_2.0.7 locfit_1.5-9.1 digest_0.6.12
[92] xtable_1.8-2 stats4_3.3.3 munsell_0.4.3 magic_1.5-6 quadprog_1.5-5

@GuangchuangYu
Copy link
Member

  1. if you really follow https://guangchuangyu.github.io/2016/10/facet_plot-a-general-solution-to-associate-data-with-phylogenetic-tree/ you will find that you need to use geom_barh instead of geom_bar.

  2. don't ignore my message to create a reproducible example, otherwise I don't know how to help.

@pedres
Copy link
Author

pedres commented Mar 17, 2017 via email

@AEgit
Copy link

AEgit commented Mar 17, 2017

Version: ggtree 1.6.11

I can confirm the issues reported by @pedres. When using the example code provided on https://guangchuangyu.github.io/2016/10/facet_plot-a-general-solution-to-associate-data-with-phylogenetic-tree/ you get an error message:

library(ggtree)

tr <- rtree(30)
p <- ggtree(tr)
d1 <- data.frame(id=tr$tip.label, location=sample(c("GZ", "HK", "CZ"), 30, replace=TRUE))
p1 <- p %<+% d1 + geom_tippoint(aes(color=location))
d2 <- data.frame(id=tr$tip.label, val=rnorm(30, sd=3))
p2 <- facet_plot(p1, panel="dot", data=d2, geom=geom_point, 
                 aes(x=val), color='firebrick') + theme_tree2()

d3 <- data.frame(id = rep(tr$tip.label, each=2),
                 value = abs(rnorm(60, mean=100, sd=50)),
                 category = rep(LETTERS[1:2], 30))
p3 <- facet_plot(p2, panel = 'Stacked Barplot', data = d3, 
                 geom = geom_barh, 
                 mapping = aes(x = value, fill = as.factor(category)), 
                 stat='identity' ) 

The following error message is displayed:

Error in facet_plot(p2, panel = "Stacked Barplot", data = d3, geom = geom_barh,  : 
  object 'geom_barh' not found

If we use geom_bar instead, no error message is displayed, but the plot looks as reported by @pedres.

@GuangchuangYu
Copy link
Member

GuangchuangYu commented Mar 19, 2017

an excerpt from the post http://guangchuangyu.github.io/2016/10/facet_plot-a-general-solution-to-associate-data-with-phylogenetic-tree/

Most of the geom in ggplot2 draw vertical graph object, while for associating graph object with phylogenetic tree, we need horizontal versions. Luckily, we have ggstance which provides horizontal versions of geoms, including:

  • geom_barh()
  • geom_histogramh()
  • geom_linerangeh()
  • geom_pointrangeh()
  • geom_errorbarh()
  • geom_crossbarh()
  • geom_boxploth()
  • geom_violinh()

With ggstance, we can associate barplot, boxplot or other graphs to phylogenetic trees.

Did both of you really read the post? AND you also ignore my guidance of reporting an issue.

@AEgit
Copy link

AEgit commented Mar 19, 2017

Ok, my fault - indeed, if the library "ggstance" is loaded, the posted coded runs smoothly. So here again the code that should run (directly taken from http://guangchuangyu.github.io/2016/10/facet_plot-a-general-solution-to-associate-data-with-phylogenetic-tree/):


library(ggtree)
library(ggstance) #Without this library the code won't work

tr <- rtree(30)
p <- ggtree(tr)
d1 <- data.frame(id=tr$tip.label, location=sample(c("GZ", "HK", "CZ"), 30, replace=TRUE))
p1 <- p %<+% d1 + geom_tippoint(aes(color=location))
d2 <- data.frame(id=tr$tip.label, val=rnorm(30, sd=3))
p2 <- facet_plot(p1, panel="dot", data=d2, geom=geom_point, 
                 aes(x=val), color='firebrick') + theme_tree2()

d3 <- data.frame(id = rep(tr$tip.label, each=2),
                 value = abs(rnorm(60, mean=100, sd=50)),
                 category = rep(LETTERS[1:2], 30))
p3 <- facet_plot(p2, panel = 'Stacked Barplot', data = d3, 
                 geom = geom_barh, 
                 mapping = aes(x = value, fill = as.factor(category)), 
                 stat='identity' ) 
p3

Apologies for any inconvenience.

@pedres
Copy link
Author

pedres commented Mar 20, 2017 via email

@pedres
Copy link
Author

pedres commented Mar 20, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants