Skip to content

Commit

Permalink
re-design cross functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 13, 2024
1 parent 0411d34 commit 8dcd0ee
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 120 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Collate:
'alignpatch-patchwork.R'
'alignpatch-title.R'
'alignpatch-wrap.R'
'cross-.R'
'cross-gg.R'
'dendrogram.R'
'fortify-data_frame.R'
'fortify-matrix.R'
Expand Down Expand Up @@ -109,7 +111,6 @@ Collate:
'layout-stack-switch.R'
'object-name.R'
'plot-add.R'
'plot-align-cross.R'
'plot-free-.R'
'plot-free-gg.R'
'raster-magick.R'
Expand Down
15 changes: 8 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

S3method("$<-",AlignProto)
S3method("+",alignpatches)
S3method("[",alignLinkGtable)
S3method("[",alignLineGtable)
S3method("[",alignRangeGtable)
S3method(.raster_magick,Layer)
S3method(.raster_magick,QuadLayout)
Expand All @@ -26,7 +26,7 @@ S3method(alignpatch,Heatmap)
S3method(alignpatch,HeatmapAnnotation)
S3method(alignpatch,HeatmapList)
S3method(alignpatch,LayoutProto)
S3method(alignpatch,align_link_plot)
S3method(alignpatch,align_line_plot)
S3method(alignpatch,align_range_plot)
S3method(alignpatch,alignpatches)
S3method(alignpatch,default)
Expand Down Expand Up @@ -154,7 +154,7 @@ S3method(layout_and_add,QuadLayout)
S3method(layout_and_add,StackLayout)
S3method(layout_subtract,QuadLayout)
S3method(layout_subtract,StackLayout)
S3method(makeContent,alignLinkGtable)
S3method(makeContent,alignLineGtable)
S3method(makeContent,alignRangeGtable)
S3method(makeContext,ggalign_raster_magick)
S3method(make_wrap,alignpatches)
Expand All @@ -164,7 +164,9 @@ S3method(make_wrap,wrapped_plot)
S3method(new_scheme_theme,scheme_theme)
S3method(new_scheme_theme,theme)
S3method(obj_print_footer,ggalign_area)
S3method(object_name,AlignGg)
S3method(object_name,AlignProto)
S3method(object_name,CrossGg)
S3method(object_name,QuadLayout)
S3method(object_name,StackLayout)
S3method(object_name,ggalign_align_plot)
Expand Down Expand Up @@ -252,7 +254,7 @@ S3method(stack_layout_add,QuadLayout)
S3method(stack_layout_add,StackLayout)
S3method(stack_layout_add,default)
S3method(stack_layout_add,ggalign_align_plot)
S3method(stack_layout_add,ggalign_cross_link)
S3method(stack_layout_add,ggalign_cross)
S3method(stack_layout_add,ggalign_free_plot)
S3method(stack_layout_add,ggalign_with_quad)
S3method(stack_layout_add,ggplot)
Expand Down Expand Up @@ -296,7 +298,7 @@ export(align_gg)
export(align_group)
export(align_hclust)
export(align_kmeans)
export(align_link)
export(align_line)
export(align_order)
export(align_panel)
export(align_plots)
Expand All @@ -311,8 +313,8 @@ export(area)
export(cross_align)
export(cross_alignh)
export(cross_alignv)
export(cross_gg)
export(cross_layout)
export(cross_link)
export(dendrogram_data)
export(draw_key_draw)
export(element_polygon)
Expand Down Expand Up @@ -343,7 +345,6 @@ export(ggheatmap)
export(ggoncoplot)
export(ggpanel)
export(ggside)
export(ggstack)
export(ggwrap)
export(gpar)
export(hclust2)
Expand Down
4 changes: 2 additions & 2 deletions R/align-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ align_gg <- function(data = waiver(), mapping = aes(), size = NULL,
active <- deprecate_active(active, "align_gg",
set_context = set_context, order = order, name = name
)
align(AlignGG,
align(AlignGg,
plot = ggplot(mapping = mapping),
size = size, data = data,
schemes = waiver(),
Expand All @@ -128,7 +128,7 @@ align_gg <- function(data = waiver(), mapping = aes(), size = NULL,
ggalign <- align_gg

#' @importFrom ggplot2 ggproto ggplot
AlignGG <- ggproto("AlignGG", Align,
AlignGg <- ggproto("AlignGg", Align,
nobs = function(self) { # no input data
axis <- to_coord_axis(.subset2(self, "direction"))
cli_abort(c(
Expand Down
30 changes: 15 additions & 15 deletions R/align-link.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Add a plot to annotate selected observations
#'
#' - `align_link`: Annotate a list of spread observations. Observations will be
#' - `align_line`: Annotate a list of spread observations. Observations will be
#' connected to the panel by a line.
#' - `align_range`: Annotate a list of ranges of observations. Observation
#' ranges will be connected to the panel by a polygon.
Expand All @@ -17,7 +17,7 @@
#'
#' - Link ranges can be customized using the `plot.ggalign_ranges` theme element
#' with [`element_polygon()`].
#' - Link lines can be customized using the `plot.ggalign_links` theme element
#' - Link lines can be customized using the `plot.ggalign_lines` theme element
#' with [`element_line()`].
#'
#' @section ggplot2 specification:
Expand Down Expand Up @@ -52,7 +52,7 @@
#' (`.panel`, `.index`, `.names`) are added to the data frame.
#'
#' @export
align_link <- function(data = waiver(), mapping = aes(),
align_line <- function(data = waiver(), mapping = aes(),
links = waiver(), position = waiver(),
size = NULL, active = NULL) {
assert_layout_position(position)
Expand All @@ -68,8 +68,8 @@ align_link <- function(data = waiver(), mapping = aes(),
new_align_link(
"AlignLink",
arg = "links",
class = "align_link_plot",
element = "plot.ggalign_links"
class = "align_line_plot",
element = "plot.ggalign_lines"
),
plot = ggplot(mapping = mapping),
size = size, data = data,
Expand All @@ -81,15 +81,15 @@ align_link <- function(data = waiver(), mapping = aes(),

#' @importFrom ggplot2 ggproto
#' @export
alignpatch.align_link_plot <- function(x) {
ggproto(NULL, PatchAlignLinkPlot, plot = x)
alignpatch.align_line_plot <- function(x) {
ggproto(NULL, PatchAlignLinePlot, plot = x)
}

#' @export
`[.alignLinkGtable` <- function(x, i, j) {
# subset will violate the `alignLinkGtable` `shape`
`[.alignLineGtable` <- function(x, i, j) {
# subset will violate the `alignLineGtable` `shape`
# we always use the next method
class(x) <- setdiff(class(x), "alignLinkGtable")
class(x) <- setdiff(class(x), "alignLineGtable")
x$links_data <- NULL
NextMethod()
}
Expand All @@ -105,7 +105,7 @@ alignpatch.align_link_plot <- function(x) {
# - popgrobvp
#' @importFrom grid makeContent unit convertHeight convertWidth viewport
#' @export
makeContent.alignLinkGtable <- function(x) {
makeContent.alignLineGtable <- function(x) {
# Grab viewport information
width <- convertWidth(unit(1, "npc"), "mm", valueOnly = TRUE)
height <- convertHeight(unit(1, "npc"), "mm", valueOnly = TRUE)
Expand Down Expand Up @@ -286,21 +286,21 @@ PatchAlignLinkProto <- ggproto(
}
)

PatchAlignLinkPlot <- ggproto(
"PatchAlignLinkPlot", PatchAlignLinkProto,
PatchAlignLinePlot <- ggproto(
"PatchAlignLinePlot", PatchAlignLinkProto,
patch_gtable = function(self, plot = self$plot) {
ans <- ggproto_parent(PatchAlignLinkProto, self)$patch_gtable(
plot = plot
)
# re-define the draw method, we assign new class
ans <- add_class(ans, "alignLinkGtable")
ans <- add_class(ans, "alignLineGtable")
ans$links_data <- .subset2(plot, "links_data")
ans
}
)

#' @importFrom ggplot2 ggproto ggplot margin element_rect
AlignLinkProto <- ggproto("AlignLinkProto", AlignGG,
AlignLinkProto <- ggproto("AlignLinkProto", AlignGg,
class = NULL, element = NULL,
finish_plot = function(self, plot, schemes, theme) {
plot <- plot_add_schemes(plot, schemes)
Expand Down
2 changes: 1 addition & 1 deletion R/align-range.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @export
#' @rdname align_link
#' @rdname align_line
align_range <- function(data = waiver(), mapping = aes(),
ranges = waiver(), position = waiver(),
size = NULL, active = NULL) {
Expand Down
16 changes: 16 additions & 0 deletions R/cross-.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Since `ggalign_align_plot` object need act with the layout, we Use R6 object
# here
cross <- function(cross, ..., call = caller_call()) {
new_align_plot(
align = ggproto(NULL, cross),
...,
class = "ggalign_cross",
call = call
)
}

#' @include plot-align-.R
methods::setClass("ggalign_cross", contains = "ggalign_align_plot")

#' @importFrom methods is
is_cross_plot <- function(x) is(x, "ggalign_cross")
39 changes: 19 additions & 20 deletions R/plot-align-cross.R → R/cross-gg.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#' Connect two layout crosswise
#'
#' @description
#' `cross_link` resets the layout ordering index of a [`cross_align()`]. This
#' `cross_gg` resets the layout ordering index of a [`cross_align()`]. This
#' allows you to add other `align_*` objects to define a new layout ordering
#' index. Any objects added after `cross_link` will use this updated layout
#' index. Any objects added after `cross_gg` will use this updated layout
#' ordering index. This feature is particularly useful for creating `tanglegram`
#' visualizations.
#' visualizations. `ggcross()` is an alias of `cross_gg()`.
#'
#' @inheritParams ggalign
#' @section ggplot2 specification:
#' `cross_link` initializes a ggplot `data` and `mapping`.
#' `ggcross()` initializes a ggplot `data` and `mapping`.
#'
#' `cross_link()` always applies a default mapping for the axis of the data
#' index in the layout. This mapping is `aes(y = .data$y)` for horizontal stack
#' layout (including left and right annotation) and `aes(x = .data$x)` for
#' vertical stack layout (including top and bottom annotation).
#' `ggcross()` always applies a default mapping for the axis of the data index
#' in the layout. This mapping is `aes(y = .data$y)` for horizontal stack layout
#' (including left and right annotation) and `aes(x = .data$x)` for vertical
#' stack layout (including top and bottom annotation).
#'
#' The data in the underlying `ggplot` object will contain following columns:
#'
Expand All @@ -33,25 +34,23 @@
#'
#' @importFrom ggplot2 ggproto aes
#' @export
cross_link <- function(mapping = aes(), size = NULL,
no_axes = NULL, active = NULL) {
cross_gg <- function(mapping = aes(), size = NULL,
no_axes = NULL, active = NULL) {
active <- update_active(active, new_active(use = TRUE))
new_align_plot(
align = ggproto(NULL, CrossLink),
cross(
cross = CrossGg,
plot = ggplot(mapping = mapping),
size = size, no_axes = no_axes, active = active,
class = "ggalign_cross_link"
size = size, no_axes = no_axes, active = active
)
}

#' @include plot-align-.R
methods::setClass("ggalign_cross_link", contains = "ggalign_align_plot")

#' @importFrom methods is
is_cross_link <- function(x) is(x, "ggalign_cross_link")
#' @usage NULL
#' @export
#' @rdname cross_gg
ggcross <- cross_gg

#' @importFrom ggplot2 ggproto ggplot
CrossLink <- ggproto("CrossLink", AlignProto,
CrossGg <- ggproto("CrossGg", AlignProto,
layout = function(self, layout_data, layout_coords, layout_name) {
if (is.null(.subset2(layout_coords, "nobs"))) {
cli_abort(sprintf(
Expand Down
4 changes: 2 additions & 2 deletions R/ggplot-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ theme_elements <- function() {
linewidth = 0.5,
linetype = 1
),
plot.ggalign_links = element_line(
plot.ggalign_lines = element_line(
color = "black",
linewidth = 0.5,
linetype = 1,
Expand All @@ -191,7 +191,7 @@ theme_elements <- function() {
plot.patch_title.position.bottom = el_def("character"),
plot.patch_title.position.right = el_def("character"),
plot.ggalign_ranges = el_def("element_polygon"),
plot.ggalign_links = el_def("element_line")
plot.ggalign_lines = el_def("element_line")
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion R/layout-coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ update_layout_coords.CrossLayout <- function(layout, ..., coords, object_name,
}
index <- seq_len(index)
} else { # update the tail plots
# one for the `cross_link()` plot itself
# one for the `ggcross()` plot itself
index <- layout@cross_points[n_breaks] + 1L + 1L
if (index > n_plots) {
return(layout)
Expand Down
24 changes: 10 additions & 14 deletions R/layout-cross-.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#' `r lifecycle::badge('experimental')`
#'
#' The `cross_align` function aligns observations, and allow different layout
#' ordering index in a single layout. Both `ggcross` and `cross_layout` are
#' alias for `cross_align`.
#' ordering index in a single layout. `cross_layout` is an alias for
#' `cross_align`.
#'
#' Two aliases are provided for convenience:
#' - `cross_alignv`: A special case of `cross_align` that sets `direction =
Expand All @@ -14,18 +14,13 @@
#' "horizontal"`.
#'
#' @inheritParams stack_align
#' @seealso [`cross_link()`]
#' @seealso [`ggcross()`]
#' @export
cross_align <- function(data = NULL, direction, ...,
theme = NULL, sizes = NA) {
UseMethod("cross_align")
}

#' @usage NULL
#' @export
#' @rdname cross_align
ggcross <- cross_align

#' @usage NULL
#' @export
#' @rdname cross_align
Expand Down Expand Up @@ -67,10 +62,11 @@ stack_build_composer.CrossLayout <- function(stack, schemes, theme,
layout_coords <- stack@layout
if (!is.null(layout_coords) &&
is.null(.subset2(layout_coords, "nobs")) &&
any(vapply(plot_list, is_cross_link, logical(1L), USE.NAMES = FALSE))) {
cli_abort(
"You must initialize the layout observations when used with a {.fn cross_link}"
)
any(vapply(plot_list, is_cross_plot, logical(1L), USE.NAMES = FALSE))) {
cli_abort(sprintf(
"You must initialize the layout observations to plot the %s",
object_name(stack)
))
}
plot_list <- stack@plot_list

Expand Down Expand Up @@ -126,8 +122,8 @@ stack_build_composer.CrossLayout <- function(stack, schemes, theme,

# we reorder the plots based on the `order` slot
plot_order <- vapply(plots, function(plot) {
# always keep cross_link() in the start
if (is_cross_link(plot)) {
# always keep cross() in the start
if (is_cross_plot(plot)) {
1L
} else if (is_ggalign_plot(plot)) {
.subset2(plot@active, "order")
Expand Down
2 changes: 1 addition & 1 deletion R/layout-operator.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#' # Modify the the color scales of all plots in the left annotation
#' scale_color_brewer(palette = "Dark2")
#'
#' # If the active layout is the `ggstack()`/`stack_layout()` itself, `-`
#' # If the active layout is the `stack_layout()` itself, `-`
#' # applies the elements to all plots in the layout except the nested
#' # `ggheatmap()`/`quad_layout()`.
#' stack_alignv(small_mat) +
Expand Down
Loading

0 comments on commit 8dcd0ee

Please sign in to comment.