Don't use alpha in color guide


I would like to change the appearance of the color guide. I am using alpha = .1 in geom_line. But I don't want to use the alpha'd color in the guide - because you can hardly differentiate the colors.

(I am using all the latest package versions)


df <- tibble(y1 = rnorm(1000, 0, 1), 
                         y2 = rnorm(1000, 2, 2),
                         y3 = rnorm(1000, 4, 3), 
                         id = 1:1000, 
                         t = c("A", "B", "C")[round(runif(1000) * 2) + 1]) %>% 
    pivot_longer(cols = starts_with("y"), values_to = "y", names_to = "x") %>% 
    mutate(x = rep(1:3, 1000))

df %>% 
    ggplot() + 
    geom_line(aes(x, y, color = t, group = id), alpha = .1)

Please note, that it's even worse in the preview in RStudio.

Anyway, I would like the guide to be the same as if there would be no alpha parameter in geom_line.

Thanks in advance

Hi Seb!

Adding the following line to your ggplot code seems to do what you want:

guides(color = guide_legend(override.aes = list(alpha = 1)))

You can change the alpha values as you please, just replace the "1" by what suits you best :slight_smile:

I found this solution in @Z3tt's amazing ggplot tutorial




It does indeed! Thank you very much.

I was on the right track, but I missed override.aes bit. I did not use alpha in the aes anyway! Maybe a little bug in ggplot2?


No probs, happy to help :slight_smile:

And yeah, it's quite interesting that it works even though alpha wasn't an aes parameter! Hopefully someone else will explain why that is so :smiley:



Hi, thanks for pointing to my tutorial, always nice to hear it helps!

The override.aes argument does not only map to aesthetics but all aesthetical properties of a layer. The guide_legend() vignette says about override.aes:

A list specifying aesthetic parameters of legend key.


1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.