-2

I created a graph using geom_area() by group. The shape of the legend is a square. I would like it to be a line, so without any information regarding the scale I use for the fill aesthetic. How can I achieve that ?

Ps : guides(override.aes()) is not helpful since it only allows me to change the informations I put in my aesthetics, not to get ride of fill.

  • The inclusion of a reproducible example would make it easier to help you. [Link for guidance on asking questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Mar 03 '22 at 14:57
  • In what way would the legend relate to the plot if you don't want it to refer to the fill? Do you have different linetypes? – Allan Cameron Mar 03 '22 at 14:58
  • It's a graph to compare score between to measures repeated in time. The bottom line is a geom_area filled in white, the upper line is a geom_area filled in color. In that way, I got two line separated by a colored area. I only need to legend the two lines, because the white area I ploted does not convey useful information. – Kévin Legueult Mar 03 '22 at 15:04
  • 1
    @KévinLegueult perhaps you would be better with `geom_ribbon` in that case? – Allan Cameron Mar 03 '22 at 15:07

1 Answers1

1

It's not clear why you want a legend that doesn't relate to the plot, but you can change the key glyph using the key_glyph parameter. You probably want to use override.aes to change the line colour of the area to black.

library(ggplot2)

set.seed(1)

df <- data.frame(x = rep(1:5, 2), 
                 y = sample(5, 10 , TRUE),
                 group = rep(c("A", "B"), each = 5))

ggplot(df, aes(x, y, fill = group)) + 
  geom_area(key_glyph = draw_key_path) +
  guides(fill = guide_legend(override.aes = list(color = "black")))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87