Reputation: 35905
I have a geom_histogram
created with ggplot
and I need to change the legend's title.
I find a lot of links about doing it with opts
but it is deprecated now. The theme
command allows to change esthetics, but not the title of the legend itself.
Which is the way of doing it?
Upvotes: 2
Views: 1917
Reputation: 424
In general:
- scale_aes_continuous("Title")
if the variable mapped to the chosen aesthetic (color, shape, linetype, fill, etc.) is continuous,
- scale_aes_discrete("Title")
if the variable is discrete, or
- scale_aes_manual("Title", values = c(...))
if you want to supply the values yourself.
See ?scale_color_continuous
for more options
Since you didn't post a minimal example, here's mine:
data <- data.frame(
x = c(rnorm(1000), rnorm(1000, mean = 6)),
group = rep(c("a", "b"), each = 1000)
)
qplot(x = x, fill = group, data = data) +
scale_fill_discrete("New Title")
Upvotes: 0
Reputation: 78792
You haven't provided which aesthetic that's shown in the legend. I usually prefer setting the legend title manually in scale_…
calls (when necessary), but you can use labs
with the aesthetic title mapping. i.e.…
labs(color='title')
to change the title of a legend/guide that maps the color
aestheticlabs(fill='title')
to change the title of a legend/guide that maps the fill
aestheticlabs(size='title')
to change the title of a legend/guide that maps the size
aesthetic(etc for all the other ones geom_histogram
supports)
Upvotes: 1