|
1 | 1 | #' Cartesian coordinates with x and y flipped
|
2 | 2 | #'
|
3 |
| -#' Flip cartesian coordinates so that horizontal becomes vertical, and |
4 |
| -#' vertical, horizontal. This is primarily useful for converting geoms and |
5 |
| -#' statistics which display y conditional on x, to x conditional on y. |
| 3 | +#' @description |
| 4 | +#' `r lifecycle::badge("superseded")` |
| 5 | +#' |
| 6 | +#' This function is superseded because in many cases, `coord_flip()` can easily |
| 7 | +#' be replaced by swapping the x and y aesthetics, or optionally setting the |
| 8 | +#' `orientation` argument in geom and stat layers. |
| 9 | +#' |
| 10 | +#' `coord_flip()` is useful for geoms and statistics that do not support |
| 11 | +#' the `orientation` setting, and converting the display of y conditional on x, |
| 12 | +#' to x conditional on y. |
6 | 13 | #'
|
7 | 14 | #' @export
|
8 | 15 | #' @inheritParams coord_cartesian
|
9 | 16 | #' @examples
|
10 |
| -#' # Very useful for creating boxplots, and other interval |
11 |
| -#' # geoms in the horizontal instead of vertical position. |
| 17 | +#' # The preferred method of creating horizontal instead of vertical boxplots |
| 18 | +#' ggplot(diamonds, aes(price, cut)) + |
| 19 | +#' geom_boxplot() |
12 | 20 | #'
|
| 21 | +#' # Using `coord_flip()` to make the same plot |
13 | 22 | #' ggplot(diamonds, aes(cut, price)) +
|
14 | 23 | #' geom_boxplot() +
|
15 | 24 | #' coord_flip()
|
16 | 25 | #'
|
17 |
| -#' h <- ggplot(diamonds, aes(carat)) + |
18 |
| -#' geom_histogram() |
19 |
| -#' h |
20 |
| -#' h + coord_flip() |
21 |
| -#' h + coord_flip() + scale_x_reverse() |
| 26 | +#' # With swapped aesthetics, the y-scale controls the left axis |
| 27 | +#' ggplot(diamonds, aes(y = carat)) + |
| 28 | +#' geom_histogram() + |
| 29 | +#' scale_y_reverse() |
| 30 | +#' |
| 31 | +#' # In `coord_flip()`, the x-scale controls the left axis |
| 32 | +#' ggplot(diamonds, aes(carat)) + |
| 33 | +#' geom_histogram() + |
| 34 | +#' coord_flip() + |
| 35 | +#' scale_x_reverse() |
22 | 36 | #'
|
23 |
| -#' # You can also use it to flip line and area plots: |
24 |
| -#' df <- data.frame(x = 1:5, y = (1:5) ^ 2) |
25 |
| -#' ggplot(df, aes(x, y)) + |
26 |
| -#' geom_area() |
27 |
| -#' last_plot() + coord_flip() |
| 37 | +#' # In line and area plots, swapped aesthetics require an explicit orientation |
| 38 | +#' df <- data.frame(a = 1:5, b = (1:5) ^ 2) |
| 39 | +#' ggplot(df, aes(b, a)) + |
| 40 | +#' geom_area(orientation = "y") |
| 41 | +#' |
| 42 | +#' # The same plot with `coord_flip()` |
| 43 | +#' ggplot(df, aes(a, b)) + |
| 44 | +#' geom_area() + |
| 45 | +#' coord_flip() |
28 | 46 | coord_flip <- function(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") {
|
29 | 47 | ggproto(NULL, CoordFlip,
|
30 | 48 | limits = list(x = xlim, y = ylim),
|
|
0 commit comments