22
22
# ' alignment with the coordinates.
23
23
# ' @param inner.radius A `numeric` between 0 and 1 setting the size of a
24
24
# ' 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.
25
28
# ' @param reverse A string giving which directions to reverse. `"none"`
26
29
# ' (default) keep directions as is. `"theta"` reverses the angle and `"r"`
27
30
# ' reverses the radius. `"thetar"` reverses both the angle and the radius.
51
54
# ' thetalim = c(200, 300),
52
55
# ' rlim = c(15, 30),
53
56
# ' )
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
+ # ' )
54
66
coord_radial <- function (theta = " x" ,
55
67
start = 0 , end = NULL ,
56
68
thetalim = NULL , rlim = NULL , expand = TRUE ,
57
69
direction = deprecated(),
58
70
clip = " off" ,
59
71
r.axis.inside = NULL ,
60
72
rotate.angle = FALSE ,
61
- inner.radius = 0 ,
73
+ inner.radius = 0 ,
74
+ panel.margin = NULL ,
62
75
reverse = " none" ,
63
76
r_axis_inside = deprecated(),
64
77
rotate_angle = deprecated()) {
@@ -93,7 +106,11 @@ coord_radial <- function(theta = "x",
93
106
check_number_decimal(start , allow_infinite = FALSE )
94
107
check_number_decimal(end , allow_infinite = FALSE , allow_null = TRUE )
95
108
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
+ }
97
114
arc <- c(start , end %|| % (start + 2 * pi ))
98
115
if (arc [1 ] > arc [2 ]) {
99
116
n_rotate <- ((arc [1 ] - arc [2 ]) %/% (2 * pi )) + 1
@@ -103,7 +120,7 @@ coord_radial <- function(theta = "x",
103
120
104
121
r.axis.inside <- r.axis.inside %|| % ! (abs(arc [2 ] - arc [1 ]) > = 1.999 * pi )
105
122
106
- inner.radius <- c(inner.radius , 1 ) * 0.4
123
+ inner.radius <- c(inner.radius , 1 ) * 0.5
107
124
inner.radius <- switch (reverse , thetar = , r = rev , identity )(inner.radius )
108
125
109
126
ggproto(NULL , CoordRadial ,
@@ -116,6 +133,7 @@ coord_radial <- function(theta = "x",
116
133
r_axis_inside = r.axis.inside ,
117
134
rotate_angle = rotate.angle ,
118
135
inner_radius = inner.radius ,
136
+ panel_margin = panel.margin ,
119
137
clip = clip
120
138
)
121
139
}
@@ -174,7 +192,8 @@ CoordRadial <- ggproto("CoordRadial", Coord,
174
192
expand = params $ expand [c(3 , 1 )]
175
193
),
176
194
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 )
178
197
)
179
198
180
199
axis_rotation <- self $ r_axis_inside
@@ -410,7 +429,17 @@ CoordRadial <- ggproto("CoordRadial", Coord,
410
429
vp = viewport(clip = clip_path )
411
430
))
412
431
}
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
+ }
414
443
},
415
444
416
445
labels = function (self , labels , panel_params ) {
0 commit comments