diff --git a/NEWS.md b/NEWS.md index bb2730a82b..74f0a287bc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* The size of the `draw_key_polygon()` glyph now reflects the `linewidth` + aesthetic (#4852). * New function `complete_theme()` to replicate how themes are handled during plot building (#5801). * Special getter and setter functions have been renamed for consistency, allowing diff --git a/R/legend-draw.R b/R/legend-draw.R index ce16f7a6ab..8e7e8b9ae4 100644 --- a/R/legend-draw.R +++ b/R/legend-draw.R @@ -71,9 +71,9 @@ draw_key_polygon <- function(data, params, size) { data$linewidth <- 0.5 } - lwd <- min(data$linewidth, min(size) / 4) + lwd <- data$linewidth - rectGrob( + grob <- rectGrob( width = unit(1, "npc") - unit(lwd, "mm"), height = unit(1, "npc") - unit(lwd, "mm"), gp = ggpar( @@ -84,6 +84,12 @@ draw_key_polygon <- function(data, params, size) { linejoin = params$linejoin %||% "mitre", lineend = params$lineend %||% "butt" )) + + # Magic number is 5 because we convert mm to cm (divide by 10) but we + # draw two lines in each direction (times 2) + attr(grob, "width") <- lwd / 5 + attr(grob, "height") <- lwd / 5 + grob } #' @export