[R] Order Bars in ggplot2 bar graph

michael.eisenring at agroscope.admin.ch michael.eisenring at agroscope.admin.ch
Thu Mar 19 10:06:28 CET 2015


Dear experts,
I am trying to make a bar graph using ggplot2. I would like to define the order of my bars independently of alphabetical or numerical order.
How can I do that.
Here a simplified example of my code and comments(#) describing my problem:


#Code start


library(ggplot2)

bar<-ggplot(data,aes(Leaf,Av_Glands_cor,fill=Damage))



# The column "Leaf"  contains the variable C_1, C_2 and C_Cot. R always plots the bars in the following orders : C_1 (closest to the y-axis), C_2, C_Cot

# How do I have to modify my code that the order of the bars is: C_Cot, C_2, C_1?



bar+stat_summary(fun.y=mean,geom="bar",position="dodge",colour="black")+

  theme_bw()+

  theme(text = element_text(size=15),

        axis.text.x = element_text(angle=90, vjust=1))+

stat_summary(fun.data=mean_cl_normal,geom="errorbar",position=position_dodge(width=0.9),width=0.4,colour="gray65")+

labs(x="Leaf",y="Average nr. glands corrected for leaf sz.",fill="Damage")+

  scale_fill_manual(values=c("gray95", "gray75", "gray45", "black"))

#Code end


Thank you very much,
Michael Eisenring

Eisenring Michael, Msc.
PhD Student

Federal Department of Economic Affairs, Education and Research
EAER
Institute of Sustainability Sciences ISS
Biosafety

Reckenholzstrasse 191, CH-8046 Zürich
Tel. +41 44 37 77181
Fax +41 44 37 77201
michael.eisenring at agroscope.admin.ch<mailto:michael.eisenring at agroscope.admin.ch>
www.agroscope.ch<http://www.agroscope.ch/>


	[[alternative HTML version deleted]]



More information about the R-help mailing list