Search code examples
rgraphggplot2stat

Count and Percent Together using Stack Bar in R


I am trying to create stack bar with counts and percent in same graph. I took help from Showing data values on stacked bar chart in ggplot2 and add group total and plotted my as on this image

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      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency);


sum_count <- 
   Data %>%
  group_by(Year) %>%
  summarise(max_pos = sum(Frequency));

sum_count;


Data <- ddply(Data, .(Year), transform, pos = 
cumsum(Frequency) - (0.5 * Frequency));

Data;



# plot bars and add text
p <- 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);

/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/

    MergeData <- merge(Data,sum_count,by="Year");

    MergeData <- transform(MergeData,
    per_cent=round((pos/max_pos)*100,0));
    MergeData<- ddply(MergeData, .(Year), transform, per_pos = 
    cumsum(per_cent) - (0.5 * per_cent));

    # calculate percent and attach % sign;

    MergeData <- transform(MergeData,
    per_cent=paste(round((pos/max_pos)*100,0),"%"));

    # Data only with percents

    Percent_Data <- subset(MergeData,select 
    = c("Year","Category","per_cent","per_pos"));

/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./


Solution

  • I think you are almost there. Use MergeData as the source for the data frame and add one more call to geom_text

    p <- ggplot(MergeData, aes(x = Year, y = Frequency, group = Category)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label=Frequency,y = pos), size = 3, vjust = 1) +  
     geom_text(
            aes(y = max_pos, label = max_pos), size = 4,
            vjust = -.5) + 
     geom_text(aes(x = Year, y = pos, label = per_cent), vjust = -1, size = 4)
    
      print(p);
    

    You may need to fiddle with hjust and vjust to get the text just how you like it.