R/tidy-cluster-tree.R

Defines functions tidy_tree_data

#' @noRd
tidy_tree_data <- function(hc,
                           side = "right",
                           max.height = 4
                           ){
  side <- match.arg(side, c("left", "right", "bottom", "top"))
  h <- hc$height / max(hc$height) * max.height
  m <- hc$merge
  o <- hc$order
  n <- length(o)
  m[m > 0] <- n + m[m > 0]
  m[m < 0] <- abs(m[m < 0])
  rows <- nrow(m)
  m1 <- m[ , 1]
  m2 <- m[ , 2]
  xx <- c(match(1:n, o), rep_len(0, n - 1))
  for (i in 1:rows) {
    xx[n + i] <- (xx[m[i, 1]] + xx[m[i, 2]]) / 2
  }
  yy <- c(rep_len(0, n), h)

  x <- c(xx[m1], xx[m1], xx[m2], xx[m2])
  y <- c(yy[m1], h, h, yy[m2])
  temp <- x
  if(side == "left") {
    x <- - y + 0.5
    y <- max(temp) - temp + 1
  } else if(side == "right") {
    x <- y
    y <- max(temp) - temp + 1
  } else if(side == "bottom") {
    y <- - y + 0.5
  }
  new_data_frame(list(x = x, y = y, id = rep(1:rows, 4)))
}
houyunhuang/ggtriangle documentation built on May 11, 2020, 2:02 p.m.