Jason Rupert
2009-Feb-05 16:09 UTC
[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") Group_df_tmp<-rbind(Me_df,You_df) Group_df<-rbind(Group_df_tmp,Them_df) 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]]
ONKELINX, Thierry
2009-Feb-05 16:17 UTC
[R] A way to "lock down" the order of bars for ggplot "dodged"histogram
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")) HTH, Thierry ---------------------------------------------------------------------------- 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 Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.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") Group_df_tmp<-rbind(Me_df,You_df) Group_df<-rbind(Group_df_tmp,Them_df) 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.