Browse code

set_tree_xlim

guangchuang yu authored on 31/10/2016 04:10:22
Showing 8 changed files

... ...
@@ -117,6 +117,7 @@ export(rtree)
117 117
 export(scaleClade)
118 118
 export(scale_color)
119 119
 export(scale_x_ggtree)
120
+export(set_tree_xlim)
120 121
 export(stat_balance)
121 122
 export(stat_hilight)
122 123
 export(subview)
... ...
@@ -189,6 +190,7 @@ importFrom(ggplot2,element_rect)
189 190
 importFrom(ggplot2,facet_grid)
190 191
 importFrom(ggplot2,fortify)
191 192
 importFrom(ggplot2,geom_bar)
193
+importFrom(ggplot2,geom_blank)
192 194
 importFrom(ggplot2,geom_rect)
193 195
 importFrom(ggplot2,geom_segment)
194 196
 importFrom(ggplot2,geom_text)
... ...
@@ -1,5 +1,11 @@
1 1
 CHANGES IN VERSION 1.7.1
2 2
 ------------------------
3
+ o set_tree_xlim function and test <2016-10-31, Mon>
4
+   + set x axis limit for Tree panel for facet_plot
5
+ o update read.nhx <2016-10-30, Sun>
6
+   + add tip numbers to @nhx_tags and add tests
7
+   + https://github.com/GuangchuangYu/ggtree/pull/83
8
+   + store nhx_tags$node as numeric values <2016-10-31, Mon>
3 9
  o facet_plot supports ggbio::geom_alignment <2016-10-26, Wed>
4 10
    + https://github.com/tengfei/ggbio/issues/83
5 11
  o make tree stats available in facet_plot <2016-10-24, Mon>
... ...
@@ -5,7 +5,7 @@
5 5
 ##' @param file nhx file
6 6
 ##' @return nhx object
7 7
 ##' @export
