44
votes

I plotted a facet plot using ggplot and here is the plot

http://i.stack.imgur.com/5qXF1.png

The problem I have is, The facets(labels) are sorted alphabetically (Ex: E1, E10, E11,E13, E2, E3, I1, I10, I2) but I need them to be a custom order like E1, I1, E2, I2, E3, E10, I10, E11, E13.

How can I do that ?

2
Rearrange the order of the underlying factor. You can use relevel() or reorder() or make a custom order and use factor().Chase
Does this answer your question? Fixing the order of facets in ggplotjan-glx

2 Answers

55
votes

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:

facets wrapped

2
votes

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.