How to change the order of facet labels in ggplot (custom facet wrap labels)

24,169

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:

facets wrapped

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.

Share:
24,169

Related videos on Youtube

Jana
Author by

Jana

Updated on February 03, 2020

Comments

  • Jana
    Jana about 4 years

    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 ?

    • Chase
      Chase about 13 years
      Rearrange the order of the underlying factor. You can use relevel() or reorder() or make a custom order and use factor().
  • CrunchyTopping
    CrunchyTopping almost 4 years
    Same as above but if the data itself is already in the custom order you want the Group variable to be in and you don't want to manually create a vector in that order: dat <- within(dat, Group <- factor(Group, levels = unique(levels(Group)))) since unique preserves the order

Related