简体   繁体   中英

ggplot2 legend labels not changing

I've been searching for an answer to why my legend labels aren't changing. so far, labs() is the only way I've managed to change the title. scale_fill_discreate/manual() don't seem to work.

Any ideas?

ggplot(na.omit(VMSdat), aes(x = mach_type, fill=mach_type, y= interest_lvl)) +
  labs(title = "Average Level of Interest in Studio machines by  Medical\nand Vetrinarian School students", x = "Machine Type", y = "Level of Interest")+
  labs(fill="Machine Type")+
  scale_fill_manual(name = "Machine type", labels=c("CNC milling", "Die Cutter", "Electronics", "3D Printing","3D Scanning", "Vacuum Former", "Virtual Reality"))+
  facet_wrap(~schoolName)+
  geom_bar(position=position_dodge(), stat="summary", fun.y="mean")+
  coord_cartesian(ylim = c(.2,5)) +
  theme(axis.title=element_text(size=12))+ 
  theme(legend.title = element_text(size=12), legend.text=element_text(size=11))+
  scale_fill_brewer(palette="Set1")

机器兴趣图

As pointed out by @Ben, you have two scale_fill... function in your code. So, it seems that one is replacing what the first one is doing. So, a possible solution is to merge them and pass all arguments in scale_fill_brewer .

Without a reproducible example of your dataset, it's difficult to be sure that this solution will perfectly work for your data. Here, I'm using the internal dataset iris to show how to change labels and name while using scale_fill_brewer :

library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_bar(stat = "identity")+
  scale_fill_brewer(palette = "Set1",
                    labels = c("A","B","C"),
                    name = "New Title")

And you get: 在此处输入图片说明

so, based on your code, you should get the correct plot by doing:

ggplot(na.omit(VMSdat), aes(x = mach_type, fill=mach_type, y= interest_lvl)) +
  labs(title = "Average Level of Interest in Studio machines by  Medical\nand Vetrinarian School students", x = "Machine Type", y = "Level of Interest")+
  labs(fill="Machine Type")+
  facet_wrap(~schoolName)+
  geom_bar(position=position_dodge(), stat="summary", fun.y="mean")+
  coord_cartesian(ylim = c(.2,5)) +
  theme(axis.title=element_text(size=12))+ 
  theme(legend.title = element_text(size=12), legend.text=element_text(size=11))+
  scale_fill_brewer(palette="Set1",
                    name = "Machine type", 
                    labels=c("CNC milling", "Die Cutter", "Electronics", "3D Printing","3D Scanning", "Vacuum Former", "Virtual Reality"))

If this is not working for you, please consider providing a reproducible example of your dataset (see here: How to make a great R reproducible example )

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