ggplot2:更改图例符号

时间:2013-10-18 18:39:06

标签: r graph ggplot2 legend

我已经绘制了几行,我想知道如何更改图例中的符号,从细线到完整的块。

我想从

开始

thisthis

(使用geom_line而不是geom_bar时)

2 个答案:

答案 0 :(得分:9)

您可以使用函数guides(),然后使用参数override.aes=将行size=(宽度)设置为某个较大的值。要移除fill=NAlegend.key=的{​​{1}}图例键周围的灰色区域。

theme()

enter image description here

答案 1 :(得分:1)

ggplot2_3.2.0 开始,您可以使用key_glyph中的geom_*()参数选择要在图例键中显示的字形。

例如,您想使用矩形而不是直线作为字形。在这种情况下,您可以

df = data.frame(x = rep(1:5, each=3),
                y = 1:15,
                group = rep(c("A", "B", "C"), each=5))

ggplot(df, aes(x, y, color=group) )+
    geom_line(key_glyph = "rect")

enter image description here

有关可用的当前字形的列表,请参见?draw_key