title: Count and Percent Together using Stack Bar in R
body: <p>I am trying to create stack bar with counts and percent in same graph. I took help from <a href="https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2">Showing data values on stacked bar chart in ggplot2</a> and add group total and plotted my as <img src="https://i.stack.imgur.com/SyuAD.jpg" alt="on this image"></p>
  
  <pre><code>By using code 
  
  ### to plot stacked bar graph with total on the top and
  ###    distribution of the frequency;
  
  library(ggplot2);
  library(plyr);
  library(dplyr);
  
  Year      &lt;- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
  Category  &lt;- c(rep(c("A", "B", "C", "D"), times = 4))
  Frequency &lt;- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
  Data      &lt;- data.frame(Year, Category, Frequency);
  
  
  sum_count &lt;- 
     Data %&gt;%
    group_by(Year) %&gt;%
    summarise(max_pos = sum(Frequency));
  
  sum_count;
  
  
  Data &lt;- ddply(Data, .(Year), transform, pos = 
  cumsum(Frequency) - (0.5 * Frequency));
  
  Data;
  
  
  
  # plot bars and add text
  p &lt;- ggplot(Data, aes(x = Year, y = Frequency)) +
       geom_bar(aes(fill = Category), stat="identity") +
       geom_text(aes(label=Frequency,y = pos), size = 3) +  
       geom_text(data = sum_count, 
       aes(y = max_pos, label = max_pos), size = 4,
       vjust = -0.5);
  
  print(p);
  </code></pre>
  
  <p>/<em>Now I want to overlay percent of each group with counts This is my         approach.merge data such a way that we can calculate
  % for each of the group you are dealing with</em>/</p>
  
  <pre><code>    MergeData &lt;- merge(Data,sum_count,by="Year");
  
      MergeData &lt;- transform(MergeData,
      per_cent=round((pos/max_pos)*100,0));
      MergeData&lt;- ddply(MergeData, .(Year), transform, per_pos = 
      cumsum(per_cent) - (0.5 * per_cent));
  
      # calculate percent and attach % sign;
  
      MergeData &lt;- transform(MergeData,
      per_cent=paste(round((pos/max_pos)*100,0),"%"));
  
      # Data only with percents
  
      Percent_Data &lt;- subset(MergeData,select 
      = c("Year","Category","per_cent","per_pos"));
  </code></pre>
  
  <p>/<em>I am wondering if it is possible to overlay percent data to the image I created using previous code so that number and percent can be presented together.</em>/</p>