我想制作一个使用facet_wrap的堆积条形图,但是希望我的堆叠变量(“developed”)的顺序翻转。我已经重新排列了因子,并尝试了“order = descend()”,以及“scale_fill_manual”,但似乎都没有起作用。
以下是我的代码:
developed=rep(c("developed","available"),6)
agriculture=rep(c(rep("loi",2), rep("dryland",2), rep("agroforestry",2)),2)
acres=c(7435,24254,10609,120500,10651,75606,6037,9910,4390,895,9747,46893)
islands=c(rep("All islands",6), rep("Oahu",6))
all_is2=data.frame(developed, agriculture, acres, islands)
head(all_is2)
developed agriculture acres island
1 developed loi 7435 All islands
2 available loi 24254 All islands
3 developed dryland 10609 All islands
4 available dryland 120500 All islands
5 developed agroforestry 10651 All islands
6 available agroforestry 75606 All islands
调整"农业"和"发达"两个因素的水平
all_is2$agriculture=factor(all_is2$agriculture,levels=c("loi","dryland","agroforestry"))
all_is2$developed=factor(all_is2$developed,levels=c("developed","available"))
levels(all_is2$developed)
[1] "developed" "available"
然后,绘制:
ggplot(all_is2,aes(x=agriculture,y=acres,fill=developed))+
geom_bar(position="stack", stat="identity")+
facet_wrap(~islands)+ scale_fill_grey(start=0.8, end=0.2, name="")+ xlab("")+ylab("Acres")+theme_bw()+ scale_y_continuous(labels=comma)
我希望将灰色的“developed”部分放在黑色的“available”部分之上,并且图例应该与条形图的顺序相匹配。
同时,是否可能将facet_wrap“全部岛屿”和“Oahu”从图的顶部移动到“loi”“dryland”和“agroforestry”的下方。谢谢您的帮助!
levels=c("available","developed")
)。而且,如果你想要标签在图形下方,那么分面总是在图形的顶部或旁边,使用grid.arrange方法可能是一个好的选择。 - Herokafacet_wrap(〜islands,switch =“x”)
。 - Heroka