Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use override.aes to change the color in a legend?

I want to remove the color line from a fill legend for a ggplot. I usually use guide_legend(override.aes = ...) to modify legend aesthetics - works great for points, lines, alpha, etc., but it's not working for my color aesthetic. What am I doing wrong?

# generate data
set.seed(47)
data = data.frame(year = rep(2000:2004, 3),
                  value = runif(15),
                  group = rep(c("A", "B", "C"), each = 5))

# create the plot
p = ggplot(data, aes(x = year, y = value, fill = group)) +
    geom_area(position = position_fill(), color = "white") +
    scale_fill_grey()

# this should modify the fill legend to remove the colored line
# but the line is still there
p + guides(fill = guide_legend(override.aes = list(color = NA)))

enter image description here

like image 998
Gregor Thomas Avatar asked Nov 13 '15 23:11

Gregor Thomas


People also ask

How do I change the legend color in ggplot2?

To change the color legend element border using ggplot2, we can use theme function where can put color in legend. key argument to desired color with element_rect.

How to change legend shape in ggplot2?

Now if we want to change only Legend shape then we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take parameter named 'color' because we use color parameter for legend in ggplot() function. 'color' has call to guide_legend() guide function as value.

How do I get rid of Alpha legend ggplot2?

Example 1: Remove All Legends in ggplot2 We simply had to specify legend. position = “none” within the theme options to get rid of both legends.


1 Answers

This was one of the cases where colour needed to be spelt with u. Adding the u made override.aes work just fine. ggplot2 releases since 2016 have fixed this bug, and you can use either spelling.

p + guides(fill = guide_legend(override.aes = list(colour = NA)))

enter image description here

like image 108
Gregor Thomas Avatar answered Sep 19 '22 08:09

Gregor Thomas