Skip to content

Commit 8fd98f6

Browse files
committed
Merge branch 'main' into fancy_ggproto_docs
2 parents 9d48cc9 + 5b0ea0b commit 8fd98f6

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ggplot2 (development version)
22

3+
* Facet gains a new method `setup_panel_params` to interact with the panel_params setted by Coord object (@Yunuuuu, #6397, #6380)
34
* `position_fill()` avoids stacking observations of zero (@teunbrand, #6338)
45
* New `layer(layout)` argument to interact with facets (@teunbrand, #3062)
56
* New `stat_connect()` to connect points via steps or other shapes

R/coord-radial.R

Lines changed: 32 additions & 6 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+
#' @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(

R/facet-.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ NULL
4646
#' the default behaviour of one or more of the following methods:
4747
#'
4848
#' - `setup_params`:
49+
#'
50+
#' - `setup_panel_params`: modifies the x and y ranges for each panel. This is
51+
#' used to allow the `Facet` to interact with the `panel_params`.
52+
#'
4953
#' - `init_scales`: Given a master scale for x and y, create panel
5054
#' specific scales for each panel defined in the layout. The default is to
5155
#' simply clone the master scale.
@@ -90,6 +94,7 @@ Facet <- ggproto("Facet", NULL,
9094
map_data = function(data, layout, params) {
9195
cli::cli_abort("Not implemented.")
9296
},
97+
setup_panel_params = function(self, panel_params, coord, ...) panel_params,
9398
init_scales = function(layout, x_scale = NULL, y_scale = NULL, params) {
9499
scales <- list()
95100
if (!is.null(x_scale)) {

R/layout.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,15 @@ Layout <- ggproto("Layout", NULL,
210210
scales_x <- self$panel_scales_x[self$layout$SCALE_X[index]]
211211
scales_y <- self$panel_scales_y[self$layout$SCALE_Y[index]]
212212

213-
self$panel_params <- Map(
213+
panel_params <- Map(
214214
self$coord$setup_panel_params,
215215
scales_x, scales_y,
216216
MoreArgs = list(params = self$coord_params)
217217
)[order] # `[order]` does the repeating
218218

219+
# Let Facet modify `panel_params` for each panel
220+
self$panel_params <- self$facet$setup_panel_params(panel_params, self$coord)
221+
219222
invisible()
220223
},
221224

man/coord_polar.Rd

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

man/ggplot2-ggproto.Rd

Lines changed: 2 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)