As a follow up to this famous thread. I decided to ask a new question because this is more specifically for vertical legends, and the given answers still don't provide a fully satisfactory solution. For polygon key glyphs, there was a suggestion to increase the size of the margin between the actual polygon glyph and the key border, with a modification of the underlying draw_key function. However, this doesn't really work if I want to keep a border, e.g., in black.
And when using other geoms (e.g., geom_line), I don't even see how to increase the margin between glyph and border - on a brief glance at the draw_key function used in ggplot2:::GeomLine
(it doesn't use an exported draw_key function), it is not clear to me where to modify this.
So, my question is if there is a way to change the actual space between the key glyphs that would allow to keep a border (e.g., color = "black"), and without increasing the glyph size.
library(ggplot2)
p <- ggplot(dplyr::filter(msleep, grepl("^C", order)),
aes(sleep_total, sleep_rem, color = order)) +
geom_line(na.rm = TRUE) +
theme(legend.key = element_rect(color = "black"))
p
Example plot: Key glyphs touching.
p + theme(legend.key.height = unit(.5, "in"))
Example that would look as if the glyphs are more spaced if I'd remove the color, but I want the key glyphs in the same size as above, just space between them.
The underlying draw_key in GeomLine
# ggplot2:::GeomLine$draw_key
#> ...
#> segmentsGrob(0.1, 0.5, 0.9, 0.5, gp = gpar(col = alpha(data$colour %||%
#> data$fill %||% "black", data$alpha), fill = alpha(params$arrow.fill %||%
#> data$colour %||% data$fill %||% "black", data$alpha),
#> lwd = (data$size %||% 0.5) * .pt, lty = data$linetype %||%
#> 1, lineend = "butt"), arrow = params$arrow)
#> ...
Created on 2021-12-29 by the reprex package (v2.0.1)
P.S. This question arose on answering this interesting question