Stacy
Stacy

Reputation: 25

Display Counts on a ggplot

I want to display the total number of lengths per year on my graphs. How do I display the total number of lengths (FREQUENCY) per year on my graph? I do NOT want it displayed above each bar. I want the total number for that year.

ggplot(data = OT_LF, aes(x = Length_mm, y = FREQUENCY))+
geom_bar(stat="identity", width = 10, fill = "black")+
theme_bw()+
labs(x = "Length (mm)", y = "Count")+
facet_wrap(~Year, ncol = 1, dir="v")

Here's my data:

dput(head(OT_LF))
structure(list(ID = c(20154113L, 20154113L, 20154113L, 20154113L, 
20154113L, 20154113L), CRUCODE = c(20154L, 20154L, 20154L, 20154L, 
20154L, 20154L), Cruise = c(4L, 4L, 4L, 4L, 4L, 4L), Year = c(2015L, 
2015L, 2015L, 2015L, 2015L, 2015L), Month = c(8L, 8L, 8L, 8L, 
8L, 8L), STRATUM = c(16L, 16L, 16L, 16L, 16L, 16L), TOW = c(16L, 
16L, 16L, 16L, 16L, 16L), STA = c(113L, 113L, 113L, 113L, 113L, 
113L), YRMODA = c(20150821L, 20150821L, 20150821L, 20150821L, 
20150821L, 20150821L), MINOUT = c(20, 20, 20, 20, 20, 20), SPP = c(136L, 
136L, 136L, 136L, 136L, 136L), LENGTH = c(28L, 24L, 29L, 25L, 
23L, 26L), Length.mm. = c(280L, 240L, 290L, 250L, 230L, 260L), 
FREQUENCY = c(2L, 4L, 1L, 3L, 3L, 2L), Length_mm = c(280, 
240, 290, 250, 230, 260)), row.names = 4257:4262, class = "data.frame")

Upvotes: 2

Views: 138

Answers (2)

Duck
Duck

Reputation: 39585

Try this (Updated):

library(dplyr)
library(ggplot2)

OT_LF %>% left_join(OT_LF %>% group_by(Year) %>% summarise(TT=sum(FREQUENCY,na.rm=T))) %>%
  mutate(NewFacet=paste0(Year,' (N=',TT,')')) -> DF2

ggplot(data = DF2, aes(x = Length_mm, y = FREQUENCY))+
  geom_bar(stat="identity", width = 10, fill = "black")+
  theme_bw()+
  labs(x = "Length (mm)", y = "Count")+
  facet_wrap(~NewFacet, ncol = 1, dir="v")

enter image description here

Updated for histogram:

OT_LF %>% left_join(OT_LF %>% group_by(Year) %>% summarise(TT=n())) %>%
    mutate(NewFacet=paste0(Year,' (N=',TT,')')) -> DF2

ggplot(data = DF2, aes(x = Length_mm))+
    geom_histogram( stat = "bin", position = "stack", binwidth = 3, fill = "black" )+
    theme_bw()+
    labs(x = "Length (mm)", y = "Count")+
    facet_wrap(~NewFacet, ncol = 1, dir="v")

enter image description here

Upvotes: 1

Ian Campbell
Ian Campbell

Reputation: 24770

Here's an approach with the ..PANNEL.. special symbol:

ggplot(data = OT_LF, aes(x = Length_mm, y = FREQUENCY))+
  geom_bar(stat="identity", width = 10, fill = "black")+
  geom_text(aes(label = paste0("Total: ",tapply(OT_LF$FREQUENCY, OT_LF$Year, sum)[..PANEL..]),
                x = diff(range(Length_mm))/2 + min(Length_mm),
                y = max(FREQUENCY) + 1)) + 
  scale_y_continuous(expand = expansion(mult = c(0, .2))) + 
  theme_bw()+
  labs(x = "Length (mm)", y = "Count")+
  facet_wrap(~Year, ncol = 1, dir="v")

enter image description here Edited Sample Data

OT_LF <- structure(list(ID = c(20154113L, 20154113L, 20154113L, 20154113L, 
20154113L, 20154113L), CRUCODE = c(20154L, 20154L, 20154L, 20154L, 
20154L, 20154L), Cruise = c(4L, 4L, 4L, 4L, 4L, 4L), Year = c(2015L, 
2015L, 2015L, 2015L, 2018L, 2018L), Month = c(8L, 8L, 8L, 8L, 
8L, 8L), STRATUM = c(16L, 16L, 16L, 16L, 16L, 16L), TOW = c(16L, 
16L, 16L, 16L, 16L, 16L), STA = c(113L, 113L, 113L, 113L, 113L, 
113L), YRMODA = c(20150821L, 20150821L, 20150821L, 20150821L, 
20150821L, 20150821L), MINOUT = c(20, 20, 20, 20, 20, 20), SPP = c(136L, 
136L, 136L, 136L, 136L, 136L), LENGTH = c(28L, 24L, 29L, 25L, 
23L, 26L), Length.mm. = c(280L, 240L, 290L, 250L, 230L, 260L), 
FREQUENCY = c(2L, 4L, 1L, 3L, 3L, 2L), Length_mm = c(280, 
240, 290, 250, 230, 260)), row.names = 4257:4262, class = "data.frame")

Upvotes: 2

Related Questions