简体   繁体   English

ggplot2图例标签没有改变

[英]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.到目前为止,labs() 是我设法更改标题的唯一方法。 scale_fill_discreate/manual() don't seem to work. scale_fill_discreate/manual() 似乎不起作用。

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.正如@Ben 所指出的,您的代码中有两个scale_fill...函数。 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 .因此,一个可能的解决方案是合并它们并在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 :在这里,我使用内部数据集iris来展示如何在使用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 )如果这对您不起作用,请考虑提供数据集的可重现示例(请参阅此处: 如何制作出色的 R 可重现示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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