1

I'm having trouble making the charts in the order that appears in the axis.

I changed the levels of the factors in every possible way (relevel, level, ...). When I plot the main graphs, they are in the correct order, the labels are in the correct order too, but the boxplots are in the alphabetical order and the content of one main graph is switched with another (alphabetical order too).

Image of the problem

The code below works well:

a <- ggplot(data, aes(x = GroupX, y = Score, fill=GroupX, order=GroupX))+
scale_fill_manual(values=colours) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position="none",
    panel.background = element_rect(fill = "white"),
    panel.grid.major = element_line(colour = "gray95"),
    panel.border = element_rect(fill = NA, colour = "black", size = 2))+
ylab("Lesion")+
xlab("")+
guides(colour=FALSE)+
facet_wrap(~ Portion)

The code below produces the correct facet:

enter image description here

When I try to plot with:

a + geom_boxplot()

The order of bars and the entire graph appears in the alphabetical order.

Levels are in the correct format. I can't figure how to correct this.

  • Reproducible example

Dataset:

structure(list(Level = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("A", "B", "C", "D", "E"
), class = "factor"), Factor = structure(c(1L, 1L, 3L, 3L, 2L, 
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("X", "Y", "Z"
), class = "factor"), Valor = c(12, 11, 20, 15, 2, 5, 21, 21, 
51, 1, 2, 15, 2, 56, 5, 21, 5, 12, 21, 15, 23, 5, 4, 55, 1, 2, 
89, 2, 4, 12, 23, 10, 12, 51, 45, 2, 15, 32, 21, 15, 4, 5, 45, 
45, 2, 14)), .Names = c("Level", "Factor", "Valor"), row.names = c(NA, 
-46L), class = "data.frame")

Code without change the order:

a <- ggplot(data, aes(x = Level, y = Valor, fill=Factor, order=Level)) +
     facet_wrap(~ Factor)
a + geom_boxplot()

When we set the factor - fast way:

levels(data$Factor) <- c("Y", "Z", "X")
levels(data$Level) <- c("D", "C", "A", "B", "E")

Labels changed, but the plot don't change.

Calefin
  • 13
  • 5
  • 3
    Your question does not contain a [reproducible example](http://stackoverflow.com/q/5963269/4303162). It is therefore hard to know what is wrong and give you an appropriate answer. Please make your data available (e.g. by using `dput(data)`) or use one of the example data sets in R. Also, add the minimal code required to reproduce your problem to your post. – Stibu Jan 27 '16 at 17:35
  • Also, in ggplot2 2.0, `order` was officially deprecated as an aesthetic. If you're using a version < 2.0, mention it in your post; if you're using >=2.0 then delete the `order = GroupX` because it is being ignored by ggplot. – Gregor Thomas Jan 27 '16 at 17:46
  • two ways that you can use to order your x axis are 1) make it a factor with explicitly declared levels, and 2) use `scale_x_discrete(breaks=c("A","B","C","D"))` – Matt74 Jan 27 '16 at 18:00
  • Sorry, I did that. Now is correct. – Calefin Jan 27 '16 at 19:04

1 Answers1

1

You try to change the order of your levels with

levels(data$Factor) <- c("Y", "Z", "X")
levels(data$Level) <- c("D", "C", "A", "B", "E")

However, this will only change the names of the levels. So what was the first level before will now be called "Y", but it is still the first level and there is still the same data associated with it. But you can redefine the factor to change the order. So this should give you the expected result:

data$Factor <- factor(data$Factor, c("Y", "Z", "X"))
data$Level <- factor(data$Level, c("D", "C", "A", "B", "E"))

Let me show you what effect this has. First, I plot your data as it is:

enter image description here

Then, I redefine the factors and plot again:

data$Factor <- factor(data$Factor, c("Y", "Z", "X"))
data$Level <- factor(data$Level, c("D", "C", "A", "B", "E"))
ggplot(data, aes(x = Level, y = Valor, fill=Factor)) +
   facet_wrap(~ Factor) + geom_boxplot()

enter image description here

Stibu
  • 15,166
  • 6
  • 57
  • 71
  • It worked well, but when I use 8 groups the last one appears named NA. – Calefin Jan 27 '16 at 19:10
  • This can not be reproduced from your example. If you have a new problem, create a new reproducible example and post a new question (after you have failed to find an already answered question on SO). – Stibu Jan 27 '16 at 19:12
  • The level is already `NA` in your original data. So the mistake happens before. – Stibu Jan 27 '16 at 19:23
  • 1
    And it is not useful to change questions after you have gotten answers. Your question has become long and complicated and risks not to be useful any more. Try to clean it up a bit... – Stibu Jan 27 '16 at 19:25