|
1 | | - |
2 | | -#' @rdname coord_polar |
| 1 | +#' Polar coordinates |
| 2 | +#' |
| 3 | +#' The polar coordinate system is most commonly used for pie charts, which |
| 4 | +#' are a stacked bar chart in polar coordinates. \cr |
| 5 | +#' `r lifecycle::badge("superseded")`: `coord_polar()` has been in favour of |
| 6 | +#' `coord_radial()`. |
3 | 7 | #' |
| 8 | +#' @param theta variable to map angle to (`x` or `y`) |
| 9 | +#' @param start Offset of starting point from 12 o'clock in radians. Offset |
| 10 | +#' is applied clockwise or anticlockwise depending on value of `direction`. |
| 11 | +#' @param direction 1, clockwise; -1, anticlockwise |
| 12 | +#' @param clip Should drawing be clipped to the extent of the plot panel? A |
| 13 | +#' setting of `"on"` (the default) means yes, and a setting of `"off"` |
| 14 | +#' means no. For details, please see [`coord_cartesian()`]. |
4 | 15 | #' @param end Position from 12 o'clock in radians where plot ends, to allow |
5 | 16 | #' for partial polar coordinates. The default, `NULL`, is set to |
6 | 17 | #' `start + 2 * pi`. |
|
26 | 37 | #' (default) keep directions as is. `"theta"` reverses the angle and `"r"` |
27 | 38 | #' reverses the radius. `"thetar"` reverses both the angle and the radius. |
28 | 39 | #' @param r_axis_inside,rotate_angle `r lifecycle::badge("deprecated")` |
29 | | -#' |
| 40 | +#' @export |
30 | 41 | #' @note |
31 | 42 | #' In `coord_radial()`, position guides can be defined by using |
32 | 43 | #' `guides(r = ..., theta = ..., r.sec = ..., theta.sec = ...)`. Note that |
|
35 | 46 | #' be used for the `theta` positions. Using the `theta.sec` position is only |
36 | 47 | #' sensible when `inner.radius > 0`. |
37 | 48 | #' |
38 | | -#' @export |
| 49 | +#' @seealso |
| 50 | +#' The `r link_book("polar coordinates section", "coord#polar-coordinates-with-coord_polar")` |
39 | 51 | #' @examples |
| 52 | +#' # NOTE: Use these plots with caution - polar coordinates has |
| 53 | +#' # major perceptual problems. The main point of these examples is |
| 54 | +#' # to demonstrate how these common plots can be described in the |
| 55 | +#' # grammar. Use with EXTREME caution. |
| 56 | +#' |
| 57 | +#' # A pie chart = stacked bar chart + polar coordinates |
| 58 | +#' pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) + |
| 59 | +#' geom_bar(width = 1) |
| 60 | +#' pie + coord_radial(theta = "y", expand = FALSE) |
| 61 | +#' |
| 62 | +#' \donttest{ |
| 63 | +#' |
| 64 | +#' # A coxcomb plot = bar chart + polar coordinates |
| 65 | +#' cxc <- ggplot(mtcars, aes(x = factor(cyl))) + |
| 66 | +#' geom_bar(width = 1, colour = "black") |
| 67 | +#' cxc + coord_radial(expand = FALSE) |
| 68 | +#' # A new type of plot? |
| 69 | +#' cxc + coord_radial(theta = "y", expand = FALSE) |
| 70 | +#' |
| 71 | +#' # The bullseye chart |
| 72 | +#' pie + coord_radial(expand = FALSE) |
| 73 | +#' |
| 74 | +#' # Hadley's favourite pie chart |
| 75 | +#' df <- data.frame( |
| 76 | +#' variable = c("does not resemble", "resembles"), |
| 77 | +#' value = c(20, 80) |
| 78 | +#' ) |
| 79 | +#' ggplot(df, aes(x = "", y = value, fill = variable)) + |
| 80 | +#' geom_col(width = 1) + |
| 81 | +#' scale_fill_manual(values = c("red", "yellow")) + |
| 82 | +#' coord_radial("y", start = pi / 3, expand = FALSE) + |
| 83 | +#' labs(title = "Pac man") |
| 84 | +#' |
| 85 | +#' # Windrose + doughnut plot |
| 86 | +#' if (require("ggplot2movies")) { |
| 87 | +#' movies$rrating <- cut_interval(movies$rating, length = 1) |
| 88 | +#' movies$budgetq <- cut_number(movies$budget, 4) |
| 89 | +#' |
| 90 | +#' doh <- ggplot(movies, aes(x = rrating, fill = budgetq)) |
| 91 | +#' |
| 92 | +#' # Wind rose |
| 93 | +#' doh + geom_bar(width = 1) + coord_radial(expand = FALSE) |
| 94 | +#' # Race track plot |
| 95 | +#' doh + geom_bar(width = 0.9, position = "fill") + |
| 96 | +#' coord_radial(theta = "y", expand = FALSE) |
| 97 | +#' } |
| 98 | +#' } |
40 | 99 | #' # A partial polar plot |
41 | 100 | #' ggplot(mtcars, aes(disp, mpg)) + |
42 | 101 | #' geom_point() + |
@@ -475,7 +534,7 @@ CoordRadial <- ggproto("CoordRadial", Coord, |
475 | 534 | } |
476 | 535 | ) |
477 | 536 |
|
478 | | -view_scales_polar <- function(scale, theta = "x", coord_limits = NULL, |
| 537 | +view_scales_polar <- function(scale, theta = "x", coord_limits = NULL, |
479 | 538 | expand = TRUE) { |
480 | 539 |
|
481 | 540 | aesthetic <- scale$aesthetics[1] |
|
0 commit comments