r – How to change legend title in ggplot

r – How to change legend title in ggplot

This should work:

p <- ggplot(df, aes(x=rating, fill=cond)) + 
           geom_density(alpha=.3) + 
           xlab(NEW RATING TITLE) + 
           ylab(NEW DENSITY TITLE)
p <- p + guides(fill=guide_legend(title=New Legend Title))

(or alternatively)

p + scale_fill_discrete(name = New Legend Title)

I didnt dig in much into this but because you used fill=cond in ggplot(),

 + labs(color=NEW LEGEND TITLE) 

might not have worked. However it you replace color by fill, it works!

+ labs(fill=NEW LEGEND TITLE) 

This worked for me in ggplot2_2.1.0

r – How to change legend title in ggplot

Since you have two densitys I imagine you may be wanting to set your own colours with scale_fill_manual.

If so you can do:

df <- data.frame(x=1:10,group=c(rep(a,5),rep(b,5)))

legend_title <- OMG My Title

ggplot(df, aes(x=x, fill=group)) + geom_density(alpha=.3) +   
    scale_fill_manual(legend_title,values=c(orange,red))

enter

Leave a Reply

Your email address will not be published. Required fields are marked *