Skip to content

Commit 8b2a764

Browse files
Yunuuuuteunbrand
andauthored
coord_radial: add xlim and ylim arguments (#6359)
* coord_radial: add xlim and ylim arguments * renmae xlim and ylim to thetalim and rlim respectively * CoordRadial: fix wrong coord_limits when view scales * Update R/coord-radial.R Co-authored-by: Teun van den Brand <[email protected]> * fix code ntote * Format codes Co-authored-by: Teun van den Brand <[email protected]> --------- Co-authored-by: Teun van den Brand <[email protected]>
1 parent 183e7ad commit 8b2a764

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

Diff for: R/coord-radial.R

+32-6
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+
#' @param thetalim,rlim Limits for the theta and r axes.
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,19 @@
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 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+
#' )
4354
coord_radial <- function(theta = "x",
4455
start = 0, end = NULL,
45-
expand = TRUE,
56+
thetalim = NULL, rlim = NULL, expand = TRUE,
4657
direction = deprecated(),
4758
clip = "off",
4859
r.axis.inside = NULL,
@@ -96,6 +107,7 @@ coord_radial <- function(theta = "x",
96107
inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius)
97108

98109
ggproto(NULL, CoordRadial,
110+
limits = list(theta = thetalim, r = rlim),
99111
theta = theta,
100112
r = r,
101113
arc = arc,
@@ -147,10 +159,20 @@ CoordRadial <- ggproto("CoordRadial", Coord,
147159
},
148160

149161
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+
}
151169
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+
),
154176
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
155177
arc = self$arc, inner_radius = self$inner_radius)
156178
)
@@ -454,15 +476,19 @@ CoordRadial <- ggproto("CoordRadial", Coord,
454476
}
455477
)
456478

457-
view_scales_polar <- function(scale, theta = "x", expand = TRUE) {
479+
view_scales_polar <- function(scale, theta = "x", coord_limits = NULL,
480+
expand = TRUE) {
458481

459482
aesthetic <- scale$aesthetics[1]
460483
is_theta <- theta == aesthetic
461484
name <- if (is_theta) "theta" else "r"
462485

463486
expansion <- default_expansion(scale, expand = expand)
464487
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+
)
466492

467493
primary <- view_scale_primary(scale, limits, continuous_range)
468494
view_scales <- list(

Diff for: man/coord_polar.Rd

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)