Change order of one set of factors plotted in ggplot2
<p>I am creating a plot using ggplot2 which is almost perfect, it looks like this:</p>

<p><a href="https://i.stack.imgur.com/b5oEt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b5oEt.jpg" alt="enter image description here"></a></p>

<p>However, I want to change the order that the 'watering treatment' boxes are shown - I want the order to be red, yellow, blue, purple, green.  </p>

<p>To create the dataframe for my plot, I started with a dataframe that looked like this (note: 'Water' letters are abbreviations for the colours mentioned above):</p>

<pre><code>  Ring CO2 Water plot_NH4.2x25
1     1 550     B      2.228750
2     1 550     P      4.945625
3     1 550     R     22.724375
4     1 550     W     -0.644375
5     1 550     Y     -0.770000
6     2 475     B      2.228750
7     2 475     P      4.945625
8     2 475     R      1.348750
</code></pre>

<p>etc.</p>

<p>I thought I could do one of the following:
a) change the name of the watering treatment to A B C D or 1 2 3 4 so they automatically plot in the correct order
b) add a bit of code that asks ggplot to plot them in the order I want.</p>

<p>But I can't figure out how to do either!  I've messed around with 'ifelse', but this seems to require you to make a statement that is relevant to a numeric column, not another factor.  I also tried 'xlim' but am unsure how to make this work for a factor that is being plotted as the colour, rather than the primary value on the x axis.</p>

<p>The code I've used to create the plot is:</p>

<pre><code>g.Amm.2 &lt;-   ggplot(data=NH4_24March_plot, aes(x=CO2, y=plot_NH4.2x25, fill=Water)) +
  stat_boxplot(geom ='errorbar', width = 0.5, position=position_dodge(0.75))+
  geom_boxplot()+
  theme_bw()+
  theme(panel.border = element_blank(),                            #remove box boarder
    axis.line.x = element_line(color="black", size = 0.5),      #add x axis line
    axis.line.y = element_line(color="black", size = 0.5),      #add y axis line
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    legend.key.size = unit(1.5, 'lines'),
    legend.position=c(0.9,0.8),
    legend.key = element_blank(),    #remove grey box from around legend
    panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
 scale_y_continuous(expand = c(0, 0), limits = c(-5,140), breaks=seq(0,140,20))+     #change x axis to intercept y axis at 0
  scale_fill_manual(values=c("skyblue2", "orchid1", "firebrick1", "seagreen3", "yellow2"),
                name=" Watering treatment",
                labels=c("optimal summer \noptimal autumn", "excess summer \nlimited autumn", 
                         "excess summer \noptimal autumn","limited summer \nexcess autumn",
                         "optimal summer \nlimited autumn"))+
  ylab(expression(Membrane~available~NH[4]^{" +"}~-N~(~mu~g~resin^{-1}~14~day^{-1})))+
  xlab(expression(CO[2]~concentration~(mu~mol~mol^{-1})))
</code></pre>

<p>Any thoughts would be greatly appreciated, thanks in advance :-)</p>