Is it possible to sort factors in a multipanel plot in ggplot2
according to the first panel? The first panel decides the order and the remaining panels follow that order.
Here is an example:
require(ggplot2)
set.seed(36)
xx<-data.frame(YEAR=rep(c("X","Y"), each=20),
CLONE=rep(c("A","B","C","D","E"), each=4, 2),
TREAT=rep(c("T1","T2","T3","C"), 10),
VALUE=sample(c(1:10), 40, replace=T))
ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) +
geom_bar(stat="identity", position="dodge") +
facet_wrap(~TREAT)
Which gives me this plot:
Now I would like to sort CLONE
based on the VALUE
in YEAR X
in a descending order (highest to lowest) but only for the Control (C
panel). This order should then be maintained for T1
, T2
, and T3
. By looking at the plot above, I want panel C
sorted as CLONE C
, B
or D
(both are 5), A
and E
. This order of CLONE
should then be replicated for the remaining panels.