Home > Software design >  How to change legend type with ggplot2 in R?
How to change legend type with ggplot2 in R?

Time:03-04

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.

CodePudding user response:

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

  • Related