Skip to content

Commit 7fb5760

Browse files
authored
New coord_cartesian(ratio) argument (#6450)
* incorporate `coord_cartesian(ratio)` * simplify `coord_fixed()` * improve information in error message * adjust documentation * add example * add news bullet * Don't officially supersede `coord_fixed()`/`coord_equal()`
1 parent d16c067 commit 7fb5760

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* New `coord_cartesian(ratio)` argument that absorbs the aspect ratio
4+
functionality from `coord_equal()` and `coord_fixed()`, which are now
5+
wrappers for `coord_cartesian()`.
36
* Better handling of the `guide_axis_logticks(negative.small)` parameter when
47
scale limits have small maximum (@teunbrand, #6121).
58
* Fixed bug where the `ggplot2::`-prefix did not work with `stage()`

R/coord-cartesian-.R

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#' (default) keeps directions as is. `"x"` and `"y"` can be used to reverse
3030
#' their respective directions. `"xy"` can be used to reverse both
3131
#' directions.
32+
#' @param ratio aspect ratio, expressed as `y / x`. Can be `NULL` (default) to
33+
#' not use an aspect ratio. Using `1` ensures that one unit on the x-axis
34+
#' is the same length as one unit on the y-axis. Ratios higher than one make
35+
#' units on the y-axis longer than units on the x-axis, and vice versa.
3236
#' @export
3337
#' @examples
3438
#' # There are two ways of zooming the plot display: with scales or
@@ -55,6 +59,10 @@
5559
#' # default limits
5660
#' p + coord_cartesian(expand = FALSE)
5761
#'
62+
#' # Using a fixed ratio: 1 y-axis unit is 100 x-axis units
63+
#' # Plot window can be resized and aspect ratio will be maintained
64+
#' p + coord_cartesian(ratio = 100)
65+
#'
5866
#' # You can see the same thing with this 2d histogram
5967
#' d <- ggplot(diamonds, aes(carat, price)) +
6068
#' stat_bin_2d(bins = 25, colour = "white")
@@ -68,15 +76,18 @@
6876
#' # displayed bigger
6977
#' d + coord_cartesian(xlim = c(0, 1))
7078
coord_cartesian <- function(xlim = NULL, ylim = NULL, expand = TRUE,
71-
default = FALSE, clip = "on", reverse = "none") {
79+
default = FALSE, clip = "on", reverse = "none",
80+
ratio = NULL) {
7281
check_coord_limits(xlim)
7382
check_coord_limits(ylim)
83+
check_number_decimal(ratio, allow_infinite = FALSE, allow_null = TRUE)
7484
ggproto(NULL, CoordCartesian,
7585
limits = list(x = xlim, y = ylim),
7686
reverse = reverse,
7787
expand = expand,
7888
default = default,
79-
clip = clip
89+
clip = clip,
90+
ratio = ratio
8091
)
8192
}
8293

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

89100
is_linear = function() TRUE,
90-
is_free = function() TRUE,
101+
is_free = function(self) is.null(self$ratio),
102+
aspect = function(self, ranges) {
103+
if (is.null(self$ratio)) {
104+
return(NULL)
105+
}
106+
diff(ranges$y.range) / diff(ranges$x.range) * self$ratio
107+
},
91108

92109
distance = function(x, y, panel_params) {
93110
max_dist <- dist_euclidean(panel_params$x$dimension(), panel_params$y$dimension())

R/coord-fixed.R

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#'
1111
#' @export
1212
#' @inheritParams coord_cartesian
13-
#' @param ratio aspect ratio, expressed as `y / x`
13+
#' @inheritDotParams coord_cartesian
1414
#' @examples
1515
#' # ensures that the ranges of axes are equal to the specified ratio by
1616
#' # adjusting the plot aspect ratio
@@ -22,17 +22,8 @@
2222
#' p + coord_fixed(xlim = c(15, 30))
2323
#'
2424
#' # Resize the plot to see that the specified aspect ratio is maintained
25-
coord_fixed <- function(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE,
26-
clip = "on", reverse = "none") {
27-
check_coord_limits(xlim)
28-
check_coord_limits(ylim)
29-
ggproto(NULL, CoordFixed,
30-
limits = list(x = xlim, y = ylim),
31-
ratio = ratio,
32-
expand = expand,
33-
reverse = reverse,
34-
clip = clip
35-
)
25+
coord_fixed <- function(ratio = 1, ...) {
26+
coord_cartesian(ratio = ratio, ...)
3627
}
3728

3829
#' @export

R/facet-.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,12 @@ Facet <- ggproto("Facet", NULL,
513513
if (space$x && space$y) {
514514
aspect_ratio <- aspect_ratio %||% coord$ratio
515515
} else if (free$x || free$y) {
516-
cli::cli_abort(
517-
"{.fn {snake_class(self)}} can't use free scales with \\
518-
{.fn {snake_class(coord)}}."
519-
)
516+
msg <- paste0("{.fn {snake_class(self)}} can't use free scales with ",
517+
"{.fn {snake_class(coord)}}")
518+
if (!is.null(coord$ratio)) {
519+
msg <- paste0(msg, " with a fixed {.arg ratio} argument")
520+
}
521+
cli::cli_abort(paste0(msg, "."))
520522
}
521523
}
522524

man/coord_cartesian.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/coord_fixed.Rd

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/facet-layout.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333

3434
---
3535

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

3838
# facet_grid throws errors at bad layout specs
3939

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

4242
---
4343

0 commit comments

Comments
 (0)