我有一个漂亮的堆叠条形图(请参见附件)。carnival colors
我使用以下代码创建了这个杰作:
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函数。我的数据如下:snippet
传说不在工厂,但我可以而且会注意的。但是,当我尝试添加这个小家伙时,问题就来了:
scale_fill_manual(values=c("#1e7640", "#de772d", "#e815dd", "#7f6c00", "#000000", "#a03223", "#9e07f5","#2abdda", "#306ebe"))
这些是某个出版物中的颜色,因此我也需要它们出现在此图表中。如果我运行第一个代码块,然后在底部添加此人,则会出现错误,“填充”的比例已经存在。为“填充”添加另一个刻度,它将替换现有的刻度。
看起来像这样:argh
我知道为什么R会这样做,我只是不知道如何解决。我也想拥有自己的颜色并吃传奇。帮助!
您具有带有scale_fill_discrete
和name
自变量的labels
。您需要为scale_fill_manual
和values
赋予完全相同的参数。删除您的scale_fill_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")
)