Skip to content

Commit d55656d

Browse files
committed
coord_radial: add xlim and ylim arguments
1 parent 1800a9a commit d55656d

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

R/coord-radial.R

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#' @param end Position from 12 o'clock in radians where plot ends, to allow
55
#' for partial polar coordinates. The default, `NULL`, is set to
66
#' `start + 2 * pi`.
7+
#' @inheritParams coord_cartesian
78
#' @param expand If `TRUE`, the default, adds a small expansion factor to
89
#' the limits to prevent overlap between data and axes. If `FALSE`, limits
910
#' are taken directly from the scale.
@@ -40,9 +41,21 @@
4041
#' ggplot(mtcars, aes(disp, mpg)) +
4142
#' geom_point() +
4243
#' coord_radial(start = -0.4 * pi, end = 0.4 * pi, inner.radius = 0.3)
44+
#'
45+
#' # Similar with coord_cartesian(), you can set limtis, but note the `clip`
46+
#' # area is not the same with the circle track area.
47+
#' ggplot(mtcars, aes(disp, mpg)) +
48+
#' geom_point() +
49+
#' coord_radial(
50+
#' start = -0.4 * pi,
51+
#' end = 0.4 * pi, inner.radius = 0.3,
52+
#' xlim = c(200, 300),
53+
#' ylim = c(15, 30),
54+
#' clip = "on"
55+
#' )
4356
coord_radial <- function(theta = "x",
4457
start = 0, end = NULL,
45-
expand = TRUE,
58+
xlim = NULL, ylim = NULL, expand = TRUE,
4659
direction = deprecated(),
4760
clip = "off",
4861
r.axis.inside = NULL,
@@ -96,6 +109,7 @@ coord_radial <- function(theta = "x",
96109
inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius)
97110

98111
ggproto(NULL, CoordRadial,
112+
limits = list(x = xlim, y = ylim),
99113
theta = theta,
100114
r = r,
101115
arc = arc,
@@ -149,8 +163,12 @@ CoordRadial <- ggproto("CoordRadial", Coord,
149163
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
150164

151165
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)]),
166+
view_scales_polar(scale_x, self$theta, self$limits$x,
167+
expand = params$expand[c(4, 2)]
168+
),
169+
view_scales_polar(scale_y, self$theta, self$limits$y,
170+
expand = params$expand[c(3, 1)]
171+
),
154172
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
155173
arc = self$arc, inner_radius = self$inner_radius)
156174
)
@@ -454,15 +472,19 @@ CoordRadial <- ggproto("CoordRadial", Coord,
454472
}
455473
)
456474

457-
view_scales_polar <- function(scale, theta = "x", expand = TRUE) {
475+
view_scales_polar <- function(scale, theta = "x", coord_limits = NULL,
476+
expand = TRUE) {
458477

459478
aesthetic <- scale$aesthetics[1]
460479
is_theta <- theta == aesthetic
461480
name <- if (is_theta) "theta" else "r"
462481

463482
expansion <- default_expansion(scale, expand = expand)
464483
limits <- scale$get_limits()
465-
continuous_range <- expand_limits_scale(scale, expansion, limits)
484+
continuous_range <- expand_limits_scale(
485+
scale, expansion, limits,
486+
coord_limits
487+
)
466488

467489
primary <- view_scale_primary(scale, limits, continuous_range)
468490
view_scales <- list(

man/coord_polar.Rd

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

0 commit comments

Comments
 (0)