[R] how to specify ggplot2 facet plot order

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Feb 27 10:38:40 CET 2008


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 <-
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
Thierry.Onkelinx op inbo.be 

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: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Chris Friedl
Verzonden: woensdag 27 februari 2008 4:39
Aan: r-help op r-project.org
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
order of the facets is as I require, rather than ordered numerically,
alphabetically, by Roman numerals, mean (answers to these were posted
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 <-
qplot(VALUE, data = mydata, geom="density", facets=SERIES ~ ID)

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

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

I can conceive an algorithm but don't know how to implement.
1. create myordervector of length(SERIES) comprising integers in a
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
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 Nabble.com.

R-help op r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list