簡體   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