0

ggplot

I am trying to add total count (n) to each bar on my ggplot bar graph. Can anyone help?

Thanks.

mydf_tmp <- mydf %>%
 group_by() %>%
 summarise(Giant.Remora = sum(Giant.Remora),
           Sharksucker.Remora = sum(Sharksucker.Remora), 
           Juvenile.Remora = sum(Juvenile.Remora), 
           Black.Trevally = sum(Black.Trevally),
           Pilot.Fish = sum(Pilot.Fish)) %>% data.frame()

plot_data <- data.frame(Species = names(mydf_tmp), 
                        Total = as.numeric(as.character(mydf_tmp[1,])))

ggplot(data = plot_data) + 
 geom_bar(mapping=aes(x = Species, y = Total),
          stat = "identity", show.legend = FALSE) + 
 theme_gray(base_size = 14) + 
 labs(x = ~italic(M.birostris) ~ "Hitchhiker Species") + 
 labs(y = 'Total Presence') + 
 theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size = 14))   
Edo
  • 7,567
  • 2
  • 9
  • 19
  • Have you tried https://stackoverflow.com/questions/26553526/how-to-add-frequency-count-labels-to-the-bars-in-a-bar-graph-using-ggplot2 or https://stackoverflow.com/questions/2551921/show-frequencies-along-with-barplot-in-ggplot2 ? – Ronak Shah Oct 26 '20 at 08:29
  • I get an Error: stat_count() requires an x or y aesthetic. – Aimee Nicholson-Jack Oct 26 '20 at 08:46
  • 1
    Can you add your data using `dput` ? Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Oct 26 '20 at 09:00

0 Answers0