0

I've run this code. Giving the results displayed. I now want to add count numbers for each individual category of each stacked bar.

dat$SU<-as.factor(dat$SU) 
#INVENTORY PER SU
ggplot(dat, aes(x=SU,fill=factor(SCIENTIFIC_NAME)))+
  geom_bar(width=0.5)+xlab("Sampling Unit (SU)")+
  ylab("Count")+labs(fill="SCIENTIFIC NAME")+
  ggtitle("Inventory per sampling unit (SU) by species")+ 
  scale_x_discrete(breaks=c(1,2,3,4,5,6,7,8,10,11,15))

stacked barplot per su

Aziz
  • 20,065
  • 8
  • 63
  • 69
  • Hi OP, have you seen the answers posted to these questions? [Question 1](https://stackoverflow.com/questions/26553526/how-to-add-frequency-count-labels-to-the-bars-in-a-bar-graph-using-ggplot2). [Question 2](https://stackoverflow.com/questions/2551921/show-frequencies-along-with-barplot-in-ggplot2). – chemdork123 Jul 01 '20 at 18:10

1 Answers1

0

It would be nice if you can provide some data. In meantime you can try something like this:

 df %>%
  count(SU, SCIENTIFIC_NAME) %>%
  group_by(SU) %>%
  mutate(y_pos = rev(n/2) + cumsum(c(0, head(rev(n), -1)))) %>%
  ggplot(aes(x = SU)) +
    geom_col(aes(y = n, fill = factor(SCIENTIFIC_NAME))) + 
    geom_text(aes(y = y_pos, label = n)) +
 ...

Counting before ggplot and using geom_col (its same as geom_bar with stat = identity) and adding geom_text for adding text. ... represent rest of code u want to add.

det
  • 5,013
  • 1
  • 8
  • 16