[R] A way to "lock down" the order of bars for ggplot "dodged"histogram

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Feb 5 17:17:58 CET 2009

Dear Jason,

Convert Person to a factor with the levels in the order that you want.

Group_df$Person <- factor(Group_df$Person, levels = c("Them", "You", "Me")) 



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be 

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

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Jason Rupert
Verzonden: donderdag 5 februari 2009 17:09
Aan: R-help at r-project.org
CC: ggplot2 at googlegroups.com
Onderwerp: [R] A way to "lock down" the order of bars for ggplot "dodged"histogram

It appears that ggplot reorder the data for a "dodged" histogram based on whether or not a specific variable has a value in the first bin. 
I would like to be able to absolutely force the order of the variables in the bin.  For example, say I have three variables Me, You, and Them. 
Me_df<-data.frame(Data = c(1:15), Person = "Me")
You_df<-data.frame(Data = c(10:20), Person = "You")
Them_df<-data.frame(Data = c(15:25), Person = "Them")
counts <- ddply(Group_df, .(cut(Data, breaks=fullseq(range(Data), 5)), Person), nrow) 
names(counts) <- c("Bin", "Person", "Frequency")
qplot(Person, Frequency, data = counts, fill = Person, geom="bar", stat="identity", width = 0.9, xlab="Person") +  facet_grid(. ~ Bin) 
However, I absolutely must have the bar order be Them, You, Me.   Is there a to fix the order of the bars so that they are always in the Them, You, Me order? 
Thank you again for any feedback you can provide.  

	[[alternative HTML version deleted]]

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

More information about the R-help mailing list