I have the following plot like below. It was created with this command:
library(ggplot2)
df <- data.frame(cond = factor(rep(c("A", "B"), each = 200)),
rating = c(rnorm(200), rnorm(200, mean=.8)))
ggplot(df, aes(x=rating, fill=cond)) +
geom_density(alpha = .3) +
xlab("NEW RATING TITLE") +
ylab("NEW DENSITY TITLE")
Now next thing I want to do is to modify the legend title from cond into NEW LEGEND TITLE.
So what I did is to just add the following line add the end of the above code:
+labs(colour="NEW LEGEND TITLE")
But it doesn't work. What's the right way to do it?
转载于:https://stackoverflow.com/questions/14622421/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")
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"))
There's another very simple answer which can work for some simple graphs.
Just add a call to guide_legend() into your graph.
ggplot(...) + ... + guide_legend(title="my awesome title")
As shown in the very nice ggplot docs.
If that doesn't work, you can more precisely set your guide parameters with a call to guides:
ggplot(...) + ... + guides(fill=guide_legend("my awesome title"))
You can also vary the shape/color/size by specifying these parameters for your call to guides
as well.
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
P.S. I would have commented but my stackoverflow reputation is not enough to comment.
Since in your code you used ggplot(data, fill= cond)
to create the histogram you need to add the legend title by also using "fill" in the label section i.e. +labs(fill="Title name")
. If you were using a different type of plot where the code was ggplot(data, colour= cond), then you could use +labs(colour= "Title Name")
. In summary, the lab argument has to match the aes argument.
I have used + guides(fill=guide_legend("my awesome title"))
to change the legend title on geom_bar plots but it did not seem to work for geom_point.
I am using a facet_wrap in my ggplot and none of the suggested solutions worked for me except ArnaudA's solution:
qplot(…) + guides(color=guide_legend(title="sale year"))
You can also add a single line (last line):
ggplot(df, aes(x=rating, fill=cond)) +
geom_density(alpha = .3) +
xlab("NEW RATING TITLE") +
ylab("NEW DENSITY TITLE") +
labs(fill = "Your Title")
None of the above code worked for me.
Here's what I found and it worked.
labs(color = "sale year")
You can also give a space between the title and the display by adding \n
at the end.
labs(color = 'sale year\n")