Search code examples
rggplot2facet-wrap

Is there a way to use vectors to determine the order of x-axis labels and plot colours when using facet_wrap in ggplot2?


I would like to make 3 plots using facet_wrap, where I could set the order of the x-axis based on myOrder, and the colours based on myColours. I have the following data.frame:

Cell <- c("AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", 
      "AAA", "ABB", "ABB", "ABB", "ABB", "ABB", "ABB", "ABB", "ABB", 
      "ABB", "ABB", "ACB", "ACB", "ACB", "ACB", "ACB", "ACB", "ACB", 
      "ACB", "ACB", "ACB", "BAA", "BAA", "BAA", "BAA", "BAA", "BAA", 
      "BAA", "BAA", "BAA", "BAA", "BCA", "BCA", "BCA", "BCA", "BCA", 
      "BCA", "BCA", "BCA", "BCA", "BCA", "BCC", "BCC", "BCC", "BCC", 
      "BCC", "BCC", "BCC", "BCC", "BCC", "BCC", "CAA", "CAA", "CAA", 
      "CAA", "CAA", "CAA", "CAA", "CAA", "CAA", "CAA", "CCC", "CCC", 
      "CCC", "CCC", "CCC", "CCC", "CCC", "CCC", "CCC", "CCC", "CBA", 
      "CBA", "CBA", "CBA", "CBA", "CBA", "CBA", "CBA", "CBA", "CBA")
Mean <- c(8, 11, 37, 3, 30, 8, 38, 50, 9, 31, 27, 41, 16, 
      29, 4, 29, 28, 22, 48, 36, 13, 46, 21, 41, 38, 21, 
      26, 27, 49, 4, 38, 44, 49, 48, 32, 3, 6, 25, 33, 
      9, 9, 6, 8, 43, 21, 44, 41, 49, 32, 35, 40, 2, 41, 
      34, 22, 12, 0, 21, 39, 30, 49, 26, 26, 39, 4, 13, 
      18, 12, 9, 29, 24, 40, 38, 17, 40, 13, 30, 8, 9, 
      11, 40, 19, 46, 6, 49, 38, 16, 1, 34, 22)
Type <- c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
      "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
      "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", 
      "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
      "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", 
      "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
      "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3")
TesT <- data.frame(Cell,Mean, Type)

I have been trying to plot these data using ggplot with the following code:

myOrder <- c("AAA", "ACB", "ABB", "BCA", "BAA", "BCC", "CBA", "CAA", "CCC")
myColours <- c("red", "blue", "green", "red", "blue", "green", rep("grey", 3))

ggplot(TesT, aes(x = Cell, y = Mean, fill = Cell)) + 
  theme_bw() +
  stat_boxplot(geom = "errorbar") +
  geom_boxplot(colour = "black") +
  scale_x_discrete(breaks = myOrder, labels = myOrder) +
  scale_fill_manual(limits = myOrder, values = myColours) + 
  facet_wrap(~Type, scales = "free_x") +
  scale_shape()

This is what I get:

enter image description here

My problem is that I do not manage to get the x-axis in the correct order (the order in myOrder). The only way in which I managed to do this was by changing breaks to limits (scale_x_discrete(limits = myOrder, labels = myOrder)). However, when I do this I get all the possible x categories in all the plots: enter image description here

I have spent quite some time looking for an answer in here and in google but nothing seems to solve my problem. Does anyone have a suggestion?


Solution

  • Define TesT$Cell as a factor, and specify the order of the levels.

    TesT$Cell <- factor(TesT$Cell, levels=myOrder)
    

    The plotting code doesn't need to change.

    enter image description here