--- date: 2017-07-24T00:11:02+01:00 title: FAQ weight: 20 --- <link rel="stylesheet" href="https://guangchuangyu.github.io/css/font-awesome.min.css"> # <i class="fa fa-download"></i> Installation ## <i class="fa fa-angle-double-right"></i> Could not find function If you got [this error](https://github.com/GuangchuangYu/ggtree/issues/12), please make sure you are using the latest R and `ggtree`. Packages in Bioconductor, like `ggtree`, have different release policy compare to CRAN. There are two branches, release and devel, in parallel. Release branch is more stable and only document improvement and bug fixes will commit to it. New functions will only commit to `devel` branch. Sometimes I may write blog post to introduce new functions which is not available in `release` branch, you need to install the `devel` version of `ggtree` in order to use these new functions. You can download the `devel` version of `ggtree` from [http://bioconductor.org/packages/devel/bioc/html/ggtree.html](http://bioconductor.org/packages/devel/bioc/html/ggtree.html) and install it, or install the github version of `ggtree`. This also applied to other of my packages, including `GOSemSim`, `DOSE`, `clusterProfiler`, `ReactomePA` and `ChIPseeker`. If you got the `could not find function` error, upgrade your installation to latest release. If the error still exists after upgrading to latest release, you need to install the devel version. # <i class="fa fa-text-height"></i> Text & Label ## <i class="fa fa-angle-double-right"></i> Tip label truncated ggplot2 can't auto adjust xlim based on added text. ```r library(ggtree) ## example tree from https://support.bioconductor.org/p/72398/ tree<-read.tree(text="(Organism1.006G249400.1:0.03977,(Organism2.022118m:0.01337,(Organism3.J34265.1:0.00284,Organism4.G02633.1:0.00468)0.51:0.0104):0.02469);") ggtree(tree) + geom_tiplab() ``` This is because the units are in two different spaces (data and pixel). Users can use xlim to allocate more space for tip label. ```r ggtree(tree) + geom_tiplab() + xlim(0, 0.06) ``` ## <i class="fa fa-angle-double-right"></i> Modify (tip) labels This could be easily done via the `%<+%` operator to attach the modified version of the labels and than use `geom_tiplab` to display the modified version. ``` raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="ggtree") raxml <- read.raxml(raxml_file) lb = get.tree(raxml)$tip.label d = data.frame(label=lb, label2 = paste("AA", substring(lb, 1, 5))) ggtree(raxml) %<+% d + geom_tiplab(aes(label=label2)) ``` see also [1](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/tFdFgCJ7gQA/tZ6phSgUDQAJ) and [2](https://github.com/GuangchuangYu/ggtree/issues/106). ## <i class="fa fa-angle-double-right"></i> Formatting (tip) labels If you want to format labels, you need to set `parse=TRUE` in `geom_text`/`geom_tiplab` and the `label` should be string that can be parsed into expression and displayed as described in `?plotmath`. For example, the tiplabels contains two parts, species name and accession number and we want to display species name in _italic_, we can use command like this: ``` ggtree(rtree(30)) + geom_tiplab(aes(subset=node==35), label='paste(italic("species name"), "accession number")', parse=T) ``` Another example for formating all tip labels: ``` ggtree(rtree(30)) + geom_tiplab(aes(label=paste0('bold(', label, ')~italic(', node, ')')), parse=TRUE) ``` see also <https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/BA7g-npY1BM>. ## <i class="fa fa-angle-double-right"></i> Avoid overlapping text labels User can use [ggrepel](https://cran.r-project.org/web/packages/ggrepel/) package to repel overlapping text labels. For example: ```r library(ggrepel) library(ggtree) raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="ggtree") raxml <- read.raxml(raxml_file) ggtree(raxml) + geom_label_repel(aes(label=bootstrap, fill=bootstrap)) ``` For details, please refer to [ggrepel usage examples](https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html). ## <i class="fa fa-angle-double-right"></i> bootstrap values from newick format It's quite command to store `bootstrap` value as node label in `newick` format. Visualizing node label is easy using `geom_text2(aes(subset = !isTip, label=label))`. If you want to only display a subset of `bootstrap` (e.g. bootstrap > 80), you can't simply using `geom_text2(subset= (label > 80), label=label)` since `label` is a character vector, which contains node label (bootstrap value) and tip label (taxa name). If we use `geom_text2(subset=(as.numeric(label) > 80), label=label)`, it will also fail since `NAs` were introduced by coercion. We need to convert `NAs` to logical `FALSE`, this can be done by the following code: ```r nwk <- system.file("extdata/RAxML","RAxML_bipartitions.H3", package='ggtree') tr <- read.tree(nwk) ggtree(tr) + geom_text2(aes(label=label, subset = !is.na(as.numeric(label)) & as.numeric(label) > 80)) ``` Another solution is converting the bootstrap value outside `ggtree` as I recommended in [google group](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/hiRBjGaAfI0/Y-2zDpvtAwAJ). ```r q <- ggtree(tr) d <- q$data d <- d[!d$isTip,] d$label <- as.numeric(d$label) d <- d[d$label > 80,] q + geom_text(data=d, aes(label=label)) ``` # <i class="fa fa-map-marker"></i> _aesthetic_ mapping ## <i class="fa fa-angle-double-right"></i> inherit _aes_ ```r ggtree(rtree(30)) + geom_point() ``` For example, we can add symbolic points to nodes with `geom_point()` directly. The magic here is we don't need to map `x` and `y` position of the points by providing `aes(x, y)` to `geom_point()` since it was already mapped by `ggtree` function and it serves as a global mapping for all layers. But what if we provide a `dataset` in a layer and the `dataset` doesn't contain column of `x` and/or `y`, the layer function also try to map `x` and `y` and also others if you map them in `ggtree` function. As these variable is not available in your `dataset`, you will get the following error: ``` Error in eval(expr, envir, enclos) : object 'x' not found ``` This can be fixed by using parameter `inherit.aes=FALSE` which will disable inheriting mapping from `ggtree` function. ## <i class="fa fa-angle-double-right"></i> use `$` in aes NEVER DO THIS. see the explaination in the [ggplot2 book 2ed](https://github.com/hadley/ggplot2-book/blob/master/layers.rmd#aesthetic-mappings-secaes): >Never refer to a variable with `$` (e.g., `diamonds$carat`) in `aes()`. This breaks containment, so that the plot no longer contains everything it needs, and causes problems if ggplot2 changes the order of the rows, as it does when facetting. see also [1](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/hViM6vRZF94/MsZT8qRgBwAJ) and [2](https://github.com/GuangchuangYu/ggtree/issues/106). # <i class="fa fa-tree"></i> Annotation ## <i class="fa fa-angle-double-right"></i> colouring edges by user data see my blog post: [Edge coloring with user data](http://guangchuangyu.github.io/2016/12/edge-coloring-with-user-data/) and also my answer on <https://github.com/GuangchuangYu/ggtree/issues/76> and <https://groups.google.com/forum/#!topic/bioc-ggtree/4GgivKqVjB8>.