简体   繁体   中英

ggplot2 Change Legend Label Values While Keeping Colors The Same

The similar questions gave solutions on how to change legend labels, by using scale_color_manual like this:

scale_fill_manual(name = "title", values = c("red","orange","blue","green"),
                     labels = c("1","2","3","4"))

Problem is, this method forces you to put the colors you want for the legends. If I put just labels, it won't work. How can I change the labels and keep the colors the same.

Try this:

library(ggplot2)
#Data
data("iris")
#Plot
ggplot(data = iris,aes(x=Species,y=Sepal.Width,fill=Species))+
 geom_bar(stat = 'identity')+
 scale_fill_discrete(labels=letters[1:3])

Output: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM