How to reorder x-axis in geom_boxplot by mean of the group in R?
<p>I want to be able to reorder the x-axis of a boxplot in ggplot by the mean of each of the groups. </p>

<p>For example, if I have this data frame:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>L3 &lt;- LETTERS[1:3]
fac &lt;- sample(L3, 50, replace = TRUE)
(d &lt;- data.frame(x = 1, y = sample(1:10, 50, replace = TRUE), fac = fac))
d

p &lt;- ggplot(d, aes(x=fac, y=y)) + geom_boxplot(fill = "#E69F00")
print(p)</code></pre>
</div>
</div>
</p>

<p>However, I want to be able to reorder the box plot by the mean of each factor (i.e. A, B, C), instead of the order in the dataframe. I have been looking for an answer for this and have run into several commands but none of them have worked. I am thinking there might be a way using order or reorder and dplyr/summarise, but everything I tried is not working.</p>

<p>I am not able to upload a picture yet, but let's say that the graph has a mean of 6 for A, 5 for B, and 5.5 for C. I would want the order to be B, C, A. My actual graph has 30 factors, so I want an easy way to do it without ordering it manually. </p>

<p>I really appreciate all the help!! </p>