0

I have seen some similar questions on here but nothing that fits what I'm looking for, as those don't describe how to add just a single percentage from a group fill. I saw how to add a count and percentage to the same bar but I want the counts for each fill category and the percentage for only one of those fills. I have a ggplot2 bar graph (see below) that shows the counts for each fill category. I'd like to include the percentage at the top of each bar that is "No Rework", but not the percentage that falls into the other categories. So for Q4 2020 at the top of the bar chart, it would show 75.86%. I've included the code used to generate the plot and the data. Any help would be greatly appreciated!

Data:

structure(list(q_year = c("Q1 2020", "Q1 2020", "Q1 2020", "Q2 2020", 
"Q2 2020", "Q2 2020", "Q3 2020", "Q3 2020", "Q3 2020", "Q4 2020", 
"Q4 2020", "Q4 2020", "Q1 2019", "Q2 2019", "Q3 2019", "Q4 2019"
), prod_bus_grp = c("CRM", "CRM", "CRM", "CRM", "CRM", "CRM", 
"CRM", "CRM", "CRM", "CRM", "CRM", "CRM", "CRM", "CRM", "CRM", 
"CRM"), rework_status = c("CO Rework", "Content Rework", "No Rework", 
"CO Rework", "Content Rework", "No Rework", "CO Rework", "Content Rework", 
"No Rework", "CO Rework", "Content Rework", "No Rework", "No Rework", 
"No Rework", "No Rework", "No Rework"), count = c(6, 2, 4, 9, 
3, 23, 12, 1, 29, 6, 1, 22, 0, 0, 0, 0), quarter = c(1, 1, 1, 
2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 2, 3, 4), percent = c(50, 16.7, 
33.3, 25.71, 8.57, 65.71, 28.57, 2.38, 69.05, 20.69, 3.45, 75.86, 
0, 0, 0, 0)), row.names = c(NA, -16L), class = c("tbl_df", "tbl", 
"data.frame"))

Code

ggplot(CRM_df_2, aes(q_year, count, fill = rework_status)) +
  geom_bar(stat = "identity", color = "black", width = 1) +
  geom_text(aes(label= count),position=position_stack(0.5), size = 6, color = "black") +
  facet_wrap(.~quarter,scales='free_x',nrow = 1,strip.position = 'bottom')+
  theme(strip.text = element_blank(),
        strip.placement = 'outside',
        legend.position = "bottom",
        legend.title = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        plot.title = element_text(hjust = 0.5),
        legend.spacing.x = unit(1.5, 'cm'))+
  labs(title = "CRM First Pass Yield") + scale_fill_manual(values = c("No Rework" = "yellowgreen", "Content Rework" = "royalblue", "CO Rework" = "steelblue1"))

enter image description here

Matt Simonson
  • 105
  • 1
  • 7
  • "I have seen some similar questions on here but nothing that fits what I'm looking for." - please kindly consider adding those questions and your thoughts as to why it doesn't help to, so we don't need to reinvent the wheel. – tjebo Jan 21 '21 at 15:52
  • Edited to include why those are different. – Matt Simonson Jan 21 '21 at 15:57

1 Answers1

1

You can isolate the desired labels in a new dataframe and use another geom_text():

library(ggplot2)
#Data for new labels
Labels <- subset(CRM_df_2,rework_status=='No Rework')
Labels <- Labels[Labels$percent!=0,]
Labels$y <- c(15,37,46,31)
#Plot
ggplot(CRM_df_2, aes(q_year, count, fill = rework_status)) +
  geom_bar(stat = "identity", color = "black", width = 1) +
  geom_text(aes(label= count),
            position=position_stack(0.5), size = 6, color = "black") +
  geom_text(data=Labels,aes(x=q_year,y=y,label=percent))+
  facet_wrap(.~quarter,scales='free_x',nrow = 1,strip.position = 'bottom')+
  theme(strip.text = element_blank(),
        strip.placement = 'outside',
        legend.position = "bottom",
        legend.title = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        plot.title = element_text(hjust = 0.5),
        legend.spacing.x = unit(1.5, 'cm'))+
  labs(title = "CRM First Pass Yield") +
  scale_fill_manual(values = c("No Rework" = "yellowgreen",
                               "Content Rework" = "royalblue",
                               "CO Rework" = "steelblue1"))

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84