1

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")})
user17144
  • 428
  • 3
  • 18

2 Answers2

2

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 ?

dc37
  • 15,840
  • 4
  • 15
  • 32
  • geom_label_repel(aes(label=value), show.legend = FALSE)+ .. did not have any effect. +theme(...) gave Error in UseMethod("ph_with", value) : no applicable method for 'ph_with' applied to an object of class "NULL" – user17144 Mar 03 '20 at 16:09
  • Weird ... What is `ph_with` ? Can you edit your question to provide a reproducible example of your dataset ? Like that I can work with similar data than yours to get it work (see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Mar 03 '20 at 16:21
  • Yes, this solution works too. However, it's quite weird that `theme(legend.position = "none")` did not work for you. But glad you figure it out. (in posting your code, remove the ** because it looks like it's part of the code whereas it is to put in bold. – dc37 Mar 03 '20 at 18:13
  • Agreed. I was hoping your solution would work because it's very intuitive. It was hard for me to post everything for you to debug, and so I kept looking for alternative solutions and found this one. Thank you for your help. By the way, "ph_with" is a call in the officer package that I used to write a graph to a ppt. – user17144 Mar 03 '20 at 20:01
1
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.

user17144
  • 428
  • 3
  • 18