Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geom_rect() can derive corners from x/width or y/height #5862

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `geom_rect()` can now derive the required corners positions from `x`/`width`
or `y`/`height` parameterisation (@teunbrand, #5861).

# ggplot2 3.5.1

This is a small release focusing on fixing regressions from 3.5.0 and
Expand Down
2 changes: 2 additions & 0 deletions R/geom-bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ geom_bar <- function(mapping = NULL, data = NULL,
GeomBar <- ggproto("GeomBar", GeomRect,
required_aes = c("x", "y"),

optional_aes = character(),

# These aes columns are created by setup_data(). They need to be listed here so
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
# limits, not just those for which x and y are outside the limits
Expand Down
55 changes: 54 additions & 1 deletion R/geom-rect.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,37 @@ GeomRect <- ggproto("GeomRect", Geom,
default_aes = aes(colour = NA, fill = "grey35", linewidth = 0.5, linetype = 1,
alpha = NA),

required_aes = c("xmin", "xmax", "ymin", "ymax"),
optional_aes = c("x", "width", "xmin", "xmax", "y", "height", "ymin", "ymax"),
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),

setup_data = function(self, data, params) {
data$xmin <- data$xmin %||% params$xmin
data$xmax <- data$xmax %||% params$xmax
data$ymin <- data$ymin %||% params$ymin
data$ymax <- data$ymax %||% params$ymax
if (is.null(data$xmin) || is.null(data$xmax)) {
x <- resolve_rect(
data[["xmin"]], data[["xmax"]],
data[["x"]], data[["width"]] %||% params$width
)
i <- lengths(x) > 1
data[c("xmin", "xmax")[i]] <- x[i]
}
if (is.null(data$ymin) || is.null(data$ymax)) {
y <- resolve_rect(
data[["ymin"]], data[["ymax"]],
data[["y"]], data[["height"]] %||% params$height
)
i <- lengths(y) > 1
data[c("ymin", "ymax")[i]] <- y[i]
}
check_required_aesthetics(
self$non_missing_aes,
names(data),
snake_class(self)
)
teunbrand marked this conversation as resolved.
Show resolved Hide resolved
data
},

draw_panel = function(self, data, panel_params, coord, lineend = "butt", linejoin = "mitre") {
data <- check_linewidth(data, snake_class(self))
Expand Down Expand Up @@ -73,3 +103,26 @@ GeomRect <- ggproto("GeomRect", Geom,

rename_size = TRUE
)

resolve_rect <- function(min = NULL, max = NULL, center = NULL, length = NULL) {
if (is.null(min) && is.null(max)) {
min <- center - 0.5 * length
max <- center + 0.5 * length
return(list(min = min, max = max))
}
if (is.null(min)) {
if (is.null(center)) {
min <- max - length
} else {
min <- max - 2 * (max - center)
}
}
if (is.null(max)) {
if (is.null(center)) {
max <- min + length
} else {
max <- min + 2 * (center - min)
}
}
list(min = min, max = max)
}
19 changes: 8 additions & 11 deletions R/geom-tile.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#' Rectangles
#'
#' `geom_rect()` and `geom_tile()` do the same thing, but are
#' parameterised differently: `geom_rect()` uses the locations of the four
#' corners (`xmin`, `xmax`, `ymin` and `ymax`), while
#' `geom_tile()` uses the center of the tile and its size (`x`,
#' `y`, `width`, `height`). `geom_raster()` is a high
#' performance special case for when all the tiles are the same size, and no
#' pattern fills are applied.
#' parameterised differently: `geom_tile()` uses the center of the tile and its
#' size (`x`, `y`, `width`, `height`), while `geom_rect()` can use those or the
#' locations of the corners (`xmin`, `xmax`, `ymin` and `ymax`).
#' `geom_raster()` is a high performance special case for when all the tiles
#' are the same size, and no pattern fills are applied.
#'
#' @eval rd_aesthetics("geom", "tile", "Note that `geom_raster()` ignores `colour`.")
#' @inheritParams layer
Expand All @@ -15,11 +14,9 @@
#' @export
#'
#' @details
#' `geom_rect()` and `geom_tile()`'s respond differently to scale
#' transformations due to their parameterisation. In `geom_rect()`, the scale
#' transformation is applied to the corners of the rectangles. In `geom_tile()`,
#' the transformation is applied only to the centres and its size is determined
#' after transformation.
#' Please note that the `width` and `height` aesthetics are not true position
#' aesthetics and therefore are not subject to scale transformation. It is
#' only after transformation that these aesthetics are applied.
#'
#' @examples
#' # The most common use for rectangles is to draw a surface. You always want
Expand Down
23 changes: 12 additions & 11 deletions man/geom_tile.Rd

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

36 changes: 36 additions & 0 deletions tests/testthat/test-geom-rect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
test_that("geom_rect can derive corners", {

corners <- c("xmin", "xmax", "ymin", "ymax")
full <- data.frame(
xmin = c(1, 2), xmax = c(3, 6),
ymin = c(1, 2), ymax = c(3, 6),
width = c(2, 4), height = c(2, 4),
x = c(2, 4), y = c(2, 4)
)

test <- full[, c("xmin", "ymin", "width", "height")]
test <- GeomRect$setup_data(test, NULL)
expect_equal(full[, corners], test[, corners])

test <- full[, c("xmin", "ymin", "x", "y")]
test <- GeomRect$setup_data(test, NULL)
expect_equal(full[, corners], test[, corners])

test <- full[, c("x", "y", "width", "height")]
test <- GeomRect$setup_data(test, NULL)
expect_equal(full[, corners], test[, corners])

test <- full[, c("xmax", "ymax", "width", "height")]
test <- GeomRect$setup_data(test, NULL)
expect_equal(full[, corners], test[, corners])

test <- full[, c("xmax", "ymax", "x", "y")]
test <- GeomRect$setup_data(test, NULL)
expect_equal(full[, corners], test[, corners])

test <- full[, c("x", "y")]
expect_error(
GeomRect$setup_data(test, NULL),
"requires the following missing aesthetics"
)
})