繁体   English   中英

ggplot2图例标签没有改变

[英]ggplot2 legend labels not changing

我一直在寻找为什么我的图例标签没有改变的答案。 到目前为止,labs() 是我设法更改标题的唯一方法。 scale_fill_discreate/manual() 似乎不起作用。

有任何想法吗?

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")

机器兴趣图

正如@Ben 所指出的,您的代码中有两个scale_fill...函数。 因此,似乎有人正在取代第一个正在做的事情。 因此,一个可能的解决方案是合并它们并在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")

你会得到: 在此处输入图片说明

因此,根据您的代码,您应该通过执行以下操作来获得正确的图:

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"))

如果这对您不起作用,请考虑提供数据集的可重现示例(请参阅此处: 如何制作出色的 R 可重现示例

暂无
暂无

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

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