|
| 1 | +##' @importFrom ggplot2 fortify |
| 2 | +##' @method fortify phylo |
| 3 | +##' @export |
| 4 | +fortify.phylo <- function(model, data, layout="rectangular", branch.length ="branch.length", |
| 5 | + ladderize=TRUE, right=FALSE, mrsd=NULL, ...) { |
| 6 | + model <- set_branch_length(model, branch.length) |
| 7 | + x <- reorder.phylo(get.tree(model), "postorder") |
| 8 | + if (is.null(x$edge.length) || branch.length == "none") { |
| 9 | + xpos <- getXcoord_no_length(x) |
| 10 | + } else { |
| 11 | + xpos <- getXcoord(x) |
| 12 | + } |
| 13 | + ypos <- getYcoord(x) |
| 14 | + N <- Nnode(x, internal.only=FALSE) |
| 15 | + xypos <- data.frame(node=1:N, x=xpos, y=ypos) |
| 16 | + |
| 17 | + df <- as.data.frame(model, branch.length="branch.length") # already set by set_branch_length |
| 18 | + idx <- is.na(df$parent) |
| 19 | + df$parent[idx] <- df$node[idx] |
| 20 | + rownames(df) <- df$node |
| 21 | + |
| 22 | + res <- merge(df, xypos, by='node', all.y=TRUE) |
| 23 | + |
| 24 | + ## add branch mid position |
| 25 | + res <- calculate_branch_mid(res) |
| 26 | + |
| 27 | + ## ## angle for all layout, if 'rectangular', user use coord_polar, can still use angle |
| 28 | + res <- calculate_angle(res) |
| 29 | + res |
| 30 | +} |
| 31 | + |
| 32 | +##' @method fortify treedata |
| 33 | +##' @export |
| 34 | +fortify.treedata <- fortify.phylo |
| 35 | + |
| 36 | + |
| 37 | +calculate_angle <- function(data) { |
| 38 | + data$angle <- 360/(diff(range(data$y)) + 1) * data$y |
| 39 | + return(data) |
| 40 | +} |
0 commit comments