R 如何获得自定义颜色并使图例具有正确的标签(ggplot)?
我有一个可爱的堆叠条形图(见附图) 我使用以下代码创建了这部杰作:R 如何获得自定义颜色并使图例具有正确的标签(ggplot)?,r,ggplot2,colors,legend,R,Ggplot2,Colors,Legend,我有一个可爱的堆叠条形图(见附图) 我使用以下代码创建了这部杰作: ggplot(data=data, aes(x=docket, y=Approved, fill=EMH_Cat)) + geom_bar(stat="identity", position="stack") + labs(title="Type of Study by Project", x="Project", y="Number of Studies") + theme(plot.title
ggplot(data=data, aes(x=docket, y=Approved, fill=EMH_Cat)) + geom_bar(stat="identity", position="stack") +
labs(title="Type of Study by Project", x="Project", y="Number of Studies") +
theme(plot.title = element_text(size=30,hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white')) +
theme(axis.line = element_line(color="black", size = 0.5)) +
theme(axis.text.x= element_text(size=8,color = "black")) +
theme(axis.text.y = element_text(size=12, color = "black")) +
scale_y_continuous(breaks=seq(0,60,10)) + scale_x_discrete(labels=c("Bartletts\nFerry","Cabin\nCreek","Claytor","Emeryville","Green\nIsland","Jackson","Jennings\nRandolph","Keowee\nand\nToxaway","Loup\nCanal","Mahoning","Martin","Mason","Monadnock","Old\nHarbor","Oswegatchie\nRiver","Otter\nCreek","Salina","Tomahawk","Vanceboro","Wallowa\nFalls","Wells","Williams","York\nHaven")) +
theme(axis.title.x = element_text(face="bold", size=12,vjust=-0.5,hjust=0.5)) +
theme(axis.title.y = element_text(face="bold", size=12,vjust=2,hjust=.5)) +
scale_fill_discrete(name = "Categories", labels = c("Biota and Biodiversity","Connectivity and Fragmentation","Cultural Resources", "Geomorphology","Infrastructure and Design", "Landcover","Recreation", "Water Quality","Water Quantity")) +
theme(panel.border = element_rect(colour = "black", fill=NA),legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) +
theme(legend.title.align=0.5) + theme(legend.position = c(0.09,0.7))
我使用了read.csv函数。我的数据如下所示:
这个传奇故事并不精彩,但我可以也将关注它。然而,当我尝试添加这个小家伙时,问题出现了:
scale_fill_manual(values=c("#1e7640", "#de772d", "#e815dd", "#7f6c00", "#000000", "#a03223", "#9e07f5","#2abdda", "#306ebe"))
这些是某个出版物中的颜色,所以我需要它们也出现在这个图表中。如果我运行第一个代码块,然后在底部添加这个家伙,我会得到一个错误,“填充”的比例已经存在。为“填充”添加另一个比例,该比例将替换现有比例
看起来是这样的:
我明白为什么R会这样,我只是不知道如何修复它。我也想要我的颜色和吃传奇。救命 您使用
名称
和标签
参数进行scale\u fill\u离散
。您需要将这些完全相同的参数与值一起提供给您的缩放填充手册
。删除scale\u fill\u discrete
行并添加
scale_fill_manual(
name = "Categories",
labels = c("Biota and Biodiversity", "Connectivity and Fragmentation",
"Cultural Resources", "Geomorphology", "Infrastructure and Design",
"Landcover","Recreation", "Water Quality","Water Quantity")
values = c("#1e7640", "#de772d", "#e815dd", "#7f6c00", "#000000",
"#a03223", "#9e07f5","#2abdda", "#306ebe")
)
您有scale\u fill\u discrete
和标签
参数。您需要将这些完全相同的参数与值一起提供给您的缩放填充手册
。删除scale\u fill\u discrete
行并添加
scale_fill_manual(
name = "Categories",
labels = c("Biota and Biodiversity", "Connectivity and Fragmentation",
"Cultural Resources", "Geomorphology", "Infrastructure and Design",
"Landcover","Recreation", "Water Quality","Water Quantity")
values = c("#1e7640", "#de772d", "#e815dd", "#7f6c00", "#000000",
"#a03223", "#9e07f5","#2abdda", "#306ebe")
)
事情从来没有这么顺利过。非常感谢。如果我能通过电子邮件发送啤酒,我会的。从来没有什么事情能如此顺利地进行。非常感谢。如果我能通过电子邮件发送啤酒,我会的。