Skip to content

Commit 959bfec

Browse files
committed
make sure using the prepared r_axis_inside for all methods
1 parent 8b2a764 commit 959bfec

File tree

2 files changed

+42
-47
lines changed

2 files changed

+42
-47
lines changed

R/coord-radial.R

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
166166
xlimits <- self$limits$r
167167
ylimits <- self$limits$theta
168168
}
169-
params <- c(
169+
panel_params <- c(
170170
view_scales_polar(scale_x, self$theta, xlimits,
171171
expand = params$expand[c(4, 2)]
172172
),
@@ -177,21 +177,39 @@ CoordRadial <- ggproto("CoordRadial", Coord,
177177
arc = self$arc, inner_radius = self$inner_radius)
178178
)
179179

180-
axis_rotation <- self$r_axis_inside
180+
panel_params$r_axis_inside <- self$r_axis_inside
181+
if (isFALSE(self$r_axis_inside)) {
182+
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
183+
if (!any(place)) {
184+
cli::cli_warn(c(
185+
"No appropriate placement found for {.arg r_axis_inside}.",
186+
i = "Axis will be placed at panel edge."
187+
))
188+
panel_params$r_axis_inside <- TRUE
189+
} else {
190+
panel_params$r_axis <- if (any(place[c(1, 3)])) "left" else "bottom"
191+
panel_params$fake_arc <- switch(
192+
which(place[c(1, 3, 2, 4)])[1],
193+
c(0, 2), c(1, 3), c(0.5, 2.5), c(1.5, 3.5)
194+
) * pi
195+
}
196+
}
197+
198+
axis_rotation <- panel_params$r_axis_inside
181199
if (is.numeric(axis_rotation)) {
182200
theta_scale <- switch(self$theta, x = scale_x, y = scale_y)
183201
axis_rotation <- theta_scale$transform(axis_rotation)
184-
axis_rotation <- oob_squish(axis_rotation, params$theta.range)
202+
axis_rotation <- oob_squish(axis_rotation, panel_params$theta.range)
185203
axis_rotation <- theta_rescale(
186-
axis_rotation, params$theta.range,
187-
params$arc, 1
204+
axis_rotation, panel_params$theta.range,
205+
panel_params$arc, 1
188206
)
189-
params$axis_rotation <- rep_len(axis_rotation, length.out = 2)
207+
panel_params$axis_rotation <- rep_len(axis_rotation, length.out = 2)
190208
} else {
191-
params$axis_rotation <- params$arc
209+
panel_params$axis_rotation <- panel_params$arc
192210
}
193211

194-
params
212+
panel_params
195213
},
196214

197215
setup_panel_guides = function(self, panel_params, guides, params = list()) {
@@ -229,7 +247,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
229247
opposite_r <- isTRUE(scales$r$position %in% c("bottom", "left"))
230248
}
231249

232-
if (!isFALSE(self$r_axis_inside)) {
250+
if (!isFALSE(panel_params$r_axis_inside)) {
233251

234252
r_position <- c("left", "right")
235253
# If both opposite direction and opposite position, don't flip
@@ -244,7 +262,9 @@ CoordRadial <- ggproto("CoordRadial", Coord,
244262
guide_params[["r"]]$angle <- guide_params[["r"]]$angle %|W|% arc[1]
245263
guide_params[["r.sec"]]$angle <- guide_params[["r.sec"]]$angle %|W|% arc[2]
246264
} else {
247-
r_position <- c(params$r_axis, opposite_position(params$r_axis))
265+
r_position <- c(panel_params$r_axis,
266+
opposite_position(panel_params$r_axis)
267+
)
248268
if (opposite_r) {
249269
r_position <- rev(r_position)
250270
}
@@ -285,15 +305,15 @@ CoordRadial <- ggproto("CoordRadial", Coord,
285305
scale = panel_params[t]
286306
)
287307

288-
if (!isFALSE(self$r_axis_inside)) {
308+
if (!isFALSE(panel_params$r_axis_inside)) {
289309
# For radial axis, we need to pretend that rotation starts at 0 and
290310
# the bounding box is for circles, otherwise tick positions will be
291311
# spaced too closely.
292312
mod <- list(bbox = list(x = c(0, 1), y = c(0, 1)), arc = c(0, 2 * pi))
293313
} else {
294314
# When drawing radial axis outside, we need to pretend that arcs starts
295315
# at horizontal or vertical position to have the transform work right.
296-
mod <- list(arc = params$fake_arc)
316+
mod <- list(arc = panel_params$fake_arc)
297317
}
298318
temp <- modify_list(panel_params, mod)
299319

@@ -337,14 +357,14 @@ CoordRadial <- ggproto("CoordRadial", Coord,
337357
},
338358

339359
render_axis_v = function(self, panel_params, theme) {
340-
if (!isFALSE(self$r_axis_inside)) {
360+
if (!isFALSE(panel_params$r_axis_inside)) {
341361
return(list(left = zeroGrob(), right = zeroGrob()))
342362
}
343363
CoordCartesian$render_axis_v(panel_params, theme)
344364
},
345365

346366
render_axis_h = function(self, panel_params, theme) {
347-
if (!isFALSE(self$r_axis_inside)) {
367+
if (!isFALSE(panel_params$r_axis_inside)) {
348368
return(list(top = zeroGrob(), bottom = zeroGrob()))
349369
}
350370
CoordCartesian$render_axis_h(panel_params, theme)
@@ -363,7 +383,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
363383

364384
border <- element_render(theme, "panel.border", fill = NA)
365385

366-
if (isFALSE(self$r_axis_inside)) {
386+
if (isFALSE(panel_params$r_axis_inside)) {
367387
out <- grobTree(
368388
panel_guides_grob(panel_params$guides, "theta", theme),
369389
panel_guides_grob(panel_params$guides, "theta.sec", theme),
@@ -449,30 +469,6 @@ CoordRadial <- ggproto("CoordRadial", Coord,
449469

450470
lapply(scales_x, scale_flip_position)
451471
lapply(scales_y, scale_flip_position)
452-
},
453-
454-
setup_params = function(self, data) {
455-
params <- ggproto_parent(Coord, self)$setup_params(data)
456-
if (!isFALSE(self$r_axis_inside)) {
457-
return(params)
458-
}
459-
460-
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
461-
if (!any(place)) {
462-
cli::cli_warn(c(
463-
"No appropriate placement found for {.arg r_axis_inside}.",
464-
i = "Axis will be placed at panel edge."
465-
))
466-
params$r_axis_inside <- TRUE
467-
return(params)
468-
}
469-
470-
params$r_axis <- if (any(place[c(1, 3)])) "left" else "bottom"
471-
params$fake_arc <- switch(
472-
which(place[c(1, 3, 2, 4)])[1],
473-
c(0, 2), c(1, 3), c(0.5, 2.5), c(1.5, 3.5)
474-
) * pi
475-
params
476472
}
477473
)
478474

man/coord_polar.Rd

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

0 commit comments

Comments
 (0)