Commit 8c9854ba authored by houyun's avatar houyun
Browse files

depend on aplot to insert annotate

parent 0a9e4509
Loading
Loading
Loading
Loading
+73 −25
Original line number Diff line number Diff line
wrap_annotate <- function(plot,
                          ...,
                          width = NULL,
                          height = NULL,
                          guides = "collect") {
                          height = NULL) {
  anno <- list(...)
  anno <- anno[names(anno)[names(anno) %in% c("r", "l", "t", "b")]]

@@ -13,23 +12,35 @@ wrap_annotate <- function(plot,
  anno <- lapply(anno, function(x) {
    if (inherits(x, "ggplot")) {
      list(x)
    } else x
    } else {
      x
    }
  })

  nm <- names(anno)

  for (ii in nm) {
    ele_name <- names(anno[[ii]])
    if (!is.null(ele_name)) {
    for (jj in seq_along(anno[[ii]])) {
      if (identical(anno[[ii]][[jj]],  "tree")) {
        anno[[ii]][[jj]] <- gen_tree(attr(plot$data,
                                          if (is_row) "row_tree" else "col_tree"),
                                     side = ii)
      }
      if (!is.null(ele_name)) {
        if (ele_name[jj] != "") {
          anno[[ii]][[jj]] <- anno[[ii]][[jj]] + ggplot2::labs(tag = ele_name[jj])
        }
      }

      if (ii %in% c("r", "l")) {
        anno[[ii]][[jj]] <- anno[[ii]][[jj]] + aplot::ylim2(plot)  + theme_no_axis("y")
      } else {
        anno[[ii]][[jj]] <- anno[[ii]][[jj]] + aplot::ylim2(plot) + theme_no_axis("x")
      }
    }
  }


  if (is.null(width)) {
    width <- 0.2
  }
@@ -57,23 +68,24 @@ wrap_annotate <- function(plot,
    height$b <- rep_len(height$b, length(anno$b))
  }

  n <- length(anno$t) + length(anno$b)
  m <- length(anno$l) + length(anno$r)
  ll <- rep_len(list(patchwork::plot_spacer()), (n + 1) * (m + 1))

  id <- seq(length(anno$t) * (m + 1) + 1, length.out = m + 1)
  id2 <- seq(length(anno$l) + 1, by = m + 1, length.out = n + 1)
  ll[id] <- c(anno$l, list(plot), anno$r)
  ll[id2] <- c(anno$t, list(plot), anno$b)
  for (ii in nm) {
    if (ii == "r") {
      for (jj in seq_along(anno[[ii]])) {
        plot <- plot %>% aplot::insert_right(anno[[ii]][[jj]], width = width[[ii]][jj])
      }
    }
    if (ii == "l") {
      plot <- plot %>% aplot::insert_left(anno[[ii]][[jj]], width = width[[ii]][jj])
    }
    if (ii == "t") {
      plot <- plot %>% aplot::insert_top(anno[[ii]][[jj]], height = height[[ii]][jj])
    }
    if (ii == "b") {
      plot <- plot %>% aplot::insert_bottom(anno[[ii]][[jj]], height = height[[ii]][jj])
    }
  }

  width <- c(width$l, 1, width$r)
  height <- c(height$t, 1, height$b)
  Reduce("+", ll) + patchwork::plot_layout(ncol = m + 1,
                                           nrow = n + 1,
                                           byrow = TRUE,
                                           widths = width,
                                           heights = height,
                                           guides = guides)
  plot
}

gen_tree <- function(hc, side = "t") {
@@ -85,8 +97,8 @@ gen_tree <- function(hc, side = "t") {
  }

  data <- ggdendro::dendro_data(hc)$segments
  max_x <- max(data$x, data$xend) + 0.5
  max_y <- max(data$y, data$yend) + 0.5
  max_x <- max(data$x, data$xend) + 1
  max_y <- max(data$y, data$yend)
  if (side == "b") {
    data$y <- max_y - data$y
    data$yend <- max_y - data$yend
@@ -105,5 +117,41 @@ gen_tree <- function(hc, side = "t") {
  }

  ggplot(data, aes_string(x = "x", xend = "xend", y = "y", yend = "yend")) +
    ggplot2::geom_segment()
    ggplot2::geom_segment() +
    ggplot2::theme_void()
}


theme_no_axis <- function(side = "x") {
  no_x <- ggplot2::theme(axis.title.x = element_blank(),
                         axis.title.x.top = element_blank(),
                         axis.title.x.bottom = element_blank(),
                         axis.text.x = element_blank(),
                         axis.text.x.top = element_blank(),
                         axis.text.x.bottom = element_blank(),
                         axis.ticks.x = element_blank(),
                         axis.ticks.x.top = element_blank(),
                         axis.ticks.x.bottom = element_blank(),
                         axis.line.x = element_blank(),
                         axis.line.x.top = element_blank(),
                         axis.line.x.bottom = element_blank())
  no_y <- ggplot2::theme(axis.title.y = element_blank(),
                         axis.title.y.left = element_blank(),
                         axis.title.y.right = element_blank(),
                         axis.text.y = element_blank(),
                         axis.text.y.left = element_blank(),
                         axis.text.y.right = element_blank(),
                         axis.ticks.y = element_blank(),
                         axis.ticks.y.left = element_blank(),
                         axis.ticks.y.right = element_blank(),
                         axis.line.y = element_blank(),
                         axis.line.y.left = element_blank(),
                         axis.line.y.right = element_blank())
  if (side == "all") {
    no_x + no_y
  } else if (side == "x") {
    no_x
  } else {
    no_y
  }
}