From d55656d46c6eb6754833688d69aecaa91b5b5eb2 Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Wed, 12 Mar 2025 20:50:29 +0800 Subject: [PATCH 1/6] coord_radial: add xlim and ylim arguments --- R/coord-radial.R | 32 +++++++++++++++++++++++++++----- man/coord_polar.Rd | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/R/coord-radial.R b/R/coord-radial.R index bc933ea1d7..2f3cc36f6f 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -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`. +#' @inheritParams coord_cartesian #' @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. @@ -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, +#' xlim = c(200, 300), +#' ylim = c(15, 30), +#' clip = "on" +#' ) coord_radial <- function(theta = "x", start = 0, end = NULL, - expand = TRUE, + xlim = NULL, ylim = NULL, expand = TRUE, direction = deprecated(), clip = "off", r.axis.inside = NULL, @@ -96,6 +109,7 @@ coord_radial <- function(theta = "x", inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius) ggproto(NULL, CoordRadial, + limits = list(x = xlim, y = ylim), theta = theta, r = r, arc = arc, @@ -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$x, + expand = params$expand[c(4, 2)] + ), + view_scales_polar(scale_y, self$theta, self$limits$y, + 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) ) @@ -454,7 +472,8 @@ 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 @@ -462,7 +481,10 @@ view_scales_polar <- function(scale, theta = "x", expand = TRUE) { 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( diff --git a/man/coord_polar.Rd b/man/coord_polar.Rd index 35d9083bfd..7dcb7151ee 100644 --- a/man/coord_polar.Rd +++ b/man/coord_polar.Rd @@ -11,6 +11,8 @@ coord_radial( theta = "x", start = 0, end = NULL, + xlim = NULL, + ylim = NULL, expand = TRUE, direction = deprecated(), clip = "off", @@ -38,6 +40,8 @@ means no. For details, please see \code{\link[=coord_cartesian]{coord_cartesian( for partial polar coordinates. The default, \code{NULL}, is set to \code{start + 2 * pi}.} +\item{xlim, ylim}{Limits for the x and y axes.} + \item{expand}{If \code{TRUE}, the default, adds a small expansion factor to the limits to prevent overlap between data and axes. If \code{FALSE}, limits are taken directly from the scale.} @@ -131,6 +135,18 @@ doh + geom_bar(width = 0.9, position = "fill") + coord_polar(theta = "y") 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, + xlim = c(200, 300), + ylim = c(15, 30), + clip = "on" + ) } \seealso{ The \href{https://ggplot2-book.org/coord#polar-coordinates-with-coord_polar}{polar coordinates section} of the online ggplot2 book. From fc4fe6746b3418f6724e6b2a40f53e45778931e8 Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Thu, 27 Mar 2025 02:52:21 +0800 Subject: [PATCH 2/6] renmae xlim and ylim to thetalim and rlim respectively --- R/coord-radial.R | 14 +++++++------- man/coord_polar.Rd | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/R/coord-radial.R b/R/coord-radial.R index 2f3cc36f6f..d8e5f2700b 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -4,7 +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`. -#' @inheritParams coord_cartesian +#' @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. @@ -49,13 +49,13 @@ #' coord_radial( #' start = -0.4 * pi, #' end = 0.4 * pi, inner.radius = 0.3, -#' xlim = c(200, 300), -#' ylim = c(15, 30), +#' thetalim = c(200, 300), +#' rlim = c(15, 30), #' clip = "on" #' ) coord_radial <- function(theta = "x", start = 0, end = NULL, - xlim = NULL, ylim = NULL, expand = TRUE, + thetalim = NULL, rlim = NULL, expand = TRUE, direction = deprecated(), clip = "off", r.axis.inside = NULL, @@ -109,7 +109,7 @@ coord_radial <- function(theta = "x", inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius) ggproto(NULL, CoordRadial, - limits = list(x = xlim, y = ylim), + limits = list(theta = thetalim, r = rlim), theta = theta, r = r, arc = arc, @@ -163,10 +163,10 @@ CoordRadial <- ggproto("CoordRadial", Coord, setup_panel_params = function(self, scale_x, scale_y, params = list()) { params <- c( - view_scales_polar(scale_x, self$theta, self$limits$x, + 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$y, + 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), diff --git a/man/coord_polar.Rd b/man/coord_polar.Rd index 7dcb7151ee..01a3ede023 100644 --- a/man/coord_polar.Rd +++ b/man/coord_polar.Rd @@ -11,8 +11,8 @@ coord_radial( theta = "x", start = 0, end = NULL, - xlim = NULL, - ylim = NULL, + thetalim = NULL, + rlim = NULL, expand = TRUE, direction = deprecated(), clip = "off", @@ -40,7 +40,7 @@ means no. For details, please see \code{\link[=coord_cartesian]{coord_cartesian( for partial polar coordinates. The default, \code{NULL}, is set to \code{start + 2 * pi}.} -\item{xlim, ylim}{Limits for the x and y axes.} +\item{thetalim, rlim}{Limits for the theta and r axes.} \item{expand}{If \code{TRUE}, the default, adds a small expansion factor to the limits to prevent overlap between data and axes. If \code{FALSE}, limits @@ -143,8 +143,8 @@ ggplot(mtcars, aes(disp, mpg)) + coord_radial( start = -0.4 * pi, end = 0.4 * pi, inner.radius = 0.3, - xlim = c(200, 300), - ylim = c(15, 30), + thetalim = c(200, 300), + rlim = c(15, 30), clip = "on" ) } From cf9fab3b4fa213181363bc1567fa2ba16947748c Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Thu, 3 Apr 2025 17:20:14 +0800 Subject: [PATCH 3/6] CoordRadial: fix wrong coord_limits when view scales --- R/coord-radial.R | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/R/coord-radial.R b/R/coord-radial.R index d8e5f2700b..e0796d344d 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -161,12 +161,18 @@ CoordRadial <- ggproto("CoordRadial", Coord, }, setup_panel_params = function(self, scale_x, scale_y, params = list()) { - + if (self$theta == "x") { + xlimits <- self$limits$theta + ylimits <- self$limits$r + } else { + xlimits <- self$limits$r + ylimits <- self$limits$theta + } params <- c( - view_scales_polar(scale_x, self$theta, self$limits$theta, + view_scales_polar(scale_x, self$theta, xlimits, expand = params$expand[c(4, 2)] ), - view_scales_polar(scale_y, self$theta, self$limits$r, + view_scales_polar(scale_y, self$theta, ylimits, expand = params$expand[c(3, 1)] ), list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius), From 9cc38c80ec2dc43ef8706451cf54b8721f6441e6 Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Thu, 3 Apr 2025 22:25:33 +0800 Subject: [PATCH 4/6] Update R/coord-radial.R Co-authored-by: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> --- R/coord-radial.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/coord-radial.R b/R/coord-radial.R index e0796d344d..ca2e934ca3 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -42,7 +42,7 @@ #' 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` +#' # Similar with coord_cartesian(), you can set limits, but note the `clip` #' # area is not the same with the circle track area. #' ggplot(mtcars, aes(disp, mpg)) + #' geom_point() + From cc3f5ed2935f2ab687cf05d7e687553227eea52a Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Sat, 5 Apr 2025 13:25:51 +0800 Subject: [PATCH 5/6] fix code ntote --- R/coord-radial.R | 5 ++--- man/coord_polar.Rd | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/R/coord-radial.R b/R/coord-radial.R index ca2e934ca3..32003a39bb 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -41,9 +41,8 @@ #' 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 limits, but note the `clip` -#' # area is not the same with the circle track area. +#' +#' # Similar with coord_cartesian(), you can set limits. #' ggplot(mtcars, aes(disp, mpg)) + #' geom_point() + #' coord_radial( diff --git a/man/coord_polar.Rd b/man/coord_polar.Rd index 01a3ede023..23bbcb97ea 100644 --- a/man/coord_polar.Rd +++ b/man/coord_polar.Rd @@ -136,8 +136,7 @@ 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. +# Similar with coord_cartesian(), you can set limits. ggplot(mtcars, aes(disp, mpg)) + geom_point() + coord_radial( From 9e54e38265830c8b46ba0c16c43da1329c22fe4f Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Mon, 7 Apr 2025 18:25:22 +0800 Subject: [PATCH 6/6] Format codes Co-authored-by: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> --- R/coord-radial.R | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/R/coord-radial.R b/R/coord-radial.R index 32003a39bb..8df50bcb1e 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -44,14 +44,13 @@ #' #' # Similar with coord_cartesian(), you can set limits. #' 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" -#' ) +#' geom_point() + +#' coord_radial( +#' start = -0.4 * pi, +#' end = 0.4 * pi, inner.radius = 0.3, +#' thetalim = c(200, 300), +#' rlim = c(15, 30), +#' ) coord_radial <- function(theta = "x", start = 0, end = NULL, thetalim = NULL, rlim = NULL, expand = TRUE,