title: Reordering bar plots - R, ggplot, position = "dodge"
body: <p>I made a data frame using the following code:</p>
  
  <pre><code>&gt; p &lt;- rep(c("5e-8", "0.05", "1"), 2)
  &gt; pgc1_rsq &lt;- c(0.0037, 0.0726, 0.0847)
  &gt; meta_rsq &lt;- c(0.0263, 0.1829, 0.1753)
  &gt; values &lt;- c(pgc1_rsq, meta_rsq)
  &gt; Type &lt;- c(rep("PGC1", 3), rep("PGC meta-analysis", 3))
  &gt; mydata &lt;- data.frame(p, values)
  &gt; mydata$p &lt;- factor(mydata$p, levels = c("5e-8", "0.05", "1"))
  </code></pre>
  
  <p>I created a bar graph using the following code:</p>
  
  <pre><code>&gt; plot &lt;-ggplot(mydata, aes(p, values))
  &gt; plot +geom_bar(stat = "identity", aes(fill = Type), position = "dodge") + xlab("P-value threshold") + ylab("Proportion of variance explained (Nagelkerke Rsq)")
  </code></pre>
  
  <p>Which produced this plot:
  <a href="https://i.stack.imgur.com/CnYG3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CnYG3.png" alt="Bar plot"></a></p>
  
  <p>I would now like to re-order the bars - so that "PGC1" comes before "PGC meta-analysis" in each pair. I have tried making both "values" and "Type" factors and ordering levels with PGC1 coming first like this:</p>
  
  <pre><code>&gt; mydata$value &lt;- factor(mydata$value, levels = c("pgc1_rsq", "pgc_meta"))
  </code></pre>
  
  <p>But this gave me an error message with "Type" and did not produce the desired outcome with "values".</p>
  
  <p>Input and suggestions would be greatly appreciated. Thanks.</p>