Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coord_radial: add xlim and ylim arguments #6359

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @param end Position from 12 o'clock in radians where plot ends, to allow
#' for partial polar coordinates. The default, `NULL`, is set to
#' `start + 2 * pi`.
#' @param thetalim,rlim Limits for the theta and r axes.
#' @param expand If `TRUE`, the default, adds a small expansion factor to
#' the limits to prevent overlap between data and axes. If `FALSE`, limits
#' are taken directly from the scale.
Expand Down Expand Up @@ -40,9 +41,21 @@
#' ggplot(mtcars, aes(disp, mpg)) +
#' geom_point() +
#' coord_radial(start = -0.4 * pi, end = 0.4 * pi, inner.radius = 0.3)
#'
#' # Similar with coord_cartesian(), you can set limtis, but note the `clip`
#' # area is not the same with the circle track area.
#' ggplot(mtcars, aes(disp, mpg)) +
#' geom_point() +
#' coord_radial(
#' start = -0.4 * pi,
#' end = 0.4 * pi, inner.radius = 0.3,
#' thetalim = c(200, 300),
#' rlim = c(15, 30),
#' clip = "on"
#' )
coord_radial <- function(theta = "x",
start = 0, end = NULL,
expand = TRUE,
thetalim = NULL, rlim = NULL, expand = TRUE,
direction = deprecated(),
clip = "off",
r.axis.inside = NULL,
Expand Down Expand Up @@ -96,6 +109,7 @@ coord_radial <- function(theta = "x",
inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius)

ggproto(NULL, CoordRadial,
limits = list(theta = thetalim, r = rlim),
theta = theta,
r = r,
arc = arc,
Expand Down Expand Up @@ -149,8 +163,12 @@ CoordRadial <- ggproto("CoordRadial", Coord,
setup_panel_params = function(self, scale_x, scale_y, params = list()) {

params <- c(
view_scales_polar(scale_x, self$theta, expand = params$expand[c(4, 2)]),
view_scales_polar(scale_y, self$theta, expand = params$expand[c(3, 1)]),
view_scales_polar(scale_x, self$theta, self$limits$theta,
expand = params$expand[c(4, 2)]
),
view_scales_polar(scale_y, self$theta, self$limits$r,
expand = params$expand[c(3, 1)]
),
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
arc = self$arc, inner_radius = self$inner_radius)
)
Expand Down Expand Up @@ -454,15 +472,19 @@ CoordRadial <- ggproto("CoordRadial", Coord,
}
)

view_scales_polar <- function(scale, theta = "x", expand = TRUE) {
view_scales_polar <- function(scale, theta = "x", coord_limits = NULL,
expand = TRUE) {

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

expansion <- default_expansion(scale, expand = expand)
limits <- scale$get_limits()
continuous_range <- expand_limits_scale(scale, expansion, limits)
continuous_range <- expand_limits_scale(
scale, expansion, limits,
coord_limits
)

primary <- view_scale_primary(scale, limits, continuous_range)
view_scales <- list(
Expand Down
16 changes: 16 additions & 0 deletions man/coord_polar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading