Skip to content

Commit

Permalink
rename coord_ggalign to discrete_ggalign
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 17, 2024
1 parent b6600a8 commit 4c07a76
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Collate:
'geom-tile3d.R'
'ggalign-package.R'
'ggplot-.R'
'ggplot-coord.R'
'ggplot-discrete.R'
'ggplot-theme.R'
'import-standalone-assert.R'
'import-standalone-obj-type.R'
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ S3method(ggoncoplot,"NULL")
S3method(ggoncoplot,default)
S3method(ggoncoplot,formula)
S3method(ggoncoplot,functon)
S3method(ggplot_add,coord_ggalign)
S3method(ggplot_add,discrete_ggalign)
S3method(ggplot_add,ggalign_default_expansion)
S3method(ggplot_add,ggalign_layer_order)
S3method(ggplot_add,ggalign_with_quad)
Expand Down
4 changes: 2 additions & 2 deletions R/align-.R
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ Align <- ggproto("Align", AlignProto,
# set limits and default scales
plot + switch_direction(
direction,
coord_ggalign(y = coords),
coord_ggalign(x = coords)
discrete_ggalign(y = coords),
discrete_ggalign(x = coords)
)
},

Expand Down
4 changes: 2 additions & 2 deletions R/cross-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ CrossGg <- ggproto("CrossGg", Cross,
}
plot$data <- data
if (is_horizontal(direction)) {
default_coord <- coord_ggalign(y = coords)
default_coord <- discrete_ggalign(y = coords)
default_expand <- default_expansion(x = expansion())
} else {
default_coord <- coord_ggalign(x = coords)
default_coord <- discrete_ggalign(x = coords)
default_expand <- default_expansion(y = expansion())
}
plot +
Expand Down
33 changes: 23 additions & 10 deletions R/ggplot-coord.R → R/ggplot-discrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
#' - limits: A boolean value indicates whether should set limits.
#' @keywords internal
#' @noRd
coord_ggalign <- function(x = NULL, y = NULL) {
structure(list(x = x, y = y), class = "coord_ggalign")
discrete_ggalign <- function(x = NULL, y = NULL) {
structure(list(x = x, y = y), class = "discrete_ggalign")
}

setup_limits <- function(axis, params) {
Expand Down Expand Up @@ -80,7 +80,7 @@ is_coord_okay.default <- function(x, axes) FALSE

#' @importFrom ggplot2 ggplot_add ggproto ggproto_parent
#' @export
ggplot_add.coord_ggalign <- function(object, plot, object_name) {
ggplot_add.discrete_ggalign <- function(object, plot, object_name) {
x_params <- .subset2(object, "x")
y_params <- .subset2(object, "y")

Expand Down Expand Up @@ -128,8 +128,12 @@ ggplot_add.coord_ggalign <- function(object, plot, object_name) {
# take the tricks to modify scales in place
modify_scales = function(self, scales_x, scales_y) {
# for each scale, we set the `breaks` and `labels`
if (!is.null(x_params)) align_scales("x", x_params, scales_x)
if (!is.null(y_params)) align_scales("y", y_params, scales_y)
if (!is.null(x_params)) {
align_discrete_scales("x", x_params, scales_x)
}
if (!is.null(y_params)) {
align_discrete_scales("y", y_params, scales_y)
}
ggproto_parent(ParentCoord, self)$modify_scales(scales_x, scales_y)
},
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
Expand Down Expand Up @@ -163,7 +167,7 @@ ggplot_add.coord_ggalign <- function(object, plot, object_name) {
plot
}

align_scales <- function(axis, params, scales) {
align_discrete_scales <- function(axis, params, scales) {
panel <- .subset2(params, "panel")
index <- .subset2(params, "index")
labels <- .subset2(params, "labels")
Expand Down Expand Up @@ -195,18 +199,27 @@ align_scales <- function(axis, params, scales) {
dindex <- .subset2(data_index, i)
pindex <- .subset2(plot_index, i)
labels <- .subset2(data_labels, i)
scale$breaks <- get_breaks(scale, pindex, dindex, labels)
scale$labels <- get_labels(
scale$breaks <- get_discrete_breaks(scale, pindex, dindex, labels)
scale$labels <- get_discrete_labels(
scale, scale$breaks, pindex, dindex, labels
)
}
# by default we elways remove any expansion
scale$expand <- scale$expand %|w|% default_expand

# for continuous scale, we don't allow the trans
if (!scale$is_discrete() && !identical(scale$trans$name, "identity")) {
cli::cli_warn(sprintf(
"{.arg trans} must be {.field identity} in {.code %s}",
deparse(scale$call)
))
scale$trans <- scales::as.transform("identity")
}
}
}

#' @importFrom rlang is_empty
get_breaks <- function(scale, pindex, dindex, labels) {
get_discrete_breaks <- function(scale, pindex, dindex, labels) {
if (scale$is_empty()) return(numeric()) # styler: off
breaks <- scale$breaks
if (identical(breaks, NA)) {
Expand Down Expand Up @@ -277,7 +290,7 @@ get_breaks <- function(scale, pindex, dindex, labels) {
}

#' @importFrom rlang is_empty
get_labels <- function(scale, breaks, pindex, dindex, labels) {
get_discrete_labels <- function(scale, breaks, pindex, dindex, labels) {
scale_labels <- scale$labels
if (is_empty(breaks) || is.null(scale_labels)) { # if no breaks, no labels
return(NULL)
Expand Down
2 changes: 1 addition & 1 deletion R/layout-quad-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ quad_build.QuadLayout <- function(quad, schemes = NULL, theme = NULL,
# we don't align observations for `quad_free()`
if (!is.null(row_coords) || !is.null(column_coords)) {
p <- p + align_melt_facet(default_facet, p$facet, strict = TRUE) +
coord_ggalign(x = column_coords, y = row_coords)
discrete_ggalign(x = column_coords, y = row_coords)
}
p <- p + theme_recycle()

Expand Down

0 comments on commit 4c07a76

Please sign in to comment.