title: Renaming variables in legend in ggplot bar plot while keeping the bar order the same as well
body: <p>I have three dataframes in R:</p>
  <pre><code>&gt; df1
     zone   meangpp
  1     1 5.4153407
  2     2 4.2429236
  3     3 4.5719178
  4     4 3.1215946
  5     5 4.9222054
  6     6 3.0384872
  7     7 1.9293729
  8     8 8.9709741
  9     9 7.8904906
  10   10 6.6410986
  11   12 5.5011823
  
  &gt; df2
     zone     meangpp
  1     1 4.050161
  2     2 7.729265
  3     3 3.408220
  4     4 4.884040
  5     5 4.258422
  6     6 2.906374
  7     7 2.241984
  8     8 4.703197
  9     9 3.617657
  10   10 2.712997
  11   12 3.589406
  
  &gt; df3
     zone     meangpp
  1     1 5.65432
  2     2 8.76234
  3     3 2.67676
  4     4 5.63432
  5     5 9.87654
  6     6 3.45423
  7     7 6.76745
  8     8 3.45545
  9     9 1.23434
  10   10 2.712997
  11   12 3.589406
  </code></pre>
  <p>I created a grouped bar plot with the following code provided by a user in my previous question.</p>
  <pre><code>library(tidyverse)
  df &lt;- bind_rows(&quot;df1&quot; = df1, &quot;df2&quot; = df2, &quot;df3&quot; = df3, .id = &quot;groups&quot;)
  
  my_sd &lt;- df %&gt;%
    group_by(groups) %&gt;%
    summarise(sd=sd(meangpp))
  
  # Standard deviation
  df %&gt;% 
    left_join(my_sd) %&gt;% 
    mutate(zone = factor(zone)) %&gt;% 
    ggplot(aes(x=zone, y=meangpp, fill = groups)) + 
    geom_col(position = position_dodge()) +
    geom_errorbar(aes(x=zone, ymin=meangpp-sd, ymax=meangpp+sd), width=0.4, position = position_dodge(.9)) +
    ggtitle(&quot;using standard deviation&quot;)
  </code></pre>
  <p>The result of the plot is shown below:</p>
  <p><a href="https://i.stack.imgur.com/XFiCi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XFiCi.png" alt="barplot" /></a></p>
  <p>I would like to change the legend variable names <code>df1</code>, <code>df2</code>, and <code>df3</code> to <code>GC</code>, <code>IP</code>, and <code>CS</code> respectively.</p>
  <p>I made some previous attempts, but the variable <code>CS</code> shifts up in the legend and the bar of <code>CS</code> is also shifted to first in the plot.</p>
  <p>I would like to keep <code>GC</code>, <code>IP</code>, and <code>CS</code> in the same order in legend as shown above, not alphabetically. Additionally, I would like to keep the bars in the order shown above (<code>GC</code>, <code>IP</code>, <code>CS</code>).</p>