I was thrilled to discover that I can change the glyph used in the legend by adding key_glyph = draw_key_rect
to my geom layer. I want to make the legend wider and shorter to resemble the legend in this map by Timo Grossenbacher:
I've tried adjusting scale_fill_manual(guide = guide_legend(keyheight = unit(0.01, units = "mm") , keywidth = unit(40, units = "mm")))
which changes the dimensions of the legend, but only seems to work when I make the glyphs bigger. I can't seem to make the keyheight any smaller.
Is there a better method of adjusting the legend glyphs' dimensions?
Simplified code here:
df <- data_frame(x_value = c(1:10),
y_value = c(rev(1:10)),
value = c("a","a","a","a","b","b","b","b","c","c"))
library(ggplot2)
ggplot(data = df) +
geom_point(aes(x_value, y_value, fill = value),
shape = 21,
size = 9,
key_glyph = draw_key_rect) +
theme(legend.justification = c(0,0), # set which corner of legend legen.position references
legend.position = c(0.05, 0.04)) +
scale_fill_manual(values = c("red", "green", "blue"),
guide = guide_legend(direction = "horizontal",
keyheight = unit(0.01, units = "mm"),
keywidth = unit(40, units = "mm"),
title.position = 'top',
label.position = "bottom"))