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.
* `scale_{x/y}_discrete(continuous.limits)` is a new argument to control the
display range of discrete scales (@teunbrand, #4174, #6259).
* `geom_ribbon()` now appropriately warns about, and removes, missing values
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,7 +2,14 @@
#' @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 = "identity",
...,
Expand All @@ -11,7 +18,11 @@
nudge_y = 0,
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,
Expand All @@ -27,6 +38,12 @@
position <- position_nudge(nudge_x, nudge_y)
}

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 44 in R/geom-label.R

View check run for this annotation

Codecov / codecov/patch

R/geom-label.R#L43-L44

Added lines #L43 - L44 were not covered by tests
}

layer(
data = data,
mapping = mapping,
Expand All @@ -39,10 +56,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 @@ -61,14 +79,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 @@ -83,6 +104,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 @@ -94,16 +121,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 @@ -321,7 +321,11 @@
nudge_y = 0,
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,
Expand All @@ -338,6 +342,12 @@
position <- position_nudge(nudge_x, nudge_y)
}

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 348 in R/geom-sf.R

View check run for this annotation

Codecov / codecov/patch

R/geom-sf.R#L347-L348

Added lines #L347 - L348 were not covered by tests
}

layer_sf(
data = data,
mapping = mapping,
Expand All @@ -350,10 +360,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 @@ -332,7 +332,6 @@
#' @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 @@ -342,6 +341,7 @@
face = data$fontface %||% 1,
size = data$size %||% 3.88
)
lwd <- data$linewidth %||% 0.25

Check warning on line 344 in R/legend-draw.R

View check run for this annotation

Codecov / codecov/patch

R/legend-draw.R#L344

Added line #L344 was not covered by tests
grob <- labelGrob(
data$label,
x = unit(just$hjust, "npc"),
Expand All @@ -351,15 +351,16 @@
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",

Check warning on line 354 in R/legend-draw.R

View check run for this annotation

Codecov / codecov/patch

R/legend-draw.R#L354

Added line #L354 was not covered by tests
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",

Check warning on line 360 in R/legend-draw.R

View check run for this annotation

Codecov / codecov/patch

R/legend-draw.R#L360

Added line #L360 was not covered by tests
fill = alpha(data$fill %||% "white", data$alpha),
lwd = params$label.size
lwd = lwd,
lty = data$linetype %||% 1L

Check warning on line 363 in R/legend-draw.R

View check run for this annotation

Codecov / codecov/patch

R/legend-draw.R#L362-L363

Added lines #L362 - L363 were not covered by tests
)
)
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