Ski*_*iea 6 r ggplot2 legend-properties
我很高兴地发现我可以通过添加key_glyph = draw_key_rect到我的 geom 层来更改图例中使用的字形。我想让图例更宽更短,以类似于Timo Grossenbacher在这张地图中的图例:

我试过调整scale_fill_manual(guide = guide_legend(keyheight = unit(0.01, units = "mm") , keywidth = unit(40, units = "mm")))改变图例尺寸的方法,但似乎只有当我把字形变大时才有效。我似乎无法使键高更小。
有没有更好的方法来调整图例字形的尺寸?

此处简化代码:
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"))
Run Code Online (Sandbox Code Playgroud)
你可以创造一个虚假的传奇。或者这是另一个稍微有点hacky的解决方案。
问题似乎是 中的尺寸参数geom_point,因此您可以制作虚假的美学,特别是使用线条,例如这样:
geom_line(alpha = 0))geom_line使用与填充颜色相同的颜色制作颜色图例。override.aes)themelibrary(ggplot2)
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"))
ggplot(data = df) +
geom_point(aes(x_value, y_value, fill = value), shape = 21, size = 7,
show.legend = FALSE) + # remove fill legend
geom_line(aes(x_value, y_value, color = value),
alpha = 0, #invisible line. If your data is big, you could create a very small data frame for that
key_glyph = draw_key_rect) +
theme(legend.justification = c(0,0),
legend.position = c(0.05, 0.04),
legend.key.height = unit(0.1, 'in')) + # control height
scale_fill_manual(values = c("red", "green", "blue")) +
scale_color_manual(values = c("red", "green", "blue")) +
guides(color = guide_legend(override.aes = list(alpha = 1), # make line visible in legend
direction = "horizontal",
keywidth = unit(20, units = "mm"),
title.position = 'top',
label.position = "bottom"))
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-02-20 创建
另一种选择是删除您的参数并使用参数key_glyph控制字形高度sizegeom_line
| 归档时间: |
|
| 查看次数: |
542 次 |
| 最近记录: |