R/ggtree.R
d32146c6
 ##' drawing phylogenetic tree from phylo object
 ##'
02a6bdec
 ##'
d32146c6
 ##' @title ggtree
dc219015
 ##' @inheritParams geom_tree
d32146c6
 ##' @param tr phylo object
f46cfbcf
 ##' @param open.angle open angle, only for 'fan' layout
75f08f2b
 ##' @param mrsd most recent sampling date
 ##' @param as.Date logical whether using Date class in time tree
877c58ce
 ##' @param yscale y scale
14435e79
 ##' @param yscale_mapping yscale mapping for category variable
908f3028
 ##' @param ladderize logical (default \code{TRUE}). Should the tree be re-organized to have a 'ladder'
 ##' aspect?
 ##' @param right logical. If \code{ladderize = TRUE}, should the ladder have the smallest clade on the
 ##' right-hand side? See \code{\link[ape]{ladderize}} for more information. 
14435e79
 ##' @param branch.length variable for scaling branch, if 'none' draw cladogram
98a43cd2
 ##' @param root.position position of the root node (default = 0)
d32146c6
 ##' @return tree
 ##' @importFrom ggplot2 ggplot
 ##' @importFrom ggplot2 xlab
 ##' @importFrom ggplot2 ylab
 ##' @importFrom ggplot2 annotate
 ##' @importFrom ggplot2 scale_x_reverse
a92aaed1
 ##' @importFrom ggplot2 ylim
d32146c6
 ##' @importFrom ggplot2 coord_flip
 ##' @importFrom ggplot2 coord_polar
 ##' @export
 ##' @author Yu Guangchuang
908f3028
 ##' @seealso \code{\link[ape]{ladderize}}
d32146c6
 ##' @examples
 ##' require(ape)
 ##' tr <- rtree(10)
 ##' ggtree(tr)
877c58ce
 ggtree <- function(tr,
9f34e866
                    mapping        = NULL,
                    layout         = "rectangular",
fded823a
                    open.angle     = 0,
9f34e866
                    mrsd           = NULL,
                    as.Date        = FALSE,
                    yscale         = "none",
14435e79
                    yscale_mapping = NULL,
9f34e866
                    ladderize      = TRUE,
                    right          = FALSE,
                    branch.length  = "branch.length",
98a43cd2
                    root.position  = 0,
9f34e866
                    ...) {
02a6bdec
 
40f0f078
     # Check if layout string is valid.
b22e59a5
     layout %<>% match.arg(c("rectangular", "slanted", "fan", "circular",
                             "radial", "unrooted", "equal_angle", "daylight"))
 
e9896b76
     if (layout == "unrooted") {
         layout <- "daylight"
         message('"daylight" method was used as default layout for unrooted tree.')
     }
 
291e9370
     if(yscale != "none") {
         ## for 2d tree
14435e79
         layout <- "slanted"
291e9370
     }
fded823a
 
291e9370
     if (is.null(mapping)) {
9f34e866
         mapping <- aes_(~x, ~y)
291e9370
     } else {
9f34e866
         mapping <- modifyList(aes_(~x, ~y), mapping)
291e9370
     }
e9896b76
 
     p <- ggplot(tr,
40f0f078
                 mapping       = mapping,
877c58ce
                 layout        = layout,
75f08f2b
                 mrsd          = mrsd,
                 as.Date       = as.Date,
877c58ce
                 yscale        = yscale,
14435e79
                 yscale_mapping= yscale_mapping,
877c58ce
                 ladderize     = ladderize,
                 right         = right,
                 branch.length = branch.length,
98a43cd2
                 root.position = root.position,
14ff9149
                 ...)
90df068e
 
     if (is(tr, "multiPhylo")) {
         multiPhylo <- TRUE
     } else {
         multiPhylo <- FALSE
     }
02a6bdec
 
90df068e
     p <- p + geom_tree(layout=layout, multiPhylo=multiPhylo, ...)
 
 
     p <- p + theme_tree()
02a6bdec
 
fded823a
     if (layout == "circular" || layout == "radial") {
d3057fa1
         p <- p + layout_circular()
ccf5737c
         ## refer to: https://github.com/GuangchuangYu/ggtree/issues/6
f1f0d8d2
         ## and also have some space for tree scale (legend)
02a6bdec
         p <- p + ylim(0, NA)
fded823a
     } else if (layout == "fan") {
d3057fa1
         p <- p + layout_fan(open.angle)
fded823a
     }
9e9bcff3
 
02a6bdec
     class(p) <- c("ggtree", class(p))
 
d32146c6
     return(p)
 }