Skip to content

Commit ad5ef6d

Browse files
committed
fix coordinate for cladogram tree type
1 parent 2f750a5 commit ad5ef6d

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

R/align-phylo.R

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ AlignPhylo <- ggproto("AlignPhylo", Align,
179179
#' @inheritParams fortify_data_frame.dendrogram
180180
#' @param tree_type A single string, one of
181181
#' `r oxford_or(c("phylogram", "cladogram"))`, indicating the type of tree.
182+
#' - `phylogram`: Represents a phylogenetic tree where branch lengths indicate
183+
#' evolutionary distance or time.
184+
#' - `cladogram`: Represents a tree where branch lengths are not used, or the
185+
#' branches do not reflect evolutionary time.
186+
#'
182187
#' Usually, you don't need to modify this.
188+
#'
183189
#' @param tip_pos The x-coordinates of the tip. Must be the same length
184190
#' of the number of tips in `tree`.
185191
#' @return A `data frame` with the node coordinates:
@@ -212,8 +218,8 @@ fortify_data_frame.phylo <- function(data, ..., type = "rectangle",
212218
))
213219
}
214220
}
215-
if (is.null(edge_lengths) || identical(tree_type, "cladogram")) {
216-
edge_lengths <- seq_len(nrow(edge))
221+
if (identical(tree_type, "cladogram")) {
222+
edge_lengths <- NULL
217223
}
218224
parent <- edge[, 1L, drop = TRUE]
219225
child <- edge[, 2L, drop = TRUE]
@@ -228,18 +234,22 @@ fortify_data_frame.phylo <- function(data, ..., type = "rectangle",
228234
)
229235
}
230236
i <- 0L # tip index
231-
phylo_data <- function(index, timing, from_root = TRUE) {
237+
phylo_data <- function(index, level, timing) {
232238
if (any(select <- parent == index)) {
233-
y <- timing
239+
# recursively for each child
240+
data <- list(index = child[select])
241+
# if we have edge length, timing should be available
242+
if (!is.null(edge_lengths)) {
243+
data <- c(data, list(timing = timing + edge_lengths[select]))
244+
}
234245
data <- list_transpose(.mapply(
235-
function(index, timing) {
236-
phylo_data(index, timing, from_root = FALSE)
246+
function(index, timing = NULL) {
247+
phylo_data(index, level = level + 1L, timing = timing)
237248
},
238-
list(
239-
index = child[select],
240-
timing = timing + edge_lengths[select]
241-
), NULL
249+
data, NULL
242250
))
251+
252+
# integrate the data for each child
243253
node <- vec_rbind(!!!.subset2(data, "node"))
244254
edge <- vec_rbind(!!!.subset2(data, "edge"))
245255

@@ -266,6 +276,13 @@ fortify_data_frame.phylo <- function(data, ..., type = "rectangle",
266276
x <- sum(range(direct_leaves_x)) / 2L
267277
}
268278

279+
# y coordinate for current node
280+
if (is.null(edge_lengths) && is.null(timing)) {
281+
y <- min(direct_leaves_y) * level / (level + 1L)
282+
} else {
283+
y <- timing
284+
}
285+
269286
# there is no node data for the root
270287
node <- vec_rbind(data_frame0(
271288
.index = index,
@@ -309,7 +326,11 @@ fortify_data_frame.phylo <- function(data, ..., type = "rectangle",
309326
} else if (any(select <- child == index)) { # for the tip
310327
i <<- i + 1L
311328
x <- tip_pos[i]
312-
y <- edge_lengths[select] + timing
329+
if (is.null(edge_lengths)) {
330+
y <- 1L
331+
} else {
332+
y <- edge_lengths[select] + timing
333+
}
313334
list(
314335
node = data_frame0(
315336
.index = index,
@@ -327,6 +348,6 @@ fortify_data_frame.phylo <- function(data, ..., type = "rectangle",
327348
}
328349

329350
# from ape::is.rooted, this should be the most ancester
330-
ans <- phylo_data(N + 1L, 0L)
351+
ans <- phylo_data(N + 1L, 0L, timing = 0)
331352
ggalign_attr_set(.subset2(ans, "node"), list(edge = .subset2(ans, "edge")))
332353
}

man/align_phylo.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fortify_data_frame.phylo.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)