150

I'm trying to remove the title of a legend in ggplot2:

df <- data.frame(
  g = rep(letters[1:2], 5),
  x = rnorm(10),
  y = rnorm(10)
)

library(ggplot2)
ggplot(df, aes(x, y, colour=g)) +
  geom_line(stat="identity") + 
  theme(legend.position="bottom")

enter image description here

I've seen this question and none of the solutions there seem to work for me. Most give an error about how opts is deprecated and to use theme instead. I've also tried various versions of theme(legend.title=NULL), theme(legend.title=""), theme(legend.title=element_blank), etc. Typical error messages are:

'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1)
'theme_blank' is deprecated. Use 'element_blank' instead. (Deprecated; last used in version 0.9.1)

I'm using ggplot2 for the first time since version 0.9.3 was released and I'm finding it difficult to navigate some of the changes...

zx8754
  • 52,746
  • 12
  • 114
  • 209
smillig
  • 5,073
  • 6
  • 36
  • 46
  • 7
    You can use `labs()` for this: Add the line `labs(colour = "")` to your code that produced the graph above. – Dennis Feb 09 '13 at 23:22

5 Answers5

260

You were almost there : just add theme(legend.title=element_blank())

ggplot(df, aes(x, y, colour=g)) +
  geom_line(stat="identity") + 
  theme(legend.position="bottom") +
  theme(legend.title=element_blank())

This page on Cookbook for R gives plenty of details on how to customize legends.

juba
  • 47,631
  • 14
  • 113
  • 118
  • 5
    This will remove all legend titles. For more local control, the ``guide = guide_legend()`` command works. To remove the fill legend title, but to keep the color legend title, e.g. ``scale_fill_brewer(palette = "Dark2", guide = guide_legend(title = NULL)) + scale_color_manual(values = c("blue", "white", "red"))`` – PatrickT Apr 28 '18 at 17:07
12

This works too and also demonstrates how to change the legend title:

ggplot(df, aes(x, y, colour=g)) +
  geom_line(stat="identity") + 
  theme(legend.position="bottom") +
  scale_color_discrete(name="")
Roland
  • 127,288
  • 10
  • 191
  • 288
  • 14
    This replaces the title with an empty string and therefore causes extra space between the label and the legend box, which would be visible only if the legend had a box or background of a color different from where it is positioned. So it's alright for a quick and ready approach in simple cases like ``theme_bw()`` but not the best in cases where the legend has a box around it and is positioned somewhere on the plot area (my usual approach). – PatrickT Dec 12 '14 at 17:44
  • 1
    +1 for the observation. i had the problem using two different legends and the whitespace between them that was created by the above solution. Setting `scale_color_manual(name=element_blank())+`for the lower legend solved it for me – joaoal Jul 05 '17 at 14:41
  • 1
    @joaoal, ``element_blank()`` seems to be the recommended approach. Setting ``name = NULL`` is another way. – PatrickT Apr 28 '18 at 17:11
4

Another option using labs and setting colour to NULL.

ggplot(df, aes(x, y, colour = g)) +
  geom_line(stat = "identity") +
  theme(legend.position = "bottom") +
  labs(colour = NULL)

enter image description here

mpalanco
  • 12,960
  • 2
  • 59
  • 67
4

Since you may have more than one legends in a plot, a way to selectively remove just one of the titles without leaving an empty space is to set the name argument of the scale_ function to NULL, i.e.

scale_fill_discrete(name = NULL)

(kudos to @pascal for a comment on another thread)

vkehayas
  • 278
  • 3
  • 14
0

For Error: 'opts' is deprecated. Use theme() instead. (Defunct; last used in version 0.9.1)' I replaced opts(title = "Boxplot - Candidate's Tweet Scores") with labs(title = "Boxplot - Candidate's Tweet Scores"). It worked!

Barker
  • 2,074
  • 2
  • 17
  • 31