2

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")
Stacy
  • 25
  • 5

2 Answers2

2

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")
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
1

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

Duck
  • 39,058
  • 13
  • 42
  • 84
  • I don't want it displayed per column. I just want it to display the total number for that year. Is there a way to do that? – Stacy Jun 25 '20 at 18:07
  • @Stacy I have updated the solution. Hoping this can help. – Duck Jun 25 '20 at 19:32
  • Thank you! This is very helpful. I've tried to repeat this solution for another dataset, but instead of getting sum of FREQUENCY, I need to get count per year. I've adjusted my code to this: DRS_LF %>% left_join(DRS_LF %>% group_by(Year) %>% summarise(TT=count(DRS_LF, Year, na.rm=T))) %>% mutate(NewFacet=paste0(Year,' (N=',TT,')')) -> DF3 but it gives me this error when I try to run my histogram, Error: At least one layer must contain all faceting variables: `NewFacet`. * Plot is missing `NewFacet` * Layer 1 is missing `NewFacet`.......do you know what I'm doing wrong? – Stacy Jun 25 '20 at 21:55
  • @Stacy Try `OT_LF %>% left_join(OT_LF %>% group_by(Year) %>% summarise(TT=n())) %>%mutate(NewFacet=paste0(Year,' (N=',TT,')')) -> DF2` – Duck Jun 25 '20 at 22:18
  • @Stacy Or more precisely `DRS_LF %>% left_join(DRS_LF %>% group_by(Year) %>% summarise(TT=n())) %>% mutate(NewFacet=paste0(Year,' (N=',TT,')')) -> DF3` – Duck Jun 25 '20 at 22:21
  • It did not work. I am also trying to use a geom_histogram instead though. Could that be the issue? Here's my code:ggplot(DRS_LF, aes(x = LENGTH))+ geom_histogram( stat = "bin", position = "stack", binwidth = 3, fill = "black" )+ theme_bw()+ labs(x = "Length (mm)", y = "Count")+ facet_wrap(~Year, ncol = 1, dir="v") – Stacy Jun 25 '20 at 22:38
  • @Stacy I have included a sketch for histogram in the solution using Ian's data. Hope it can be useful. – Duck Jun 26 '20 at 00:45
  • Ugh yes! It worked and I realized what I did wrong! I was trying to use my original data file instead of the new one created for the summarized data. It's the little things that get ya! THANK YOU!! – Stacy Jun 26 '20 at 01:53