How to change the order of facet labels in ggplot (custom facet wrap labels)
Solution 1:
Don't rely on the default ordering of levels imposed by factor()
or internally by ggplot
if the grouping variable you supply is not a factor. Set the levels explicitly yourself.
dat <- data.frame(x = runif(100), y = runif(100),
Group = gl(5, 20, labels = LETTERS[1:5]))
head(dat)
with(dat, levels(Group))
What if I want them in this arbitrary order?
set.seed(1)
with(dat, sample(levels(Group)))
To do this, set the levels the way you want them.
set.seed(1) # reset the seed so I get the random order form above
dat <- within(dat, Group <- factor(Group, levels = sample(levels(Group))))
with(dat, levels(Group))
Now we can use this to have the panels drawn in the order we want:
require(ggplot2)
p <- ggplot(dat, aes(x = x)) + geom_bar()
p + facet_wrap( ~ Group)
Which produces:
Solution 2:
Just being working on a similar problem. I have levels which look like this by default:
[1] "A1" "A10" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9"
[11] "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9"
Note that the second level is out of place due to alphabetical order.
This is what I am doing to fix the order:
reorder(factor(fct),
fct %>%
str_replace("([[:alpha:]]+)", "\\1|") %>%
str_split("\\|") %>%
sapply(function(d) sprintf("%s%02d", d[1], as.integer(d[2]))),
function(x) x[1])
It replaces levels like "A1" with "A01" then reorders according to these. I'm sure that you could do this a lot more efficiently, but it does the job.
It could be adapted to address the original problem.