在 ggplot 上显示计数

Display Counts on a ggplot

我想在图表上显示每年的长度总数。如何在图表上显示每年的长度总数 (FREQUENCY)?我不希望它显示在每个栏上方。我要那一年的总数。

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")

这是我的数据:

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")

试试这个(更新):

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")

已更新直方图:

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")

这是一个使用 ..PANNEL.. 特殊符号的方法:

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")

已编辑示例数据

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")