user17144
user17144

Reputation: 438

Suppressing legend

Please see the graph below.

enter image description here

How do I suppress the legend highlighted in yellow?

The following is my code. The show.legend = FALSE does not work.

plots_yearly_avg_price<-lapply(overall_yearly_average_price,function(category_table){o<-melt(category_table, id = "Year", measure = c("THDT yearly avg price","All national and private label yearly avg price","Private label yearly avg price"));
ggplot(o, aes(Year, value, colour = variable),**show.legend=FALSE**) + geom_line()+
geom_label_repel(aes(label=value))+
labs(title=paste(category_table$Category,"Yearly avg. price",sep=" "),y="Average price")})

Upvotes: 1

Views: 266

Answers (2)

user17144
user17144

Reputation: 438

plots_yearly_avg_price<-lapply(overall_yearly_average_price,function(category_table){o<-melt(category_table, id = "Year", measure = c("THDT yearly avg price","All national and private label yearly avg price","Private label yearly avg price"));
ggplot(o, aes(Year, value, colour = variable)) + geom_line()+
geom_label_repel(aes(label=value))+
labs(title=paste(category_table$Category,"Yearly avg. price",sep=" "),y="Average price")+guides(colour=FALSE)})

works. I added "+guides(colour=FALSE)" at the end.

Upvotes: 1

dc37
dc37

Reputation: 16178

You should add show.legend = FALSE in each of the geom you don't want to display the corresponding legend. For exemple, in your geom_label_repel to suppress color letters (associated to this function):

ggplot(o, aes(Year, value, colour = variable)) + 
geom_line()+
geom_label_repel(aes(label=value), show.legend = FALSE)+
labs(title=paste(category_table$Category,"Yearly avg. price",sep=" "),y="Average price")})

Alternatively, you can use theme(legend.position = "none") to completely suppress your legend.

ggplot(o, aes(Year, value, colour = variable)) + 
geom_line()+
geom_label_repel(aes(label=value), show.legend = FALSE)+
labs(title=paste(category_table$Category,"Yearly avg. price",sep=" "),y="Average price")})+
theme(legend.position = "none")

Does it answer your question ?

Upvotes: 2

Related Questions