so I have a simple example–a fully crossed three treatmentthree context experiment, where a continuous effect was measured for each treatmentcontext pair. I want to order each treatment by effect, separately according for each context, but I’m stuck on ggplot’s faceting.
here’s my data
df <- data.frame(treatment = rep(letters[1:3], times = 3),
context = rep(LETTERS[1:3], each = 3),
effect = runif(9,0,1))
and I can get something very close if I collapse treatment and context into a single 9 point scale, as such:
df$treat.con <- paste(df$treatment,df$context, sep = ".")
df$treat.con <- reorder(df$treat.con, -df$effect, )
ggplot(df, aes(x = treat.con, y = effect)) +
geom_point() +
facet_wrap(~context,
scales="free_x",
ncol = 1)

except to achieve the separate ordering in each facet, the new x variable I created is potentially misleading, since it doesn’t demonstrate that we’ve used the same treatment in all three contexts.
Is this solved via some manipulation of the underlying factor, or is there a ggplot command for this situation?
Try:
The anonymous function provided to the
labelsargument does the formatting of the labels. In older versions of ggplot2 you used theformatterargument for this. If your treatment names are of differing lengths, then thesubstrapproach might not work too well, but you could usestrsplit, eg: