0

how to show the count and total for each bar seperatedly sample image

 ggplot(dealflow_summary_fiscal_yr,aes(x=Total,y=Type,fill=status))+
      geom_bar(stat='identity',width=0.2)+theme_classic()+
      geom_text(aes(label = stat(x),group = Type),stat = "summary",fun = sum,vjust = .20,hjust= 1)+
      theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom",
            axis.text.x = element_text(face = "bold", color = "black", size = 10, angle = 45, hjust = 1))+
      labs(x="", y="", fill="")+
      scale_fill_manual(values=c("#284a8d", "#00B5CE"))
Fariya
  • 234
  • 1
  • 11

1 Answers1

0

Calculate the totals for each Type separately and plot them in separate geom_text.

library(dplyr)
library(ggplot2)

Total_data <- dealflow_summary_fiscal_yr %>% 
                 group_by(Type) %>% summarise(Total = sum(Total))

dealflow_summary_fiscal_yr %>%
  group_by(Type, status) %>%
  summarise(Total = sum(Total)) %>%
  ggplot() + aes(x=Total,y=Type,fill=status)+
  geom_bar(stat='identity',width=0.2)+
  theme_classic() +
  geom_text(aes(label = Total)) +
  geom_text(data = Total_data, aes(x = Total, y = Type, label = Total), 
            inherit.aes = FALSE) + 
  theme(axis.line.y = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "bottom",
        axis.text.x = element_text(face = "bold", color = "black", 
                                   size = 10, angle = 45, hjust = 1))+
  labs(x="", y="", fill="")+
  scale_fill_manual(values=c("#284a8d", "#00B5CE"))

You might need to adjust the labels using hjust, vjust in geom_text based upon your complete data so that they are at correct position.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213