I'm trying to make a stacked bar graph with a facet_wrap, but I want the order of my stacked variables ("developed") to be flipped. I've reordered the factors, and tried "order=descend()," as well as "scale_fill_manual" and nothing seems to work.
Here is my code:
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
changing factor levels of "agriculture" and "developed"
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"
Then, plotting:
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)
I want the "developed" parts of the bars in gray on top of the "available" parts of the bars, which are black. And the legend should match the order of the bars as well.
Also, is it possible to move the facet_wrap "All islands" and "Oahu" at the top to the bottom of the graph under "loi" "dryland" and "agroforestry." Thank you for your help!!
See Question&Answers more detail:
os