[R] how to specify ggplot2 facet plot order

Chris Friedl cfriedalek at gmail.com
Wed Feb 27 04:38:48 CET 2008

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: http://www.nabble.com/how-to-specify-ggplot2-facet-plot-order-tp15705404p15705404.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list