title: How to center stacked percent barchart labels
body: <p>I am trying to plot nice <code>stacked percent barchart</code> using <code>ggplot2</code>. I've read some material and almost manage to plot, what I want. Also, I enclose the material, it might be useful in one place:</p>
  
  <p><a href="https://stackoverflow.com/questions/7364404/how-do-i-label-a-stacked-bar-chart-in-ggplot2-without-creating-a-summary-data-fr">How do I label a stacked bar chart in ggplot2 without creating a summary data frame?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9563368/create-stacked-percent-barplot-in-r">Create stacked barplot where each stack is scaled to sum to 100%</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12386005/r-stacked-percentage-bar-plot-with-percentage-of-binary-factor-and-labels-with">R stacked percentage bar plot with percentage of binary factor and labels (with ggplot)</a></p>
  
  <p>My problem is that I can't place <code>labels</code> where I want - in the middle of the bars. <a href="https://i.stack.imgur.com/s79Wb.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/s79Wb.jpg" alt="enter image description here"></a></p>
  
  <p>You can see the problem in the picture above - labels looks awfull and also overlap each other.</p>
  
  <p>What I am looking for right now is:</p>
  
  <ol>
  <li><p>How to place labels in the midde of the bars (areas)</p></li>
  <li><p>How to plot not all the labels, but for example which are greather than 10%?</p></li>
  <li><p>How to solve overlaping problem?</p></li>
  </ol>
  
  <p>For the <code>Q 1.</code> @MikeWise suggested possible <a href="https://stackoverflow.com/questions/34898222/stacked-ggplot-percent-barchart-in-shiny">solution</a>. However, I still can't deal with this problem.</p>
  
  <p>Also, I enclose reproducible example, how I've plotted this grahp.</p>
  
  <pre><code>library('plyr')
  library('ggplot2')
  library('scales')
  set.seed(1992)
  n=68
  
  Category &lt;- sample(c("Black", "Red", "Blue", "Cyna", "Purple"), n, replace = TRUE, prob = NULL)
  Brand &lt;- sample("Brand", n, replace = TRUE, prob = NULL)
  Brand &lt;- paste0(Brand, sample(1:5, n, replace = TRUE, prob = NULL))
  USD &lt;- abs(rnorm(n))*100
  
  df &lt;- data.frame(Category, Brand, USD)
  
  # Calculate the percentages
  df = ddply(df, .(Brand), transform, percent = USD/sum(USD) * 100)
  
  
  # Format the labels and calculate their positions
  df = ddply(df, .(Brand), transform, pos = (cumsum(USD) - 0.5 * USD))
  
  #create nice labes
  df$label = paste0(sprintf("%.0f", df$percent), "%")  
  
  
  
  ggplot(df, aes(x=reorder(Brand,USD,
                                function(x)+sum(x)),  y=percent, fill=Category))+
    geom_bar(position = "fill", stat='identity',  width = .7)+
    geom_text(aes(label=label, ymax=100, ymin=0), vjust=0, hjust=0,color = "white",  position=position_fill())+
    coord_flip()+
    scale_y_continuous(labels = percent_format())+
    ylab("")+
    xlab("")
  </code></pre>