<!DOCTYPE html> <html class="no-js"> <head lang="en-us"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1"> <meta http-equiv="X-UA-Compatible" content="IE=10" /> <title>FAQ - ggtree</title> <meta name="generator" content="Hugo 0.24.1" /> <meta name="description" content="Visualization and annotation of phylogenetic trees."> <link rel="canonical" href="https://guangchuangyu.github.io/ggtree/faq/"> <meta name="author" content="Guangchuang Yu"> <meta property="og:url" content="https://guangchuangyu.github.io/ggtree/faq/"> <meta property="og:title" content="ggtree"> <meta property="og:image" content="https://guangchuangyu.github.io/ggtree/images/gc.png"> <meta name="apple-mobile-web-app-title" content="ggtree"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <link rel="shortcut icon" type="image/x-icon" href="https://guangchuangyu.github.io/ggtree/images/favicon.ico"> <link rel="icon" type="image/x-icon" href="https://guangchuangyu.github.io/ggtree/images/favicon.ico"> <style> @font-face { font-family: 'Icon'; src: url('https://guangchuangyu.github.io/ggtree/fonts/icon.eot'); src: url('https://guangchuangyu.github.io/ggtree/fonts/icon.eot') format('embedded-opentype'), url('https://guangchuangyu.github.io/ggtree/fonts/icon.woff') format('woff'), url('https://guangchuangyu.github.io/ggtree/fonts/icon.ttf') format('truetype'), url('https://guangchuangyu.github.io/ggtree/fonts/icon.svg') format('svg'); font-weight: normal; font-style: normal; } </style> <link rel="stylesheet" href="https://guangchuangyu.github.io/ggtree/stylesheets/application.css"> <link rel="stylesheet" href="https://guangchuangyu.github.io/ggtree/stylesheets/temporary.css"> <link rel="stylesheet" href="https://guangchuangyu.github.io/ggtree/stylesheets/palettes.css"> <link rel="stylesheet" href="https://guangchuangyu.github.io/ggtree/stylesheets/highlight/highlight.css"> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Ubuntu:400,700|Ubuntu+Mono"> <style> body, input { font-family: 'Ubuntu', Helvetica, Arial, sans-serif; } pre, code { font-family: 'Ubuntu Mono', 'Courier New', 'Courier', monospace; } </style> <script src="https://guangchuangyu.github.io/ggtree/javascripts/modernizr.js"></script> </head> <body class="palette-primary-teal palette-accent-teal"> <div class="backdrop"> <div class="backdrop-paper"></div> </div> <input class="toggle" type="checkbox" id="toggle-drawer"> <input class="toggle" type="checkbox" id="toggle-search"> <label class="toggle-button overlay" for="toggle-drawer"></label> <header class="header"> <nav aria-label="Header"> <div class="bar default"> <div class="button button-menu" role="button" aria-label="Menu"> <label class="toggle-button icon icon-menu" for="toggle-drawer"> <span></span> </label> </div> <div class="stretch"> <div class="title"> FAQ </div> </div> <div class="button button-twitter" role="button" aria-label="Twitter"> <a href="https://twitter.com/guangchuangyu" title="@guangchuangyu on Twitter" target="_blank" class="toggle-button icon icon-twitter"></a> </div> <div class="button button-github" role="button" aria-label="GitHub"> <a href="https://github.com/GuangchuangYu" title="@GuangchuangYu on GitHub" target="_blank" class="toggle-button icon icon-github"></a> </div> </div> <div class="bar search"> <div class="button button-close" role="button" aria-label="Close"> <label class="toggle-button icon icon-back" for="toggle-search"></label> </div> <div class="stretch"> <div class="field"> <input class="query" type="text" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck> </div> </div> <div class="button button-reset" role="button" aria-label="Search"> <button class="toggle-button icon icon-close" id="reset-search"></button> </div> </div> </nav> </header> <main class="main"> <div class="drawer"> <nav aria-label="Navigation"> <a href="https://github.com/GuangchuangYu/ggtree" class="project"> <div class="banner"> <div class="logo"> <img src="https://guangchuangyu.github.io/ggtree/images/gc.png"> </div> <div class="name"> <strong>ggtree </strong> <br> GuangchuangYu/ggtree </div> </div> </a> <div class="scrollable"> <div class="wrapper"> <ul class="repo"> <li class="repo-download"> <a href="https://github.com/GuangchuangYu/ggtree/archive/master.zip" target="_blank" title="Download" data-action="download"> <i class="icon icon-download"></i> Download </a> </li> <li class="repo-stars"> <a href="https://github.com/GuangchuangYu/ggtree/stargazers" target="_blank" title="Stargazers" data-action="star"> <i class="icon icon-star"></i> Stars <span class="count">–</span> </a> </li> </ul> <hr> <div class="toc"> <ul> <li> <a title="ggtree" href="../"> ggtree </a> </li> <li> <a title="Documentation" href="../documentation/"> Documentation </a> </li> <li> <a class="current" title="FAQ" href="../faq/"> FAQ </a> <ul id="scrollspy"> </ul> </li> <li> <a title="Featured Articles" href="../featured-articles/"> Featured Articles </a> </li> <li> <a title="Gallery" href="../gallery/"> Gallery </a> </li> <li> <a title="Tweets" href="../tweets/"> Tweets </a> </li> </ul> <hr> <span class="section">The author</span> <ul> <li> <a href="https://twitter.com/guangchuangyu" target="_blank" title="@guangchuangyu on Twitter"> @guangchuangyu on Twitter </a> </li> <li> <a href="https://github.com/GuangchuangYu" target="_blank" title="@GuangchuangYu on GitHub"> @GuangchuangYu on GitHub </a> </li> <li> <a href="https://guangchuangyu.github.io/blog_images/biobabble.jpg" title="关注公众号: biobabble"> @biobabble on WeChat </a> </li> <li> <a href="mailto:guangchuangyu@gmail.com" title="Email of guangchuangyu@gmail.com"> Contact via email </a> </li> </ul> <center><span class="zaln"><a href="https://mp.weixin.qq.com/s?__biz=MzI5NjUyNzkxMg==&mid=2247484170&idx=1&sn=ce5f7eb6df66d79661276a64bf25b4bc&chksm=ec43b24ddb343b5b2877574153aeccd049a6824716063883edb35c2338bd32c067caf3885f53#rd" class="zb">赞赏</a></span></center> <div style="width:250px;margin:auto;"><script type="text/javascript" src="//rf.revolvermaps.com/0/0/7.js?i=5qpvv5zz7fp&m=0&c=ff0000&cr1=ffffff&sx=0" async="async"></script></div> </div> </div> </div> </nav> </div> <article class="article"> <div class="wrapper"> <h1>FAQ </h1> <p><link rel="stylesheet" href="https://guangchuangyu.github.io/css/font-awesome.min.css"></p> <div id="installation" class="section level1"> <h1><i class="fa fa-download"></i> Installation</h1> <div id="could-not-find-function" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> Could not find function</h2> <p>If you got <a href="https://github.com/GuangchuangYu/ggtree/issues/12">this error</a>, please make sure you are using the latest R and <code>ggtree</code>.</p> <p>Packages in Bioconductor, like <code>ggtree</code>, 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 <code>devel</code> branch.</p> <p>Sometimes I may write blog post to introduce new functions which is not available in <code>release</code> branch, you need to install the <code>devel</code> version of <code>ggtree</code> in order to use these new functions.</p> <p>You can download the <code>devel</code> version of <code>ggtree</code> from <a href="http://bioconductor.org/packages/devel/bioc/html/ggtree.html" class="uri">http://bioconductor.org/packages/devel/bioc/html/ggtree.html</a> and install it, or install the github version of <code>ggtree</code>.</p> <p>This also applied to other of my packages, including <code>GOSemSim</code>, <code>DOSE</code>, <code>clusterProfiler</code>, <code>ReactomePA</code> and <code>ChIPseeker</code>. If you got the <code>could not find function</code> error, upgrade your installation to latest release. If the error still exists after upgrading to latest release, you need to install the devel version.</p> </div> </div> <div id="text-label" class="section level1"> <h1><i class="fa fa-text-height"></i> Text & Label</h1> <div id="tip-label-truncated" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> Tip label truncated</h2> <p>ggplot2 can’t auto adjust xlim based on added text.</p> <pre class="r"><code>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()</code></pre> <p>This is because the units are in two different spaces (data and pixel). Users can use xlim to allocate more space for tip label.</p> <pre class="r"><code>ggtree(tree) + geom_tiplab() + xlim(0, 0.06)</code></pre> </div> <div id="modify-tip-labels" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> Modify (tip) labels</h2> <p>This could be easily done via the <code>%<+%</code> operator to attach the modified version of the labels and than use <code>geom_tiplab</code> to display the modified version.</p> <pre><code>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))</code></pre> <p>see also <a href="https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/tFdFgCJ7gQA/tZ6phSgUDQAJ">1</a> and <a href="https://github.com/GuangchuangYu/ggtree/issues/106">2</a>.</p> </div> <div id="formatting-tip-labels" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> Formatting (tip) labels</h2> <p>If you want to format labels, you need to set <code>parse=TRUE</code> in <code>geom_text</code>/<code>geom_tiplab</code> and the <code>label</code> should be string that can be parsed into expression and displayed as described in <code>?plotmath</code>.</p> <p>For example, the tiplabels contains two parts, species name and accession number and we want to display species name in <em>italic</em>, we can use command like this:</p> <pre><code>ggtree(rtree(30)) + geom_tiplab(aes(subset=node==35), label='paste(italic("species name"), "accession number")', parse=T)</code></pre> <p>Another example for formating all tip labels:</p> <pre><code>ggtree(rtree(30)) + geom_tiplab(aes(label=paste0('bold(', label, ')~italic(', node, ')')), parse=TRUE)</code></pre> <p>see also <a href="https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/BA7g-npY1BM" class="uri">https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/BA7g-npY1BM</a>.</p> </div> <div id="avoid-overlapping-text-labels" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> Avoid overlapping text labels</h2> <p>User can use <a href="https://cran.r-project.org/web/packages/ggrepel/">ggrepel</a> package to repel overlapping text labels.</p> <p>For example:</p> <pre class="r"><code>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))</code></pre> <p>For details, please refer to <a href="https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html">ggrepel usage examples</a>.</p> </div> <div id="bootstrap-values-from-newick-format" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> bootstrap values from newick format</h2> <p>It’s quite command to store <code>bootstrap</code> value as node label in <code>newick</code> format. Visualizing node label is easy using <code>geom_text2(aes(subset = !isTip, label=label))</code>.</p> <p>If you want to only display a subset of <code>bootstrap</code> (e.g. bootstrap > 80), you can’t simply using <code>geom_text2(subset= (label > 80), label=label)</code> since <code>label</code> is a character vector, which contains node label (bootstrap value) and tip label (taxa name). If we use <code>geom_text2(subset=(as.numeric(label) > 80), label=label)</code>, it will also fail since <code>NAs</code> were introduced by coercion. We need to convert <code>NAs</code> to logical <code>FALSE</code>, this can be done by the following code:</p> <pre class="r"><code>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))</code></pre> <p>Another solution is converting the bootstrap value outside <code>ggtree</code> as I recommended in <a href="https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/hiRBjGaAfI0/Y-2zDpvtAwAJ">google group</a>.</p> <pre class="r"><code>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))</code></pre> </div> </div> <div id="aesthetic-mapping" class="section level1"> <h1><i class="fa fa-map-marker"></i> <em>aesthetic</em> mapping</h1> <div id="inherit-aes" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> inherit <em>aes</em></h2> <pre class="r"><code>ggtree(rtree(30)) + geom_point()</code></pre> <p>For example, we can add symbolic points to nodes with <code>geom_point()</code> directly. The magic here is we don’t need to map <code>x</code> and <code>y</code> position of the points by providing <code>aes(x, y)</code> to <code>geom_point()</code> since it was already mapped by <code>ggtree</code> function and it serves as a global mapping for all layers.</p> <p>But what if we provide a <code>dataset</code> in a layer and the <code>dataset</code> doesn’t contain column of <code>x</code> and/or <code>y</code>, the layer function also try to map <code>x</code> and <code>y</code> and also others if you map them in <code>ggtree</code> function. As these variable is not available in your <code>dataset</code>, you will get the following error:</p> <pre><code>Error in eval(expr, envir, enclos) : object 'x' not found</code></pre> <p>This can be fixed by using parameter <code>inherit.aes=FALSE</code> which will disable inheriting mapping from <code>ggtree</code> function.</p> </div> <div id="use-in-aes" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> use <code>$</code> in aes</h2> <p>NEVER DO THIS.</p> <p>see the explaination in the <a href="https://github.com/hadley/ggplot2-book/blob/master/layers.rmd#aesthetic-mappings-secaes">ggplot2 book 2ed</a>:</p> <blockquote> <p>Never refer to a variable with <code>$</code> (e.g., <code>diamonds$carat</code>) in <code>aes()</code>. 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.</p> </blockquote> <p>see also <a href="https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/bioc-ggtree/hViM6vRZF94/MsZT8qRgBwAJ">1</a> and <a href="https://github.com/GuangchuangYu/ggtree/issues/106">2</a>.</p> </div> </div> <div id="annotation" class="section level1"> <h1><i class="fa fa-tree"></i> Annotation</h1> <div id="colouring-edges-by-user-data" class="section level2"> <h2><i class="fa fa-angle-double-right"></i> colouring edges by user data</h2> <p>see my blog post: <a href="http://guangchuangyu.github.io/2016/12/edge-coloring-with-user-data/">Edge coloring with user data</a> and also my answer on <a href="https://github.com/GuangchuangYu/ggtree/issues/76" class="uri">https://github.com/GuangchuangYu/ggtree/issues/76</a> and <a href="https://groups.google.com/forum/#!topic/bioc-ggtree/4GgivKqVjB8" class="uri">https://groups.google.com/forum/#!topic/bioc-ggtree/4GgivKqVjB8</a>.</p> </div> </div> <aside class="copyright" role="note"> © 2017 Released under the Artistic-2.0 license – Documentation built with <a href="https://www.gohugo.io" target="_blank">Hugo</a> using the <a href="http://github.com/digitalcraftsman/hugo-material-docs" target="_blank">Material</a> theme. </aside> <footer class="footer"> <nav class="pagination" aria-label="Footer"> <div class="previous"> <a href="https://guangchuangyu.github.io/ggtree/documentation/" title="Documentation"> <span class="direction"> Previous </span> <div class="page"> <div class="button button-previous" role="button" aria-label="Previous"> <i class="icon icon-back"></i> </div> <div class="stretch"> <div class="title"> Documentation </div> </div> </div> </a> </div> <div class="next"> <a href="https://guangchuangyu.github.io/ggtree/featured-articles/" title="Featured Articles"> <span class="direction"> Next </span> <div class="page"> <div class="stretch"> <div class="title"> Featured Articles </div> </div> <div class="button button-next" role="button" aria-label="Next"> <i class="icon icon-forward"></i> </div> </div> </a> </div> </nav> </footer> </div> </article> <div class="results" role="status" aria-live="polite"> <div class="scrollable"> <div class="wrapper"> <div class="meta"></div> <div class="list"></div> </div> </div> </div> </main> <script> var base_url = 'https:\/\/guangchuangyu.github.io\/ggtree\/'; var repo_id = 'GuangchuangYu\/ggtree'; </script> <script src="https://guangchuangyu.github.io/ggtree/javascripts/application.js"></script> <script> /* Add headers to scrollspy */ var headers = document.getElementsByTagName("h2"); var scrollspy = document.getElementById('scrollspy'); if(scrollspy) { if(headers.length > 0) { for(var i = 0; i < headers.length; i++) { var li = document.createElement("li"); li.setAttribute("class", "anchor"); var a = document.createElement("a"); a.setAttribute("href", "#" + headers[i].id); a.setAttribute("title", headers[i].innerHTML); a.innerHTML = headers[i].innerHTML; li.appendChild(a) scrollspy.appendChild(li); } } else { scrollspy.parentElement.removeChild(scrollspy) } /* Add permanent link next to the headers */ var headers = document.querySelectorAll("h1, h2, h3, h4, h5, h6"); for(var i = 0; i < headers.length; i++) { var a = document.createElement("a"); a.setAttribute("class", "headerlink"); a.setAttribute("href", "#" + headers[i].id); a.setAttribute("title", "Permanent link") a.innerHTML = " "; headers[i].appendChild(a); } } </script> <script> (function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q|| []).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; m.parentNode.insertBefore(a,m) })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); ga('create', 'UA-77901140-1', 'auto'); ga('set', 'anonymizeIp', true); ga('send', 'pageview'); var buttons = document.querySelectorAll('a'); Array.prototype.map.call(buttons, function(item) { if (item.host != document.location.host) { item.addEventListener('click', function() { var action = item.getAttribute('data-action') || 'follow'; ga('send', 'event', 'outbound', action, item.href); }); } }); var query = document.querySelector('.query'); query.addEventListener('blur', function() { if (this.value) { var path = document.location.pathname; ga('send', 'pageview', path + '?q=' + this.value); } }); </script> <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js"></script> <script>hljs.initHighlightingOnLoad();</script> </body> </html>