Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional settings for geom_label() #6307

Merged
merged 11 commits into from
Jan 28, 2025
Merged
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ggplot2 (development version)

* New parameters for `geom_label()` (@teunbrand and @steveharoz, #5365):
* The `linewidth` aesthetic is now applied and replaces the `label.size`
argument.
* The `linetype` aesthetic is now applied.
* New `border.colour` argument to set the colour of borders.
* New `text.colour` argument to set the colour of text.
* New `element_point()` and `element_polygon()` that can be given to
`theme(point, polygon)` as an extension point (@teunbrand, #6248).
* Turned off fallback for `size` to `linewidth` translation in
Expand Down
50 changes: 39 additions & 11 deletions R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@
#' @rdname geom_text
#' @param label.padding Amount of padding around label. Defaults to 0.25 lines.
#' @param label.r Radius of rounded corners. Defaults to 0.15 lines.
#' @param label.size Size of label border, in mm.
#' @param label.size `r lifecycle::badge("deprecated")` Replaced by the
#' `linewidth` aesthetic. Size of label border, in mm.
#' @param border.colour,border.color Colour of label border. When `NULL`
#' (default), the `colour` aesthetic determines the colour of the label border.
#' `border.color` is an alias for `border.colour`.
#' @param text.colour,text.color Colour of the text. When `NULL` (default), the
#' `colour` aesthetic determines the colour of the text. `text.color` is an
#' alias for `text.colour`.
geom_label <- function(mapping = NULL, data = NULL,
stat = "identity", position = "nudge",
...,
parse = FALSE,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"),
label.size = 0.25,
label.size = deprecated(),
border.colour = NULL,
border.color = NULL,
text.colour = NULL,
text.color = NULL,
size.unit = "mm",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {

extra_args <- list2(...)
if (lifecycle::is_present(label.size)) {
deprecate_warn0("3.5.0", "geom_label(label.size)", "geom_label(linewidth)")
extra_args$linewidth <- extra_args$linewidth %||% label.size

Check warning on line 32 in R/geom-label.R

View check run for this annotation

Codecov / codecov/patch

R/geom-label.R#L31-L32

Added lines #L31 - L32 were not covered by tests
}

layer(
data = data,
mapping = mapping,
Expand All @@ -27,10 +44,11 @@
parse = parse,
label.padding = label.padding,
label.r = label.r,
label.size = label.size,
size.unit = size.unit,
border.colour = border.color %||% border.colour,
text.colour = text.color %||% text.colour,
na.rm = na.rm,
...
!!!extra_args
)
)
}
Expand All @@ -49,14 +67,17 @@
size = from_theme(fontsize),
angle = 0,
hjust = 0.5, vjust = 0.5, alpha = NA, fontface = 1,
lineheight = 1.2
lineheight = 1.2,
linewidth = from_theme(borderwidth * 0.5),
linetype = from_theme(bordertype)
),

draw_panel = function(self, data, panel_params, coord, parse = FALSE,
na.rm = FALSE,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"),
label.size = 0.25,
border.colour = NULL,
text.colour = NULL,
size.unit = "mm") {
lab <- data$label
if (parse) {
Expand All @@ -71,6 +92,12 @@
}

size.unit <- resolve_text_unit(size.unit)
data$text.colour <- text.colour %||% data$colour
data$border.colour <- border.colour %||% data$colour
data$border.colour[data$linewidth == 0] <- NA
data$fill <- fill_alpha(data$fill, data$alpha)
data$size <- data$size * size.unit


grobs <- lapply(seq_len(nrow(data)), function(i) {
row <- data[i, , drop = FALSE]
Expand All @@ -82,16 +109,17 @@
r = label.r,
angle = row$angle,
text.gp = gg_par(
col = row$colour,
fontsize = row$size * size.unit,
col = row$text.colour,
fontsize = row$size,
fontfamily = row$family,
fontface = row$fontface,
lineheight = row$lineheight
),
rect.gp = gg_par(
col = if (isTRUE(all.equal(label.size, 0))) NA else row$colour,
fill = fill_alpha(row$fill, row$alpha),
lwd = label.size
col = row$border.colour,
fill = row$fill,
lwd = row$linewidth,
lty = row$linetype
)
)
})
Expand Down
17 changes: 14 additions & 3 deletions R/geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,22 @@
parse = FALSE,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"),
label.size = 0.25,
label.size = deprecated(),
border.colour = NULL,
border.color = NULL,
text.colour = NULL,
text.color = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
fun.geometry = NULL) {

extra_args <- list2(...)
if (lifecycle::is_present(label.size)) {
deprecate_warn0("3.5.0", "geom_label(label.size)", "geom_label(linewidth)")
extra_args$linewidth <- extra_args$linewidth %||% label.size

Check warning on line 333 in R/geom-sf.R

View check run for this annotation

Codecov / codecov/patch

R/geom-sf.R#L332-L333

Added lines #L332 - L333 were not covered by tests
}

layer_sf(
data = data,
mapping = mapping,
Expand All @@ -335,10 +345,11 @@
parse = parse,
label.padding = label.padding,
label.r = label.r,
label.size = label.size,
na.rm = na.rm,
fun.geometry = fun.geometry,
...
border.colour = border.color %||% border.colour,
text.colour = text.color %||% text.colour,
!!!extra_args
)
)
}
Expand Down
9 changes: 5 additions & 4 deletions R/legend-draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ draw_key_text <- function(data, params, size) {
#' @rdname draw_key
draw_key_label <- function(data, params, size) {
data <- replace_null(unclass(data), label = "a", angle = 0)
params$label.size <- params$label.size %||% 0.25
hjust <- compute_just(data$hjust %||% 0.5)
vjust <- compute_just(data$vjust %||% 0.5)
just <- rotate_just(data$angle, hjust, vjust)
Expand All @@ -338,6 +337,7 @@ draw_key_label <- function(data, params, size) {
face = data$fontface %||% 1,
size = data$size %||% 3.88
)
lwd <- data$linewidth %||% 0.25
grob <- labelGrob(
data$label,
x = unit(just$hjust, "npc"),
Expand All @@ -347,15 +347,16 @@ draw_key_label <- function(data, params, size) {
padding = padding,
r = params$label.r %||% unit(0.15, "lines"),
text.gp = gg_par(
col = data$colour %||% "black",
col = params$text.colour %||% data$colour %||% "black",
fontfamily = data$family %||% "",
fontface = data$fontface %||% 1,
fontsize = (data$size %||% 3.88) * .pt
),
rect.gp = gg_par(
col = if (isTRUE(all.equal(params$label.size, 0))) NA else data$colour,
col = if (isTRUE(all.equal(lwd, 0))) NA else params$border.colour %||% data$colour %||% "black",
fill = alpha(data$fill %||% "white", data$alpha),
lwd = params$label.size
lwd = lwd,
lty = data$linetype %||% 1L
)
)
angle <- deg2rad(data$angle %||% 0)
Expand Down
1 change: 1 addition & 0 deletions ggplot2.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: f500cb87-e0be-413f-b396-3eb022932f55

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
17 changes: 15 additions & 2 deletions man/geom_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions man/ggsf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading