Skip to content

Commit 5e0473d

Browse files
committed
coord_radial() gains panel.margin argument to control the spacing between panel area and axis
1 parent 8b2a764 commit 5e0473d

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

R/coord-radial.R

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#' alignment with the coordinates.
2323
#' @param inner.radius A `numeric` between 0 and 1 setting the size of a
2424
#' inner radius hole.
25+
#' @param panel.margin A single [unit][grid::unit] object indicating the margin
26+
#' around the panel area. Reducing this value decreases the spacing between the
27+
#' panel area and the axes.
2528
#' @param reverse A string giving which directions to reverse. `"none"`
2629
#' (default) keep directions as is. `"theta"` reverses the angle and `"r"`
2730
#' reverses the radius. `"thetar"` reverses both the angle and the radius.
@@ -51,14 +54,24 @@
5154
#' thetalim = c(200, 300),
5255
#' rlim = c(15, 30),
5356
#' )
57+
#'
58+
#' # Remove the spacing between the panel area and axis
59+
#' ggplot(mtcars, aes(disp, mpg)) +
60+
#' geom_point() +
61+
#' coord_radial(
62+
#' start = -0.4 * pi,
63+
#' end = 0.4 * pi,
64+
#' panel.margin = unit(0, "mm")
65+
#' )
5466
coord_radial <- function(theta = "x",
5567
start = 0, end = NULL,
5668
thetalim = NULL, rlim = NULL, expand = TRUE,
5769
direction = deprecated(),
5870
clip = "off",
5971
r.axis.inside = NULL,
6072
rotate.angle = FALSE,
61-
inner.radius = 0,
73+
inner.radius = 0,
74+
panel.margin = NULL,
6275
reverse = "none",
6376
r_axis_inside = deprecated(),
6477
rotate_angle = deprecated()) {
@@ -93,7 +106,11 @@ coord_radial <- function(theta = "x",
93106
check_number_decimal(start, allow_infinite = FALSE)
94107
check_number_decimal(end, allow_infinite = FALSE, allow_null = TRUE)
95108
check_number_decimal(inner.radius, min = 0, max = 1, allow_infinite = FALSE)
96-
109+
if (is.null(panel.margin)) {
110+
panel.margin <- unit(0.1, "npc")
111+
} else if (length(panel.margin) != 1L || !is.unit(panel.margin)) {
112+
cli::cli_abort("{.arg panel.margin} must be a single {.fn unit}")
113+
}
97114
arc <- c(start, end %||% (start + 2 * pi))
98115
if (arc[1] > arc[2]) {
99116
n_rotate <- ((arc[1] - arc[2]) %/% (2 * pi)) + 1
@@ -103,7 +120,7 @@ coord_radial <- function(theta = "x",
103120

104121
r.axis.inside <- r.axis.inside %||% !(abs(arc[2] - arc[1]) >= 1.999 * pi)
105122

106-
inner.radius <- c(inner.radius, 1) * 0.4
123+
inner.radius <- c(inner.radius, 1) * 0.5
107124
inner.radius <- switch(reverse, thetar = , r = rev, identity)(inner.radius)
108125

109126
ggproto(NULL, CoordRadial,
@@ -116,6 +133,7 @@ coord_radial <- function(theta = "x",
116133
r_axis_inside = r.axis.inside,
117134
rotate_angle = rotate.angle,
118135
inner_radius = inner.radius,
136+
panel_margin = panel.margin,
119137
clip = clip
120138
)
121139
}
@@ -174,7 +192,8 @@ CoordRadial <- ggproto("CoordRadial", Coord,
174192
expand = params$expand[c(3, 1)]
175193
),
176194
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
177-
arc = self$arc, inner_radius = self$inner_radius)
195+
arc = self$arc, inner_radius = self$inner_radius,
196+
panel_margin = self$panel_margin)
178197
)
179198

180199
axis_rotation <- self$r_axis_inside
@@ -410,7 +429,17 @@ CoordRadial <- ggproto("CoordRadial", Coord,
410429
vp = viewport(clip = clip_path)
411430
))
412431
}
413-
ggproto_parent(Coord, self)$draw_panel(panel, params, theme)
432+
panel <- ggproto_parent(Coord, self)$draw_panel(panel, params, theme)
433+
panel_margin <- params$panel_margin
434+
vp <- viewport(
435+
width = unit(1, "npc") - panel_margin * 2L,
436+
height = unit(1, "npc") - panel_margin * 2L
437+
)
438+
if (is.null(panel$vp)) {
439+
editGrob(panel, vp = vp)
440+
} else {
441+
editGrob(panel, vp = vpStack(vp, panel$vp))
442+
}
414443
},
415444

416445
labels = function(self, labels, panel_params) {

man/coord_polar.Rd

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