Skip to content

Commit 1bfb3c9

Browse files
authored
Additional settings for geom_label() (#6307)
* replace `geom_label(label.size)` with `linewidth` aesthetic * add `linetype` aesthetic * add border and text colours * vectorise some operations * preserve thinner linewidth * apply to `geom_sf_labels()` too * adapt legend key * document new params * add test * add news bullet
1 parent 6e0664b commit 1bfb3c9

9 files changed

+227
-22
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ggplot2 (development version)
22

3+
* New parameters for `geom_label()` (@teunbrand and @steveharoz, #5365):
4+
* The `linewidth` aesthetic is now applied and replaces the `label.size`
5+
argument.
6+
* The `linetype` aesthetic is now applied.
7+
* New `border.colour` argument to set the colour of borders.
8+
* New `text.colour` argument to set the colour of text.
39
* New `element_point()` and `element_polygon()` that can be given to
410
`theme(point, polygon)` as an extension point (@teunbrand, #6248).
511
* Turned off fallback for `size` to `linewidth` translation in

R/geom-label.R

+39-11
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,36 @@
22
#' @rdname geom_text
33
#' @param label.padding Amount of padding around label. Defaults to 0.25 lines.
44
#' @param label.r Radius of rounded corners. Defaults to 0.15 lines.
5-
#' @param label.size Size of label border, in mm.
5+
#' @param label.size `r lifecycle::badge("deprecated")` Replaced by the
6+
#' `linewidth` aesthetic. Size of label border, in mm.
7+
#' @param border.colour,border.color Colour of label border. When `NULL`
8+
#' (default), the `colour` aesthetic determines the colour of the label border.
9+
#' `border.color` is an alias for `border.colour`.
10+
#' @param text.colour,text.color Colour of the text. When `NULL` (default), the
11+
#' `colour` aesthetic determines the colour of the text. `text.color` is an
12+
#' alias for `text.colour`.
613
geom_label <- function(mapping = NULL, data = NULL,
714
stat = "identity", position = "nudge",
815
...,
916
parse = FALSE,
1017
label.padding = unit(0.25, "lines"),
1118
label.r = unit(0.15, "lines"),
12-
label.size = 0.25,
19+
label.size = deprecated(),
20+
border.colour = NULL,
21+
border.color = NULL,
22+
text.colour = NULL,
23+
text.color = NULL,
1324
size.unit = "mm",
1425
na.rm = FALSE,
1526
show.legend = NA,
1627
inherit.aes = TRUE) {
1728

29+
extra_args <- list2(...)
30+
if (lifecycle::is_present(label.size)) {
31+
deprecate_warn0("3.5.0", "geom_label(label.size)", "geom_label(linewidth)")
32+
extra_args$linewidth <- extra_args$linewidth %||% label.size
33+
}
34+
1835
layer(
1936
data = data,
2037
mapping = mapping,
@@ -27,10 +44,11 @@ geom_label <- function(mapping = NULL, data = NULL,
2744
parse = parse,
2845
label.padding = label.padding,
2946
label.r = label.r,
30-
label.size = label.size,
3147
size.unit = size.unit,
48+
border.colour = border.color %||% border.colour,
49+
text.colour = text.color %||% text.colour,
3250
na.rm = na.rm,
33-
...
51+
!!!extra_args
3452
)
3553
)
3654
}
@@ -49,14 +67,17 @@ GeomLabel <- ggproto("GeomLabel", Geom,
4967
size = from_theme(fontsize),
5068
angle = 0,
5169
hjust = 0.5, vjust = 0.5, alpha = NA, fontface = 1,
52-
lineheight = 1.2
70+
lineheight = 1.2,
71+
linewidth = from_theme(borderwidth * 0.5),
72+
linetype = from_theme(bordertype)
5373
),
5474

5575
draw_panel = function(self, data, panel_params, coord, parse = FALSE,
5676
na.rm = FALSE,
5777
label.padding = unit(0.25, "lines"),
5878
label.r = unit(0.15, "lines"),
59-
label.size = 0.25,
79+
border.colour = NULL,
80+
text.colour = NULL,
6081
size.unit = "mm") {
6182
lab <- data$label
6283
if (parse) {
@@ -71,6 +92,12 @@ GeomLabel <- ggproto("GeomLabel", Geom,
7192
}
7293

7394
size.unit <- resolve_text_unit(size.unit)
95+
data$text.colour <- text.colour %||% data$colour
96+
data$border.colour <- border.colour %||% data$colour
97+
data$border.colour[data$linewidth == 0] <- NA
98+
data$fill <- fill_alpha(data$fill, data$alpha)
99+
data$size <- data$size * size.unit
100+
74101

75102
grobs <- lapply(seq_len(nrow(data)), function(i) {
76103
row <- data[i, , drop = FALSE]
@@ -82,16 +109,17 @@ GeomLabel <- ggproto("GeomLabel", Geom,
82109
r = label.r,
83110
angle = row$angle,
84111
text.gp = gg_par(
85-
col = row$colour,
86-
fontsize = row$size * size.unit,
112+
col = row$text.colour,
113+
fontsize = row$size,
87114
fontfamily = row$family,
88115
fontface = row$fontface,
89116
lineheight = row$lineheight
90117
),
91118
rect.gp = gg_par(
92-
col = if (isTRUE(all.equal(label.size, 0))) NA else row$colour,
93-
fill = fill_alpha(row$fill, row$alpha),
94-
lwd = label.size
119+
col = row$border.colour,
120+
fill = row$fill,
121+
lwd = row$linewidth,
122+
lty = row$linetype
95123
)
96124
)
97125
})

R/geom-sf.R

+14-3
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,22 @@ geom_sf_label <- function(mapping = aes(), data = NULL,
317317
parse = FALSE,
318318
label.padding = unit(0.25, "lines"),
319319
label.r = unit(0.15, "lines"),
320-
label.size = 0.25,
320+
label.size = deprecated(),
321+
border.colour = NULL,
322+
border.color = NULL,
323+
text.colour = NULL,
324+
text.color = NULL,
321325
na.rm = FALSE,
322326
show.legend = NA,
323327
inherit.aes = TRUE,
324328
fun.geometry = NULL) {
325329

330+
extra_args <- list2(...)
331+
if (lifecycle::is_present(label.size)) {
332+
deprecate_warn0("3.5.0", "geom_label(label.size)", "geom_label(linewidth)")
333+
extra_args$linewidth <- extra_args$linewidth %||% label.size
334+
}
335+
326336
layer_sf(
327337
data = data,
328338
mapping = mapping,
@@ -335,10 +345,11 @@ geom_sf_label <- function(mapping = aes(), data = NULL,
335345
parse = parse,
336346
label.padding = label.padding,
337347
label.r = label.r,
338-
label.size = label.size,
339348
na.rm = na.rm,
340349
fun.geometry = fun.geometry,
341-
...
350+
border.colour = border.color %||% border.colour,
351+
text.colour = text.color %||% text.colour,
352+
!!!extra_args
342353
)
343354
)
344355
}

R/legend-draw.R

+5-4
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ draw_key_text <- function(data, params, size) {
328328
#' @rdname draw_key
329329
draw_key_label <- function(data, params, size) {
330330
data <- replace_null(unclass(data), label = "a", angle = 0)
331-
params$label.size <- params$label.size %||% 0.25
332331
hjust <- compute_just(data$hjust %||% 0.5)
333332
vjust <- compute_just(data$vjust %||% 0.5)
334333
just <- rotate_just(data$angle, hjust, vjust)
@@ -338,6 +337,7 @@ draw_key_label <- function(data, params, size) {
338337
face = data$fontface %||% 1,
339338
size = data$size %||% 3.88
340339
)
340+
lwd <- data$linewidth %||% 0.25
341341
grob <- labelGrob(
342342
data$label,
343343
x = unit(just$hjust, "npc"),
@@ -347,15 +347,16 @@ draw_key_label <- function(data, params, size) {
347347
padding = padding,
348348
r = params$label.r %||% unit(0.15, "lines"),
349349
text.gp = gg_par(
350-
col = data$colour %||% "black",
350+
col = params$text.colour %||% data$colour %||% "black",
351351
fontfamily = data$family %||% "",
352352
fontface = data$fontface %||% 1,
353353
fontsize = (data$size %||% 3.88) * .pt
354354
),
355355
rect.gp = gg_par(
356-
col = if (isTRUE(all.equal(params$label.size, 0))) NA else data$colour,
356+
col = if (isTRUE(all.equal(lwd, 0))) NA else params$border.colour %||% data$colour %||% "black",
357357
fill = alpha(data$fill %||% "white", data$alpha),
358-
lwd = params$label.size
358+
lwd = lwd,
359+
lty = data$linetype %||% 1L
359360
)
360361
)
361362
angle <- deg2rad(data$angle %||% 0)

ggplot2.Rproj

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: f500cb87-e0be-413f-b396-3eb022932f55
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

man/geom_text.Rd

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ggsf.Rd

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)