rggplot2geom

change symbol in ggplot2 legend, with geom_line?


the question is quite straightforward and yet i can't find a solution. how do i change the legend symbol, for a geom_line(), into something that is actually readable?

i found this post which is actually the answer (i think) but can't make it work. there are some other posts (here or here) where people change shapes in legends but none seem to work with geom_line(). what am i missing?

this creates the plot and increases the length of the line in the legend but the shape is not applied. it may help even increasing the width of the line itself. but how?

# load library
library("ggplot2")

# plot
ggplot(iris, aes(x=Petal.Length, y=Sepal.Width, color=Species)) +
    geom_line() +
    guides(color=guide_legend(override.aes=list(size=50, shape = 18), ncol=2))

Solution

  • The symbol used for a geom in the legend is set by the draw_key_xxx function and could be set via the key_glyph= parameter. In case of geom_line it defaults to "path" or draw_key_path. If you want a point shape instead then use key_glyph="point":

    library(ggplot2)
    
    ggplot(iris, aes(x = Petal.Length, y = Sepal.Width, color = Species)) +
      geom_line(key_glyph = "point") +
      guides(
        color = guide_legend(
          override.aes = list(size = 5, shape = 18), ncol = 2
        )
      )