Helen
Helen

Reputation: 607

Why does ggplot2 not show colours in mylegend?

I'm wondering why I don't get colors in my legend:

test=data.frame(a=1:6,b=7:12,c=13:18,d=rep(c("a","b"),each=3))
  ggplot() + 
  geom_line(data=test,aes(y=a,x= b,colour=d,group=d),size=1,alpha=0.5)+
  theme(legend.position="bottom") +
  xlab("x-axis")

I don't think this has happened before... has it got anything to do with my data?

enter image description here

Upvotes: 0

Views: 28

Answers (1)

Dan
Dan

Reputation: 12074

It's because of your alpha value. You can reset it for the legend like so:

df <- data.frame(a=1:6,b=7:12,c=13:18,d=rep(c("a","b"),each=3))

library(ggplot2)

g <- ggplot()
g <- g + geom_line(data = df, aes(y = a, x = b, colour = d, group = d), 
                   size = 1, alpha = 0.5)
g <- g + theme(legend.position="bottom")
g <- g + xlab("x-axis")
g <- g + guides(colour = guide_legend(override.aes = list(alpha = 1)))

print(g)

Created on 2019-06-24 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions