Skip to content

Commit

Permalink
consistent naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 12, 2024
1 parent 97b0622 commit 2fb324d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
12 changes: 7 additions & 5 deletions R/align-dendrogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
#' coordinates will be added.
#'
#' dendrogram `node` and `edge` contains following columns:
#' - `index`: the original index in the tree for the current node
#' - `.panel`: Similar with `panel` column, but always give the correct branch
#' for usage of the ggplot facet.
#' - `.index`: the original index in the tree for the current node.
#' - `label`: node label text
#' - `x` and `y`: x-axis and y-axis coordinates for current node or the start
#' node of the current edge.
#' - `xend` and `yend`: the x-axis and y-axis coordinates of the terminal node
#' for current edge.
#' - `branch`: which branch current node or edge is. You can use this column
#' to color different groups.
#' - `leaf`: A logical value indicates whether current node is a leaf.
#' - `panel`: which panel current node is, if we split the plot into panel
#' using [`facet_grid`][ggplot2::facet_grid], this column will show
#' which panel current node or edge is from. Note: some nodes may
#' fall outside panel (between two panel), so there are possible
#' `NA` values in this column.
#' - `.panel`: Similar with `panel` column, but always give the correct branch
#' for usage of the ggplot facet.
#' - `panel1` and `panel2`: The panel1 and panel2 variables have the same
#' functionality as `panel`, but they are specifically for the `edge` data
#' and correspond to both nodes of each edge.
#' - `leaf`: A logical value indicates whether current node is a leaf.
#'
#' @param merge_dendrogram A single boolean value, indicates whether we should
#' merge multiple dendrograms, only used when previous groups have been
Expand Down Expand Up @@ -178,7 +178,7 @@ AlignDendro <- ggproto("AlignDendro", AlignHclust,
}
data <- lapply(list_transpose(data), function(dat) {
ans <- vec_rbind(!!!dat, .names_to = "parent")
ans$.panel <- factor(.subset2(ans, ".panel"), branches)
ans$ggpanel <- factor(.subset2(ans, "ggpanel"), branches)
ans
})
} else {
Expand All @@ -202,6 +202,8 @@ AlignDendro <- ggproto("AlignDendro", AlignHclust,
}
node <- .subset2(data, "node")
edge <- .subset2(data, "edge")
node <- rename(node, c(ggpanel = ".panel", index = ".index"))
edge <- rename(edge, c(ggpanel = ".panel"))
if (is_horizontal(direction)) {
edge <- rename(
edge,
Expand Down
28 changes: 17 additions & 11 deletions R/dendrogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ make_dist <- function(matrix, distance, use_missing,
#' which panel current node or edge is from. Note: some nodes may
#' fall outside panel (between two panels), so there are possible
#' `NA` values in this column.
#' - `.panel`: Similar with `panel` column, but always give the correct
#' - `ggpanel`: Similar with `panel` column, but always give the correct
#' branch for usage of the ggplot facet.
#' - `panel1` and `panel2`: The panel1 and panel2 variables have the same
#' functionality as `panel`, but they are specifically for the `edge` data
Expand Down Expand Up @@ -258,11 +258,12 @@ dendrogram_data <- function(tree,
index = index, label = label,
x = x, y = y, branch = branch,
leaf = TRUE, panel = branch,
.panel = branch
ggpanel = branch
)
list(
# current node
node = node, edge = NULL,
# current node information
x = x, y = y,
branch = branch,
panel = branch,
Expand Down Expand Up @@ -309,21 +310,24 @@ dendrogram_data <- function(tree,
# all x coordinate for children nodes --------------------
# used if center is `TRUE`, we'll calculate the center position
# among all children nodes
leaves <- node[.subset2(node, "leaf"), ]
leaves <- node[.subset2(node, "leaf"), ] # all leaves

# x coordinate for current branch: the midpoint
if (center) {
x <- sum(range(.subset2(leaves, "x"))) / 2L
} else {
x <- sum(direct_leaves_x) / 2L
}
if (is.null(leaf_braches)) { # only one panel
if (is.null(leaf_braches)) { # no branches
ggpanel <- panel <- branch <- root
} else {
# we assign the branch for current branch node
branch <- unique(direct_leaves_branch)
# if two children leaves are different, this branch should be
# `root`, this is often used to color the segments
if (length(branch) > 1L) branch <- root

# we assign the `panel` for current branch node
ranges <- split(
.subset2(leaves, "x"),
.subset2(leaves, "panel")
Expand All @@ -342,6 +346,8 @@ dendrogram_data <- function(tree,
break
}
}
# if the node is between two panels, no panel
# we choose the priority
if (is.na(ggpanel <- panel)) {
# it's not possible for an branch node live outside the
# all panels - the left or right most. So `i` won't be 1 or
Expand All @@ -358,7 +364,7 @@ dendrogram_data <- function(tree,
node <- vec_rbind(node, data_frame0(
index = NA, label = NA,
x = x, y = y, branch = branch, leaf = FALSE,
panel = panel, .panel = ggpanel
panel = panel, ggpanel = ggpanel
))
}

Expand All @@ -373,7 +379,7 @@ dendrogram_data <- function(tree,
branch = direct_leaves_branch,
panel1 = direct_leaves_panel,
panel2 = direct_leaves_panel,
.panel = direct_leaves_ggpanel
ggpanel = direct_leaves_ggpanel
)
# 2 horizontal lines
# we double the left line and the right line when a node is not
Expand Down Expand Up @@ -428,7 +434,7 @@ dendrogram_data <- function(tree,
panel,
.subset(direct_leaves_panel, i)
),
.panel = c(
ggpanel = c(
.subset(direct_leaves_ggpanel, 3L - i),
ggpanel,
.subset(direct_leaves_ggpanel, i),
Expand All @@ -445,7 +451,7 @@ dendrogram_data <- function(tree,
branch = direct_leaves_branch,
panel1 = rep_len(panel, 2L),
panel2 = direct_leaves_panel,
.panel = rep_len(ggpanel, 2L)
ggpanel = rep_len(ggpanel, 2L)
)
}
added_edge <- vec_rbind(vertical_lines, horizontal_lines)
Expand All @@ -458,7 +464,7 @@ dendrogram_data <- function(tree,
branch = direct_leaves_branch,
panel1 = rep_len(panel, 2L),
panel2 = direct_leaves_panel,
.panel = rep_len(ggpanel, 2L)
ggpanel = rep_len(ggpanel, 2L)
)
}
if (is.null(edge)) {
Expand All @@ -484,12 +490,12 @@ dendrogram_data <- function(tree,
branch_levels <- c(branch_levels, root)
node$panel <- factor(.subset2(node, "panel"), panel_levels)
node$branch <- factor(.subset2(node, "branch"), branch_levels)
node$.panel <- factor(.subset2(node, ".panel"), panel_levels)
node$ggpanel <- factor(.subset2(node, "ggpanel"), panel_levels)
if (!is.null(edge)) {
edge$panel1 <- factor(.subset2(edge, "panel1"), panel_levels)
edge$panel2 <- factor(.subset2(edge, "panel2"), panel_levels)
edge$branch <- factor(.subset2(edge, "branch"), branch_levels)
edge$.panel <- factor(.subset2(edge, ".panel"), panel_levels)
edge$ggpanel <- factor(.subset2(edge, "ggpanel"), panel_levels)
}
list(node = node, edge = edge)
}
Expand Down
8 changes: 4 additions & 4 deletions man/align_dendro.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/dendrogram_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2fb324d

Please sign in to comment.