Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

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...

1 Answer

0 votes
by

To remove legend title in ggplot, you can add the following line to your plot:

theme(legend.title=element_blank())

i.e.,

ggplot(df, aes(x, y, colour=g)) +

  geom_line(stat="identity") + 

  theme(legend.position="bottom") +

  theme(legend.title=element_blank())

Output:

image

Related questions

0 votes
2 answers
0 votes
1 answer
asked Jul 4, 2019 in R Programming by leealex956 (7.3k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...