Skip to content

Commit 41fba61

Browse files
committed
check r.axis.inside in the CoordRadial constructor
1 parent d91200f commit 41fba61

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

R/coord-radial.R

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ coord_radial <- function(theta = "x",
102102
arc <- switch(reverse, thetar = , theta = rev(arc), arc)
103103

104104
r.axis.inside <- r.axis.inside %||% !(abs(arc[2] - arc[1]) >= 1.999 * pi)
105+
if (isFALSE(r.axis.inside)) {
106+
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, arc)
107+
if (!any(place)) {
108+
cli::cli_warn(c(
109+
"No appropriate placement found for {.arg r.axis.inside}.",
110+
i = "Will use {.code r.axis.inside = TRUE} instead"
111+
))
112+
r.axis.inside <- TRUE
113+
}
114+
}
105115

106116
inner.radius <- c(inner.radius, 1) * 0.4
107117
inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius)
@@ -174,23 +184,10 @@ CoordRadial <- ggproto("CoordRadial", Coord,
174184
expand = params$expand[c(3, 1)]
175185
),
176186
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
177-
arc = self$arc, inner_radius = self$inner_radius,
178-
r_axis_inside = params$r_axis_inside)
187+
arc = self$arc, inner_radius = self$inner_radius)
179188
)
180189

181-
if (isFALSE(panel_params$r_axis_inside)) {
182-
if (any(params$place[c(1, 3)])) {
183-
panel_params$r_axis <- "left"
184-
} else {
185-
panel_params$r_axis <- "bottom"
186-
}
187-
panel_params$fake_arc <- switch(
188-
which(params$place[c(1, 3, 2, 4)])[1],
189-
c(0, 2), c(1, 3), c(0.5, 2.5), c(1.5, 3.5)
190-
) * pi
191-
}
192-
193-
axis_rotation <- panel_params$r_axis_inside
190+
axis_rotation <- self$r_axis_inside
194191
if (is.numeric(axis_rotation)) {
195192
theta_scale <- switch(self$theta, x = scale_x, y = scale_y)
196193
axis_rotation <- theta_scale$transform(axis_rotation)
@@ -242,7 +239,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
242239
opposite_r <- isTRUE(scales$r$position %in% c("bottom", "left"))
243240
}
244241

245-
if (!isFALSE(panel_params$r_axis_inside)) {
242+
if (!isFALSE(self$r_axis_inside)) {
246243

247244
r_position <- c("left", "right")
248245
# If both opposite direction and opposite position, don't flip
@@ -257,9 +254,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
257254
guide_params[["r"]]$angle <- guide_params[["r"]]$angle %|W|% arc[1]
258255
guide_params[["r.sec"]]$angle <- guide_params[["r.sec"]]$angle %|W|% arc[2]
259256
} else {
260-
r_position <- c(panel_params$r_axis,
261-
opposite_position(panel_params$r_axis)
262-
)
257+
r_position <- c(params$r_axis, opposite_position(params$r_axis))
263258
if (opposite_r) {
264259
r_position <- rev(r_position)
265260
}
@@ -300,15 +295,15 @@ CoordRadial <- ggproto("CoordRadial", Coord,
300295
scale = panel_params[t]
301296
)
302297

303-
if (!isFALSE(panel_params$r_axis_inside)) {
298+
if (!isFALSE(self$r_axis_inside)) {
304299
# For radial axis, we need to pretend that rotation starts at 0 and
305300
# the bounding box is for circles, otherwise tick positions will be
306301
# spaced too closely.
307302
mod <- list(bbox = list(x = c(0, 1), y = c(0, 1)), arc = c(0, 2 * pi))
308303
} else {
309304
# When drawing radial axis outside, we need to pretend that arcs starts
310305
# at horizontal or vertical position to have the transform work right.
311-
mod <- list(arc = panel_params$fake_arc)
306+
mod <- list(arc = params$fake_arc)
312307
}
313308
temp <- modify_list(panel_params, mod)
314309

@@ -352,14 +347,14 @@ CoordRadial <- ggproto("CoordRadial", Coord,
352347
},
353348

354349
render_axis_v = function(self, panel_params, theme) {
355-
if (!isFALSE(panel_params$r_axis_inside)) {
350+
if (!isFALSE(self$r_axis_inside)) {
356351
return(list(left = zeroGrob(), right = zeroGrob()))
357352
}
358353
CoordCartesian$render_axis_v(panel_params, theme)
359354
},
360355

361356
render_axis_h = function(self, panel_params, theme) {
362-
if (!isFALSE(panel_params$r_axis_inside)) {
357+
if (!isFALSE(self$r_axis_inside)) {
363358
return(list(top = zeroGrob(), bottom = zeroGrob()))
364359
}
365360
CoordCartesian$render_axis_h(panel_params, theme)
@@ -378,7 +373,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
378373

379374
border <- element_render(theme, "panel.border", fill = NA)
380375

381-
if (isFALSE(panel_params$r_axis_inside)) {
376+
if (isFALSE(self$r_axis_inside)) {
382377
out <- grobTree(
383378
panel_guides_grob(panel_params$guides, "theta", theme),
384379
panel_guides_grob(panel_params$guides, "theta.sec", theme),
@@ -468,20 +463,14 @@ CoordRadial <- ggproto("CoordRadial", Coord,
468463

469464
setup_params = function(self, data) {
470465
params <- ggproto_parent(Coord, self)$setup_params(data)
471-
params$r_axis_inside <- self$r_axis_inside
472-
if (!isFALSE(params$r_axis_inside)) {
473-
return(params)
474-
}
475-
476-
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
477-
if (!any(place)) {
478-
cli::cli_warn(c(
479-
"No appropriate placement found for {.arg r_axis_inside}.",
480-
i = "Axis will be placed at panel edge."
481-
))
482-
params$r_axis_inside <- TRUE
466+
if (isFALSE(self$r_axis_inside)) {
467+
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
468+
params$r_axis <- if (any(place[c(1, 3)])) "left" else "bottom"
469+
params$fake_arc <- switch(
470+
which(place[c(1, 3, 2, 4)])[1],
471+
c(0, 2), c(1, 3), c(0.5, 2.5), c(1.5, 3.5)
472+
) * pi
483473
}
484-
params$place <- place
485474
params
486475
}
487476
)

0 commit comments

Comments
 (0)