[R] how to specify ggplot2 facet plot order

ONKELINX, Thierry
Wed, 27 Feb 2008 09:38:40 -0000

Chris,

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.

library(ggplot2)
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)

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 op inbo.be
www.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
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
halt.

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.

thanks.




--
View this message in context:
http://www.nabble.com/how-to-specify-ggplot2-facet-plot-order-tp15705404
p15705404.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help op r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.