8
-##' @author Guangchuang Yu \url{http://ygc.name}
8
+##' @author Guangchuang Yu \url{https://guangchuangyu.github.io}
9 9
 read.nhx <- function(file) {
10 10
     treetext <- suppressWarnings(readLines(file))
11 11
     treetext <- treetext[treetext != ""]
... ...
@@ -27,7 +27,7 @@ read.nhx <- function(file) {
27 27
 
28 28
     phylo2 <- read.tree(text = tree2)
29 29
     treeinfo <- fortify(phylo2)
30
-    node <- as.character(treeinfo$node[match(nlab, sub(".+(X\\d+)$","\\1",treeinfo$label))])
30
+    node <- treeinfo$node[match(nlab, sub(".+(X\\d+)$","\\1",treeinfo$label))] # as.character
31 31
 
32 32
     nhx.matches <- gregexpr("(\\w+)?(:?\\d*\\.?\\d*[Ee]?[\\+\\-]?\\d*)?\\[&&NHX.*?\\]", treetext)
33 33
     matches <- nhx.matches[[1]]
... ...
@@ -1,6 +1,6 @@
1 1
 ##' plot tree associated data in an additional panel
2 2
 ##'
3
-##' 
3
+##'
4 4
 ##' @title facet_plot
5 5
 ##' @param p tree view
6 6
 ##' @param panel panel name for plot of input data
... ...
@@ -27,3 +27,29 @@ add_panel <- function(p, panel) {
27 27
     p$data <- df
28 28
     p + facet_grid(.~panel, scales="free_x")
29 29
 }
30
+
31
+##' set x axis limits for Tree panel
32
+##'
33
+##'
34
+##' @title set_tree_xlim
35
+##' @param tree_view tree view
36
+##' @param xlim xlim, should be of length 2
37
+##' @return updated tree view
38
+##' @export
39
+##' @importFrom ggplot2 geom_blank
40
+##' @author guangchuang yu
41
+set_tree_xlim <- function(tree_view, xlim) {
42
+    if (length(xlim) != 2) {
43
+        stop('-> xlim should be of length 2...')
44
+    }
45
+    x <- tree_view$data$x
46
+    if (is.na(xlim[1])) {
47
+        xlim[1] <- min(x)
48
+    }
49
+    if (is.na(xlim[2])) {
50
+        xlim[2] <- max(x)
51
+    }
52
+    dummy <- data.frame(x=xlim, panel='Tree')
53
+    tree_view + geom_blank(aes(x=x), dummy, inherit.aes = FALSE)
54
+}
55
+
... ...
@@ -3,7 +3,7 @@ ggtree: an R package for visualization and annotation of phylogenetic trees with
3 3
 
4 4
 [![releaseVersion](https://img.shields.io/badge/release%20version-1.6.1-green.svg?style=flat)](https://bioconductor.org/packages/ggtree) [![develVersion](https://img.shields.io/badge/devel%20version-1.7.1-green.svg?style=flat)](https://github.com/GuangchuangYu/ggtree) [![Bioc](http://www.bioconductor.org/shields/years-in-bioc/ggtree.svg)](https://www.bioconductor.org/packages/devel/bioc/html/ggtree.html#since) [![total](https://img.shields.io/badge/downloads-15734/total-blue.svg?style=flat)](https://bioconductor.org/packages/stats/bioc/ggtree) [![month](https://img.shields.io/badge/downloads-1678/month-blue.svg?style=flat)](https://bioconductor.org/packages/stats/bioc/ggtree)
5 5
 
6
-[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) [![codecov](https://codecov.io/gh/GuangchuangYu/ggtree/branch/master/graph/badge.svg)](https://codecov.io/gh/GuangchuangYu/ggtree) [![Last-changedate](https://img.shields.io/badge/last%20change-2016--10--30-green.svg)](https://github.com/GuangchuangYu/ggtree/commits/master) [![GitHub forks](https://img.shields.io/github/forks/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/network) [![GitHub stars](https://img.shields.io/github/stars/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/stargazers) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://awesome-r.com/#awesome-r-graphic-displays)
6
+[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) [![codecov](https://codecov.io/gh/GuangchuangYu/ggtree/branch/master/graph/badge.svg)](https://codecov.io/gh/GuangchuangYu/ggtree) [![Last-changedate](https://img.shields.io/badge/last%20change-2016--10--31-green.svg)](https://github.com/GuangchuangYu/ggtree/commits/master) [![GitHub forks](https://img.shields.io/github/forks/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/network) [![GitHub stars](https://img.shields.io/github/stars/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/stargazers) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://awesome-r.com/#awesome-r-graphic-displays)
7 7
 
8 8
 [![platform](http://www.bioconductor.org/shields/availability/devel/ggtree.svg)](https://www.bioconductor.org/packages/devel/bioc/html/ggtree.html#archives) [![Build Status](http://www.bioconductor.org/shields/build/devel/bioc/ggtree.svg)](https://bioconductor.org/checkResults/devel/bioc-LATEST/ggtree/) [![Linux/Mac Travis Build Status](https://img.shields.io/travis/GuangchuangYu/ggtree/master.svg?label=Mac%20OSX%20%26%20Linux)](https://travis-ci.org/GuangchuangYu/ggtree) [![AppVeyor Build Status](https://img.shields.io/appveyor/ci/Guangchuangyu/ggtree/master.svg?label=Windows)](https://ci.appveyor.com/project/GuangchuangYu/ggtree) [![install with bioconda](https://img.shields.io/badge/install%20with-bioconda-green.svg?style=flat)](http://bioconda.github.io/recipes/bioconductor-ggtree/README.html)
9 9
 
... ...
@@ -16,6 +16,6 @@ nhx object
16 16
 read nhx tree file
17 17
 }
18 18
 \author{
19
-Guangchuang Yu \url{http://ygc.name}
19
+Guangchuang Yu \url{https://guangchuangyu.github.io}
20 20
 }
21 21
 
22 22
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/facet_plot.R
3
+\name{set_tree_xlim}
4
+\alias{set_tree_xlim}
5
+\title{set_tree_xlim}
6
+\usage{
7
+set_tree_xlim(tree_view, xlim)
8
+}
9
+\arguments{
10
+\item{tree_view}{tree view}
11
+
12
+\item{xlim}{xlim, should be of length 2}
13
+}
14
+\value{
15
+updated tree view
16
+}
17
+\description{
18
+set x axis limits for Tree panel
19
+}
20
+\author{
21
+guangchuang yu
22
+}
23
+
0 24
new file mode 100644
... ...
@@ -0,0 +1,14 @@
1
+context("set_tree_xlim")
2
+
3
+test_that("dummy layer to set x axis limits of Tree panel", {
4
+    set.seed(2016-10-31)
5
+    tr <- rtree(50)
6
+    tr$tip.label <- paste(tr$tip.label, tr$tip.label, sep="_")
7
+    p <- ggtree(tr) + geom_tiplab(align=TRUE) + theme_tree2()
8
+
9
+    d <- data.frame(id = tr$tip.label, v= rnorm(50))
10
+
11
+    p2 <- facet_plot(set_tree_xlim(p, c(NA, 6)), geom=geom_point, data=d, mapping=aes(x=v), panel='dot') + ggtitle('*set_tree_xlim* only change x axis limits of *Tree* panel')
12
+
13
+    expect_true(is.ggplot(p2)) # should plot appropriately
14
+})