Re: [R] how to specify ggplot2 facet plot order

From: ONKELINX, Thierry <>
Date: Wed, 27 Feb 2008 10:38:40 +0100


The order of the facets row or column depend on the order in the associated factor. The code below is what you want. Note that I have changed 'cases' in 'series' because your example was not reproducible as a definition of 'cases' was missing.

series <- c('C2','C4','C8','C10','C15','C20') series <- factor(series, levels = series) ids <- c('ID1','ID2','ID3')
mydata <-
data.frame(SERIES=rep(series,30),ID=rep(ids,60),VALUE=rnorm(180)) qplot(VALUE, data = mydata, geom="density", facets=SERIES ~ ID)


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

Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney

-----Oorspronkelijk bericht-----
Van: [] Namens Chris Friedl
Verzonden: woensdag 27 februari 2008 4:39 Aan:
Onderwerp: [R] how to specify ggplot2 facet plot order

Hi, new to R and ggplot2. I've been trying to get a facet plot in which the
order of the facets is as I require, rather than ordered numerically, alphabetically, by Roman numerals, mean (answers to these were posted here
after much searching). Here's some test code to demonstrate what I get.

series = c('C2','C4','C8','C10','C15','C20') ids = c('ID1','ID2','ID3')
mydata <-
data.frame(SERIES=rep(cases,30),ID=rep(ids,60),VALUE=rnorm(180)) qplot(VALUE, data = mydata, geom="density", facets=SERIES ~ ID)

the facet rows are plotted in alpha order, namely, C10, C15, C2, C20, C4,
C8. I want them plotted in the order specified by series.

I've looked at reorder to reorder the factor called SERIES but that requires
a vector of the same length upon which the ordering is defined through some
function. I guess my noobness with all things R has brought me to a grinding

I can conceive an algorithm but don't know how to implement. 1. create myordervector of length(SERIES) comprising integers in a mapping
C2: 1, C4: 2, C8: 3 ...
2. reorder using this vector as follows: mydata <- with(mydata, reorder(SERIES, myordervector , as.numeric)) 3. Then plot as above

Is this remotely sensible? Perhaps the order is determined at plot time rather than from the data.frame. In this case I guess the reordering before
plotting is moot. I'm stuck. Can anyone help out please.


View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
Received on Wed 27 Feb 2008 - 09:44:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Feb 2008 - 10:30:17 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive