[R] Order Bars in ggplot2 bar graph

Thierry Onkelinx thierry.onkelinx at inbo.be
Thu Mar 19 10:27:29 CET 2015


You need to set the levels of the factor in the required order.

data$Leaf <- factor(data$Leaf, levels = c("C_Cot", "C_2", "C_1"))

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-03-19 10:06 GMT+01:00 <michael.eisenring op agroscope.admin.ch>:

> 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 op agroscope.admin.ch<mailto:
> michael.eisenring op agroscope.admin.ch>
> www.agroscope.ch<http://www.agroscope.ch/>
>
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list