Skip to content

Commit

Permalink
use coordinates to remove expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Jan 2, 2025
1 parent b5a7859 commit 249cda4
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions R/ggplot-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,39 +358,35 @@ no_expansion <- function(borders = "tlbr") {
#' @importFrom ggplot2 ggplot_add ggproto ggproto_parent
#' @export
ggplot_add.ggalign_no_expansion <- function(object, plot, object_name) {
ParentFacet <- .subset2(plot, "facet")
ParentCoord <- .subset2(plot, "coordinates")
borders <- .subset2(object, "borders")
plot$facet <- ggproto(
NULL, ParentFacet,
init_scales = function(self, layout, x_scale = NULL,
y_scale = NULL, params) {
if (!is.null(x_scale) && any(borders == c("left", "right"))) {
expansion <- x_scale$expand %|w|%
ggfun("default_expansion")(x_scale, expand = TRUE)
plot$coordinates <- ggproto(
NULL, ParentCoord,
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
if (!is.null(scale_x) && any(borders == c("left", "right"))) {
expansion <- scale_x$expand %|w|%
ggfun("default_expansion")(scale_x, expand = TRUE)
if (any(borders == "left")) {
expansion[1:2] <- 0
}
if (any(borders == "right")) {
expansion[3:4] <- 0
}
x_scale$expand <- expansion
scale_x$expand <- expansion
}
if (!is.null(y_scale) && any(borders == c("bottom", "top"))) {
expansion <- y_scale$expand %|w|%
ggfun("default_expansion")(y_scale, expand = TRUE)
if (!is.null(scale_y) && any(borders == c("bottom", "top"))) {
expansion <- scale_y$expand %|w|%
ggfun("default_expansion")(scale_y, expand = TRUE)
if (any(borders == "bottom")) {
expansion[1:2] <- 0
}
if (any(borders == "top")) {
expansion[3:4] <- 0
}
y_scale$expand <- expansion
scale_y$expand <- expansion
}
ggproto_parent(ParentFacet, self)$init_scales(
layout = layout,
x_scale = x_scale,
y_scale = y_scale,
params = params
ggproto_parent(ParentCoord, self)$setup_panel_params(
scale_x = scale_x, scale_y = scale_y, params = params
)
}
)
Expand Down

0 comments on commit 249cda4

Please sign in to comment.