How to change legend title in ggplot How to change legend title in ggplot r 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 didn't 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


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 image description here