title: R - ggplot: Selection of which labels to appear in bar stack and their position
body: <p>Currently we have struggled with the following for many, many hours before posting this question.</p>
  
  <p>We have a huge number of data-sets similar to the following:</p>
  
  <pre><code>Income                 Inhabitants              Percent
  Below  15000            Below 5000              4.664723
  15000 - 3.000           Below 5000              15.743440
  30000 - 40000           Below 5000              13.994169
  40000 - 50000           Below 5000              12.609329
  50000 - 60000           Below 5000              11.333819
  60000 - 70000           Below 5000              11.370262
  70000 - 100000          Below 5000              14.795918
  Above  100000           Below 5000              5.211370
  Do not know             Below 5000              10.276968
  Below  15000            5000-20000              4.225146
  15000 - 3.000           5000-20000              13.157895
  30000 - 40000           5000-20000              12.733918
  40000 - 50000           5000-20000              11.739766
  60000 - 70000           5000-20000              11.315789
  70000 - 100000          5000-20000              18.728070
  Above  100000           5000-20000              7.880117
  Do not know             5000-20000              9.356725
  Below  15000            20000-110000            4.013588
  15000 - 3.000           20000-110000            11.147458
  30000 - 40000           20000-110000            11.927529
  40000 - 50000           20000-110000            11.751384
  50000 - 60000           20000-110000            9.738299
  60000 - 70000           20000-110000            10.367388
  70000 - 100000          20000-110000            17.929039
  Above  100000           Above 110000            13.198289
  Do not know             Above 110000            9.927026
  Below  15000            Above 110000            4.662941
  15000 - 3.000           Above 110000            10.286413
  30000 - 40000           Above 110000            11.054838
  40000 - 50000           Above 110000            10.513447
  50000 - 60000           Above 110000            9.081383
  60000 - 70000           Above 110000            8.539993
  70000 - 100000          Above 110000            18.389801
  Above  100000           Above 110000            18.040517
  Do not know             Above 110000            9.430667`
  </code></pre>
  
  <p>We want to make stacked bars of the data, showing the distribution between areas.</p>
  
  <p>This did it:    </p>
  
  <pre><code>dg=ggplot(data=frame, aes(x=Inhabitants, ymax=100, y=Percent,fill=eval(parse(text=special))))       
  g=g+geom_bar(stat="identity")
  g=g+theme_minimal()
  g=g+xlab("") + ylab("")
  g=g+theme(axis.text.y=element_blank(),axis.ticks.y=element_blank(),axis.ticks.x=element_blank()) 
  g=g+scale_fill_discrete("",guide = guide_legend(reverse=TRUE)) 
  g
  </code></pre>
  
  <p>Nice, we are getting exactly what we want. We just want to add some information: How many percent does each section represent? </p>
  
  <p>With the following code we are close:</p>
  
  <pre><code>g=g+geom_text(aes(label = paste(round(Percent,digits=1),"%"),y=Percent),size = 2,hjust = 0.4, vjust = 1.4, position ="stack") 
  </code></pre>
  
  <p>Getting this: <a href="http://s28.postimg.org/lv3zg2cnh/bars2.png" rel="nofollow">http://s28.postimg.org/lv3zg2cnh/bars2.png</a></p>
  
  <p>We just want to place the numbers in the middle of the sections. However, it turns out it is very difficult (for us) to do!</p>
  
  <p>We have tried code like the following, with no luck.</p>
  
  <pre><code>data=transform(frame,pos=round(ave(Percent,Inhabitants,FUN=cumsum)-Percent/2))
  g=ggplot(data, aes(x=Inhabitants, ymax=100, y=Percent, fill=eval(parse(text=special)))) 
  g=g+geom_bar(stat="identity")
  g=g+theme_minimal()
  g=g+xlab("") + ylab("")
  g=g+theme(axis.text.y=element_blank(),axis.ticks.y=element_blank(),axis.ticks.x=element_blank()) 
  g=g+scale_fill_discrete("",guide = guide_legend(reverse=TRUE))
  g=g+geom_text(aes(label = paste(round(Percent,digits=1),"%"),y=pos),size = 3,hjust = 0.4, vjust = 0, position ="stack") 
  g
  </code></pre>
  
  <p>We have checked SO for solutions. Without luck, due to our inexperience with R. After many many hours we are now giving up and would be satisfied with our first solution, were it not for the fact that when we are handling data-sets with more sections it often turns into a mess: <a href="http://s13.postimg.org/5jxavvohz/bars3.png" rel="nofollow">http://s13.postimg.org/5jxavvohz/bars3.png</a></p>
  
  <p>Our primary question is:</p>
  
  <p>1) How can we prevent labels with values less than 2 percent appearing.</p>
  
  <p>(Our secondary question is: How can we get the values positioned in the middle? )</p>