Commit f6425d7c authored by houyun's avatar houyun
Browse files

can reorder md_tbl by hc or ggtree object

parent a0d49eca
Loading
Loading
Loading
Loading
+53 −9
Original line number Diff line number Diff line
@@ -41,10 +41,6 @@ reorder_by.matrix <- function(x,
    by_rows <- stats::as.hclust(by_rows)
  }

  if (inherits(by_rows, "hclust") || inherits(by_rows, "ggtree")) {
    attr(x, "row_tree") <- by_rows
  }

  if (inherits(by_rows, "numeric") || inherits(by_rows, "character")) {
    row_ord <- by_rows
  } else {
@@ -62,16 +58,20 @@ reorder_by.matrix <- function(x,
    by_cols <- stats::as.hclust(by_cols)
  }

  if (inherits(by_cols, "hclust") || inherits(by_cols, "ggtree")) {
    attr(x, "col_tree") <- by_cols
  }

  if (inherits(by_cols, "numeric") || inherits(by_cols, "character")) {
    col_ord <- by_cols
  } else {
    col_ord <- get_order(by_cols)
  }
  x[row_ord, col_ord, drop = FALSE]
  x <- x[row_ord, col_ord, drop = FALSE]

  if (inherits(by_rows, "hclust") || inherits(by_rows, "ggtree")) {
    attr(x, "row_tree") <- by_rows
  }
  if (inherits(by_cols, "hclust") || inherits(by_cols, "ggtree")) {
    attr(x, "col_tree") <- by_cols
  }
  x
}

#' @rdname reorder_by
@@ -89,6 +89,8 @@ reorder_by.data.frame <- function(x,
  as.data.frame(x)
}

#' @rdname reorder_by
#' @export
reorder_by.correlate <- function(x,
                                 by_rows = "hclust",
                                 by_cols = by_rows,
@@ -161,3 +163,45 @@ reorder_by.matrix_data <- function(x,
  attr(x, "col_names") <- cnm
  x
}

#' @rdname reorder_by
#' @export
reorder_by.md_tbl <- function(x,
                              by_rows = NULL,
                              by_cols = NULL,
                              ...) {
  if (inherits(by_rows, "dist")) {
    by_rows <- hclust(by_rows, ...)
  }
  if (inherits(by_rows, "dendrogram")) {
    by_rows <- stats::as.hclust(by_rows, ...)
  }
  if (inherits(by_rows, "md_tbl")) {
    by_rows <- row_names(by_rows)
  }
  if (!is.null(by_rows)) {
    if (inherits(by_rows, "hclust") || inherits(by_rows, "ggtree")) {
      attr(x, "row_tree") <- by_rows
    }
    row_ord <- get_order(by_rows, name = row_names(x))
    attr(x, "row_names") <- row_names(x)[row_ord]
  }

  if (inherits(by_cols, "dist")) {
    by_cols <- hclust(by_cols, ...)
  }
  if (inherits(by_cols, "dendrogram")) {
    by_cols <- stats::as.hclust(by_cols, ...)
  }
  if (inherits(by_cols, "md_tbl")) {
    by_cols <- col_names(by_cols)
  }
  if (!is.null(by_cols)) {
    if (inherits(by_cols, "hclust") || inherits(by_cols, "ggtree")) {
      attr(x, "col_tree") <- by_cols
    }
    col_ord <- get_order(by_cols, name = col_names(x))
    attr(x, "col_names") <- col_names(x)[col_ord]
  }
  x
}
+6 −0
Original line number Diff line number Diff line
@@ -4,7 +4,9 @@
\alias{reorder_by}
\alias{reorder_by.matrix}
\alias{reorder_by.data.frame}
\alias{reorder_by.correlate}
\alias{reorder_by.matrix_data}
\alias{reorder_by.md_tbl}
\title{Re-sorting Matrix Data}
\usage{
reorder_by(x, ...)
@@ -25,6 +27,8 @@ reorder_by(x, ...)
  ...
)

\method{reorder_by}{correlate}(x, by_rows = "hclust", by_cols = by_rows, dist_fun = NULL, ...)

\method{reorder_by}{matrix_data}(
  x,
  by_rows = "hclust",
@@ -32,6 +36,8 @@ reorder_by(x, ...)
  dist_fun = dist_func(),
  ...
)

\method{reorder_by}{md_tbl}(x, by_rows = NULL, by_cols = NULL, ...)
}
\arguments{
\item{x}{matrix data.}