|
4 | 4 | #' @param end Position from 12 o'clock in radians where plot ends, to allow
|
5 | 5 | #' for partial polar coordinates. The default, `NULL`, is set to
|
6 | 6 | #' `start + 2 * pi`.
|
| 7 | +#' @param thetalim,rlim Limits for the theta and r axes. |
7 | 8 | #' @param expand If `TRUE`, the default, adds a small expansion factor to
|
8 | 9 | #' the limits to prevent overlap between data and axes. If `FALSE`, limits
|
9 | 10 | #' are taken directly from the scale.
|
|
40 | 41 | #' ggplot(mtcars, aes(disp, mpg)) +
|
41 | 42 | #' geom_point() +
|
42 | 43 | #' coord_radial(start = -0.4 * pi, end = 0.4 * pi, inner.radius = 0.3)
|
| 44 | +#' |
| 45 | +#' # Similar with coord_cartesian(), you can set limits. |
| 46 | +#' ggplot(mtcars, aes(disp, mpg)) + |
| 47 | +#' geom_point() + |
| 48 | +#' coord_radial( |
| 49 | +#' start = -0.4 * pi, |
| 50 | +#' end = 0.4 * pi, inner.radius = 0.3, |
| 51 | +#' thetalim = c(200, 300), |
| 52 | +#' rlim = c(15, 30), |
| 53 | +#' ) |
43 | 54 | coord_radial <- function(theta = "x",
|
44 | 55 | start = 0, end = NULL,
|
45 |
| - expand = TRUE, |
| 56 | + thetalim = NULL, rlim = NULL, expand = TRUE, |
46 | 57 | direction = deprecated(),
|
47 | 58 | clip = "off",
|
48 | 59 | r.axis.inside = NULL,
|
@@ -96,6 +107,7 @@ coord_radial <- function(theta = "x",
|
96 | 107 | inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius)
|
97 | 108 |
|
98 | 109 | ggproto(NULL, CoordRadial,
|
| 110 | + limits = list(theta = thetalim, r = rlim), |
99 | 111 | theta = theta,
|
100 | 112 | r = r,
|
101 | 113 | arc = arc,
|
@@ -147,10 +159,20 @@ CoordRadial <- ggproto("CoordRadial", Coord,
|
147 | 159 | },
|
148 | 160 |
|
149 | 161 | setup_panel_params = function(self, scale_x, scale_y, params = list()) {
|
150 |
| - |
| 162 | + if (self$theta == "x") { |
| 163 | + xlimits <- self$limits$theta |
| 164 | + ylimits <- self$limits$r |
| 165 | + } else { |
| 166 | + xlimits <- self$limits$r |
| 167 | + ylimits <- self$limits$theta |
| 168 | + } |
151 | 169 | params <- c(
|
152 |
| - view_scales_polar(scale_x, self$theta, expand = params$expand[c(4, 2)]), |
153 |
| - view_scales_polar(scale_y, self$theta, expand = params$expand[c(3, 1)]), |
| 170 | + view_scales_polar(scale_x, self$theta, xlimits, |
| 171 | + expand = params$expand[c(4, 2)] |
| 172 | + ), |
| 173 | + view_scales_polar(scale_y, self$theta, ylimits, |
| 174 | + expand = params$expand[c(3, 1)] |
| 175 | + ), |
154 | 176 | list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
|
155 | 177 | arc = self$arc, inner_radius = self$inner_radius)
|
156 | 178 | )
|
@@ -454,15 +476,19 @@ CoordRadial <- ggproto("CoordRadial", Coord,
|
454 | 476 | }
|
455 | 477 | )
|
456 | 478 |
|
457 |
| -view_scales_polar <- function(scale, theta = "x", expand = TRUE) { |
| 479 | +view_scales_polar <- function(scale, theta = "x", coord_limits = NULL, |
| 480 | + expand = TRUE) { |
458 | 481 |
|
459 | 482 | aesthetic <- scale$aesthetics[1]
|
460 | 483 | is_theta <- theta == aesthetic
|
461 | 484 | name <- if (is_theta) "theta" else "r"
|
462 | 485 |
|
463 | 486 | expansion <- default_expansion(scale, expand = expand)
|
464 | 487 | limits <- scale$get_limits()
|
465 |
| - continuous_range <- expand_limits_scale(scale, expansion, limits) |
| 488 | + continuous_range <- expand_limits_scale( |
| 489 | + scale, expansion, limits, |
| 490 | + coord_limits |
| 491 | + ) |
466 | 492 |
|
467 | 493 | primary <- view_scale_primary(scale, limits, continuous_range)
|
468 | 494 | view_scales <- list(
|
|
0 commit comments