title: Order piechart pieces
body: <p>Although I am aware there are a few posts on this matter I still have not been able to figure out how to order the wedgesof the pie chart and match them with their according label. </p>
  
  <p>I have the following dataset (called plotter data):</p>
  
  <pre><code>Code     Percentage
  a          21,43
  b          21,43
  c          3.58
  d          21,43
  e          3.58
  f          14.29
  g          3.58
  h          7.14
  i          3.58
  </code></pre>
  
  <p>Following the ideas from this post (<a href="https://stackoverflow.com/questions/41338757/adding-percentage-labels-on-pie-chart-in-r">adding-percentage-labels-on-pie-chart-in-r</a>) I have developed the following code:</p>
  
  <pre><code>library(ggplot2)
  library(dplyr)
  library(ggforce)
  library(scales)
  library(ggrepel)
  
  plotter.data&lt;-read.csv("plotter analysis.csv",header=T)
  plotter.data$Code&lt;-factor(plotter.data$Code)
  plotter.data$Percentage&lt;-round(plotter.data$Percentage, 2)
  
  plotter.data&lt;-plotter.data %&gt;%
  arrange(desc(Code))%&gt;%
  mutate(text_y = cumsum(Percentage) - Percentage/2)
  plotter.data
  
  plotter.plot&lt;ggplot(data=plotter.data,aes(x=1,y=Percentage,fill=reorder(Code,-Percentage)))+
  geom_bar(stat = "identity",color='black')+
  geom_label_repel(aes(label = percent(Percentage/100),y = text_y), size=3, show.legend = F, nudge_x = 1)+
  guides(fill = guide_legend(title = "Code"))+
  coord_polar("y",start=0)+ 
  labs(x='',y='',title='Code Plotter systems')+
  theme(plot.title=element_text(hjust=0.5,face='bold',size=16))+
  theme(axis.text.x=element_blank(),
   axis.text.y=element_blank(),
   legend.title=element_text(size=12,face='bold'))
  plotter.plot
  </code></pre>
  
  <p>Which give me this figure
  <a href="https://i.stack.imgur.com/Qd4hf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qd4hf.png" alt="enter image description here"></a></p>
  
  <p>As you can see in the plot, the order of the wedges is correct, since I reorder my levels with <code>reorder()</code> in the <code>ggplot</code> code, but however, my labels (<code>text_y</code>) do not follow the same order. I have tried to order the dataset previous to making the <code>ggplot</code> and still does not work. I was wondering if you could let me know why the labels appear in a different order and how to fix it.</p>
  
  <p>I have also tried to calculate <code>text_y</code> prior and after ordering my dataframe but once using ggplot later the labels still appear in a different position.</p>
  
  <p>Thanks a lot for any advice</p>