Skip to content

Commit

Permalink
make cross_link data the same with ggalign
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 10, 2024
1 parent f448a93 commit af35b0d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
1 change: 1 addition & 0 deletions R/align-.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Align <- ggproto("Align", AlignProto,
object_name, NROW(data), layout_name, layout_nobs
))
}
# save the labels
self$labels <- vec_names(data) %||% vec_names(layout_data)
params <- self$setup_params(layout_nobs, input_params)
self$data <- ggalign_attr_restore(
Expand Down
15 changes: 7 additions & 8 deletions R/align-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ AlignGG <- ggproto("AlignGG", Align,
},
setup_data = function(self, params, data) {
ans <- fortify_data_frame(data)
# we always add `.names` and `.index` to align the observations
# we always add `.index` to align the observations
if (is.matrix(data)) {
ans$.index <- vec_rep(seq_len(NROW(data)), NCOL(data))
} else {
Expand Down Expand Up @@ -181,19 +181,18 @@ AlignGG <- ggproto("AlignGG", Align,
coord_name <- paste0(".", axis)
ans <- data_frame0(
.panel = panel, .index = index,
# `data_frame0` will omit `NULL`
.names = .subset(self$labels, index)
)
ans[[coord_name]] <- seq_along(index)

# if inherit from the parent layout
if (is.waive(.subset2(self, "input_data")) && !is.null(extra_panel)) {
# if the data is inherit from the heatmap data
# Align object always regard row as the observations
ans <- cross_join(
ans, data_frame0(
.extra_panel = extra_panel, .extra_index = extra_index
)
)
# if the data is inherit from the `quad_layout()`
# the data must be a matrix
ans <- cross_join(ans, data_frame0(
.extra_panel = extra_panel, .extra_index = extra_index
))
if (!is.null(data)) {
ans <- full_join(data, ans,
by.x = c(".column_index", ".index"),
Expand Down
42 changes: 25 additions & 17 deletions R/plot-align-cross.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#'
#' The data in the underlying `ggplot` object will contain following columns:
#'
#' - `panel`: the panel for the aligned axis. It means `x-axis` for vertical
#' - `.panel`: the panel for the aligned axis. It means `x-axis` for vertical
#' stack layout (including top and bottom annotation), `y-axis` for
#' horizontal stack layout (including left and right annotation).
#'
#' - `index`: an integer of the data index.
#' - `.index`: an integer of the data index.
#'
#' - `hand`: a factor indicates the index groups.
#' - `.hand`: a factor indicates the index groups.
#'
#' - `x` or `y`: the `x` or `y` coordinates.
#' - `.x` or `.y`: the `x` or `y` coordinates.
#'
#' It is recommended to use `x`/`y` as the `x`/`y` mapping.
#' It is recommended to use `.x`/`.y` as the `x`/`y` mapping.
#'
#' @importFrom ggplot2 ggproto aes
#' @export
Expand Down Expand Up @@ -60,12 +60,14 @@ CrossLink <- ggproto("CrossLink", AlignProto,
layout_name
))
}
layout_coords["index"] <- list(NULL)
# we keep the names from the layout data for usage
self$labels <- vec_names(layout_data)
layout_coords["index"] <- list(NULL) # reset the index
layout_coords
},
setup_plot = function(self, plot, mapping, direction) {
ggadd_default(plot, mapping = switch_direction(
direction, aes(y = .data$y), aes(x = .data$x)
direction, aes(y = .data$.y), aes(x = .data$.x)
))
},
finish = function(layout, layout_coords) {
Expand All @@ -78,18 +80,20 @@ CrossLink <- ggproto("CrossLink", AlignProto,
)
layout
},
build = function(plot, schemes, coords, extra_coords, previous_coords,
build = function(self, plot, schemes, coords, extra_coords, previous_coords,
direction, position) {
index <- vec_c(
.subset2(previous_coords, "index"),
.subset2(coords, "index")
)
data <- data_frame0(
panel = vec_c(
.panel = vec_c(
.subset2(previous_coords, "panel"),
.subset2(coords, "panel")
),
index = vec_c(
.subset2(previous_coords, "index"),
.subset2(coords, "index")
),
hand = if (is_horizontal(direction)) {
.index = index,
.names = .subset(.subset2(self, "labels"), index),
.hand = if (is_horizontal(direction)) {
factor(
vec_rep_each(
c("left", "right"),
Expand All @@ -113,20 +117,24 @@ CrossLink <- ggproto("CrossLink", AlignProto,
)
}
)
data[[switch_direction(direction, "y", "x")]] <- vec_c(
coords$labels <- .subset(
.subset2(self, "labels"),
.subset2(coords, "index")
)
data[[switch_direction(direction, ".y", ".x")]] <- vec_c(
seq_len(.subset2(previous_coords, "nobs")),
seq_len(.subset2(coords, "nobs"))
)
if (nlevels(.subset2(coords, "panel")) > 1L) {
default_facet <- switch_direction(
direction,
ggplot2::facet_grid(
rows = ggplot2::vars(fct_rev(.data$panel)),
rows = ggplot2::vars(fct_rev(.data$.panel)),
scales = "free_y", space = "free",
drop = FALSE
),
ggplot2::facet_grid(
cols = ggplot2::vars(.data$panel),
cols = ggplot2::vars(.data$.panel),
scales = "free_x", space = "free",
drop = FALSE
)
Expand Down
10 changes: 5 additions & 5 deletions man/cross_link.Rd

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

0 comments on commit af35b0d

Please sign in to comment.