Home > Software engineering >  Geom_point : when using color and alpha : how to delete the alpha legend in ggplot?
Geom_point : when using color and alpha : how to delete the alpha legend in ggplot?

Time:01-20

I have this df that is visualized as followed:

df = data.frame(x = -10:11,
                y = seq(-5,5.5,by=0.5),
                z = rep(c(1,2),11) )

df

ggplot(df) aes(x=x,y=y,color=factor(z),alpha=z) 
  geom_point() 
  geom_hline(yintercept=0) 
  geom_vline(xintercept = 0) 
  theme_blank()

The output: enter image description here

I checked all similar questions but nothing has worked out to remove the alpha legend. Thanks.

CodePudding user response:

You could add in alpha in aesthetics and use guides to remove it like this:

df = data.frame(x = -10:11,
                y = seq(-5,5.5,by=0.5),
                z = rep(c(1,2),11) )
library(ggplot2)

ggplot(df) aes(x=x,y=y,color=factor(z), alpha=z) 
  geom_point() 
  geom_hline(yintercept=0) 
  geom_vline(xintercept = 0)  
  guides(alpha = "none")

Created on 2023-01-20 with reprex v2.0.2

  • Related