Skip to content

Commit

Permalink
Fix linewidth of draw_key_polygon() (#5815)
Browse files Browse the repository at this point in the history
* `draw_key_polygon()` uses actual linewidth

* add news bullet
  • Loading branch information
teunbrand committed May 20, 2024
1 parent 9c4ac34 commit 89b829d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions R/legend-draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 89b829d

Please sign in to comment.