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