0

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?

Euclides
  • 99
  • 1
  • 7
  • @camille I don't think this is a duplicate. Even though the solution is the same, I think that as a problem it is a bit different, as I am concerned with the order in the axis and not in the legend. As an explicitly different question it can be useful for others facing the same problem in a similar context (but maybe this is just my newbie perspective...) – Euclides May 28 '19 at 17:50
  • That's fair to point out that you're asking about legend order vs axis label order, but you ask also about the order of the colors, which by default will be the same as the order in the legend. Either way, there are definitely a lot of posts already on ordering an axis; [here's one](https://stackoverflow.com/q/3253641/5325862) with 5 answers, and [here's another](https://stackoverflow.com/q/12774210/5325862) – camille May 28 '19 at 18:42

1 Answers1

2

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

phalteman
  • 3,442
  • 1
  • 29
  • 46