GGPLOT- how to change default order of BOXPLOT with 3 groups and 3 subgroups

Viewed 1531

I would like to make this kind of plot using ggplot2, geom_boxplot:

enter image description here

the plot has 3 groups (Taille des cours d'eau) which are "Petit" "Moyens" and "Grands" and 3 subgroups "Pesticides" "Medicaments" "Autres MP". And the order on the X is given by the subgroups rather then the groups.

If I type the following code, where Case1 is my data.frame Type defines the subgroups and Rivers the group and newMess the y values

P<-ggplot(Case1,aes(x=River,y=newMess, fill=Type)) +geom_boxplot(position = "dodge2",outlier.shape = 1)+scale_y_continuous(trans = 'log10') + stat_boxplot(geom = 'errorbar', lenght=0.5)

ggplot2 automatically order the boxplots on the groups (as in the picture below). However, I would like to have them ordered by soubgroups. Does anyone know how I can change the default order from groups to subgroups (meaning in the case below match the type (coulors) together)?

enter image description here

Thank you sooo much for your answer!!

(P.S I tried to change the order in the data.frame and organize it by the Type (subgroups) however this does not solve my problem)

3 Answers

When encountering this problem you can use factor with the variables (both in X variable and the subgroup variable to get them ordered using level=

In this case I think would look like this:

level_orderX <- c("Petit", "Moyens","Grands")
level_order_subgroup <- c("Pesticides", "Medicaments", "Autres MP")

P <- Case1 %>% ggplot(aes(x= factor(River, level=level_orderX), y=newMess, fill=factor(Type, level = level_order_subgroup))) +
  geom_boxplot(position = "dodge2",outlier.shape = 1) +
  scale_y_continuous(trans = 'log10') + 
  stat_boxplot(geom = 'errorbar', lenght=0.5)
  

Also when trying this it gives me a warning for the boxplot saying the lenght isn't used. When haing a boxplot usually the error bar is implied in the wiskers of the plot calculated from the data itself, so I am not sure if the last line of code is really needed? It would be for example for a barplot but maybe not for a boxplot! Hope it helped!

It would be helpful to see your plot as it comes out. If you use 'River' on the x-axis, then it will be ordered by 'River', and 'Type' will be used to colour the bars, although you can change the order of the 'River' groups on the x-axis. If you instead use 'Type' as your x-axis, you can sort it on sub-groups per River.

These examples might be helpful, you can also see this answer described here.

I have data where Session takes values 0, 1 and 2. I also have Group which takes on values 1 and 2.

DG_Means <- summarySE(data = DGUG_All, measurevar = "UG_Deliberation", groupvars = c("Session","Group"))

ggplot(data=DGUG_All, aes(x = factor(Session,level=c(2,1,0)), y = UG_Deliberation, fill = Group, color = Group, Group = Group)) +
  geom_boxplot(alpha = 0.6,color="black") +
  theme_light()

This is what the plot looks like

I can also change what goes on the x-axis and what I use for fill, to change this (also, by specifying x = factor(Group,level=c(2,1)) you can decide the order of 'Group' on the x-axis. If you say: x = factor(Group,level=c(1,2)), then the order will change).

ggplot(data=DGUG_All, aes(x = factor(Group,level=c(2,1)), y = UG_Deliberation, fill = Session, color = Session)) +
  geom_boxplot(alpha = 0.6,color="black") +
  theme_light()

sorted by Group

Thank you really much for your answer! Unfortunately, it doesn't really reply to my question. Swapping the x and y axis don't solve my problem as I want the order on the x axis to follow the subgroups in any of the two cases.

I thought I added images, I'm sorry. I will add them here. How I would like to have my plot

How my plot look like

So in my example, I would like to have the subgroups sorted together, like this:

type1 type 1 type 1 ->type 2 type 2 type 2 -> type3 type 3 type 3

(so that the colour match together) and on the x axis it would be as follow:

river 1 river 2 river 3 -> river 1 river 2 river 3-> river 1 river 2 river 3

In your examples, I would like to have follow the order of the subgroups so that the subgroup with the same colour are one after the other rather than alternated, therefore in example 1 i would like to have the following sequence of subgroups:

Group 1 Group 2

Therefore on the x asis it will be: session 2 1 0 (Group1) -> Session 2 1 0 (Group2)

In the second example instead, it would be ordered again by subgroups, therefore:

Session 0 Session 1 session 2 (so that all the colour match together) and as a consequence on the x axis it will be (reading from left to right):

factor 1 2 (Session 0) then next subgroup -> factor 1 2 (Session 1) and then the last subgroup -> factor 1 2 (session 2)

I hope I made my question a bit clearer now. I appreciate any kind of help, I'm really stuck with this problem :)

Related