Brennan
Brennan

Reputation: 429

How to suppress legends with ggplot

I have the following ggplot code:

ggplot(Wheat_by_yearContinent, aes(x = Year, y = avgWheatYield, color = ContinentName)) + 
  theme(legend.position = "none") + 
  geom_line(size = 1) + 
  expand_limits(y = 0) + 
  ggtitle("Wheat") + 
  ylab(bquote('Average Yield ( '*'ton'~ ha^-1*')'))

and I have tried a few ways to suppress the legend but can't seem to figure it out and nothing I have tried is doing anything. Has anyone encountered this problem/know how to do this? I have included + theme(legend.position = "none") in the above code as this was my final attempt, I also tried legend = FALSE and nothing there either.

Upvotes: 6

Views: 9927

Answers (1)

Will M
Will M

Reputation: 812

Have you tried some of the these?

Remove legend from a particular aesthetic:

plot + guides(colour=FALSE)

EDIT Note that the <scale> arguments of guides() (colour=, fill=, etc.) take "none" instead of FALSE since ggplot2 Version 3.3.4.

or

Remove legend inside geom_line():

geom_line(aes(...), show.legend = FALSE))

Upvotes: 9

Related Questions