Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

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

* New `coord_cartesian(ratio)` argument that supersedes `coord_fixed()` and
`coord_equal()`.
* (internal) New `Facet$draw_panel_content()` method for delegating panel
assembly (@Yunuuuu, #6406).
* Facet gains a new method `setup_panel_params` to interact with the
Expand Down
23 changes: 20 additions & 3 deletions R/coord-cartesian-.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#' (default) keeps directions as is. `"x"` and `"y"` can be used to reverse
#' their respective directions. `"xy"` can be used to reverse both
#' directions.
#' @param ratio aspect ratio, expressed as `y / x`. Can be `NULL` (default) to
#' not use an aspect ratio. Using `1` ensures that one unit on the x-axis
#' is the same length as one unit on the y-axis. Ratios higher than one make
#' units on the y-axis longer than units on the x-axis, and vice versa.
#' @export
#' @examples
#' # There are two ways of zooming the plot display: with scales or
Expand All @@ -55,6 +59,10 @@
#' # default limits
#' p + coord_cartesian(expand = FALSE)
#'
#' # Using a fixed ratio: 1 y-axis unit is 100 x-axis units
#' # Plot window can be resized and aspect ratio will be maintained
#' p + coord_cartesian(ratio = 100)
#'
#' # You can see the same thing with this 2d histogram
#' d <- ggplot(diamonds, aes(carat, price)) +
#' stat_bin_2d(bins = 25, colour = "white")
Expand All @@ -68,15 +76,18 @@
#' # displayed bigger
#' d + coord_cartesian(xlim = c(0, 1))
coord_cartesian <- function(xlim = NULL, ylim = NULL, expand = TRUE,
default = FALSE, clip = "on", reverse = "none") {
default = FALSE, clip = "on", reverse = "none",
ratio = NULL) {
check_coord_limits(xlim)
check_coord_limits(ylim)
check_number_decimal(ratio, allow_infinite = FALSE, allow_null = TRUE)
ggproto(NULL, CoordCartesian,
limits = list(x = xlim, y = ylim),
reverse = reverse,
expand = expand,
default = default,
clip = clip
clip = clip,
ratio = ratio
)
}

Expand All @@ -87,7 +98,13 @@ coord_cartesian <- function(xlim = NULL, ylim = NULL, expand = TRUE,
CoordCartesian <- ggproto("CoordCartesian", Coord,

is_linear = function() TRUE,
is_free = function() TRUE,
is_free = function(self) is.null(self$ratio),
aspect = function(self, ranges) {
if (is.null(self$ratio)) {
return(NULL)
}
diff(ranges$y.range) / diff(ranges$x.range) * self$ratio
},

distance = function(x, y, panel_params) {
max_dist <- dist_euclidean(panel_params$x$dimension(), panel_params$y$dimension())
Expand Down
17 changes: 5 additions & 12 deletions R/coord-fixed.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#' Cartesian coordinates with fixed "aspect ratio"
#'
#' `r lifecycle::badge("superseded")` This coordinate system can be replaced by
#' using the `coord_cartesian(ratio)` argument.\cr\cr
#' A fixed scale coordinate system forces a specified ratio between the
#' physical representation of data units on the axes. The ratio represents the
#' number of units on the y-axis equivalent to one unit on the x-axis. The
Expand All @@ -10,7 +12,7 @@
#'
#' @export
#' @inheritParams coord_cartesian
#' @param ratio aspect ratio, expressed as `y / x`
#' @inheritDotParams coord_cartesian
#' @examples
#' # ensures that the ranges of axes are equal to the specified ratio by
#' # adjusting the plot aspect ratio
Expand All @@ -22,17 +24,8 @@
#' p + coord_fixed(xlim = c(15, 30))
#'
#' # Resize the plot to see that the specified aspect ratio is maintained
coord_fixed <- function(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE,
clip = "on", reverse = "none") {
check_coord_limits(xlim)
check_coord_limits(ylim)
ggproto(NULL, CoordFixed,
limits = list(x = xlim, y = ylim),
ratio = ratio,
expand = expand,
reverse = reverse,
clip = clip
)
coord_fixed <- function(ratio = 1, ...) {
coord_cartesian(ratio = ratio, ...)
}

#' @export
Expand Down
12 changes: 7 additions & 5 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Facet <- ggproto("Facet", NULL,
params
)

# Draw individual panels, then call `$draw_panels()` method to
# Draw individual panels, then call `$draw_panels()` method to
# assemble into gtable
lapply(seq_along(panels[[1]]), function(i) {
panel <- lapply(panels, `[[`, i)
Expand All @@ -185,10 +185,12 @@ Facet <- ggproto("Facet", NULL,
if (space$x && space$y) {
aspect_ratio <- aspect_ratio %||% coord$ratio
} else if (free$x || free$y) {
cli::cli_abort(
"{.fn {snake_class(self)}} can't use free scales with \\
{.fn {snake_class(coord)}}."
)
msg <- paste0("{.fn {snake_class(self)}} can't use free scales with ",
"{.fn {snake_class(coord)}}")
if (!is.null(coord$ratio)) {
msg <- paste0(msg, " with a fixed {.arg ratio} argument")
}
cli::cli_abort(paste0(msg, "."))
}
}

Expand Down
12 changes: 11 additions & 1 deletion man/coord_cartesian.Rd

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

35 changes: 19 additions & 16 deletions man/coord_fixed.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/facet-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@

---

`facet_wrap()` can't use free scales with `coord_fixed()`.
`facet_wrap()` can't use free scales with `coord_cartesian()` with a fixed `ratio` argument.

# facet_grid throws errors at bad layout specs

`facet_grid()` can't use free scales with `coord_fixed()`.
`facet_grid()` can't use free scales with `coord_cartesian()` with a fixed `ratio` argument.

---

Expand Down