Ho 将 geom_histogram 中计算的计数值添加到 R 中的条形顶部

Ho to add the count values calculated in the geom_histogram on top of the bars in R

我想在 ggplot2 上添加在 geom_histogram 函数中计算的计数值。我将 ggplot2 放入一个循环中,这样我就可以生成多个图,在我的例子中是 30 个,但为方便起见,这里是一个仅用于四个图的虚拟集。 Facet wrap 不起作用,因为几何密度在计算比例之前汇集了所有因素的数据,而不是在 factor/variable 内。为了制作这个情节,我基本上混合了来自各种来源的一大堆代码,所以归功于他们。

              library(dplyr)
              library(ggplot2)
              library(ggridges)
              library(reshape2)
              library(gridExtra)
       #Make the data#       
        df.fact <- data.frame("A"=rnorm(400, mean = 350, sd=160),"B"=rnorm(400, mean = 300, sd=100), "C"=rnorm(400, mean = 200, sd=80), names=rep(factor(LETTERS[23:26]), 100))
        df.test<-melt(df.fact, id.vars = "names", value.name = "Length2")
        names(df.test)[names(df.test) =="variable"] <- "TSM.FACT"

    #Create the plotlist##      
        myplots <- list()

    #Loop for plots##
        for(i in 1:(length(unique(df.test$names)))){ 
          p1 <- eval(substitute(
            ggplot(data=df.test[df.test$names == levels(df.test$names)[i],], aes(x=Length2, group=TSM.FACT, colour = TSM.FACT, fill=TSM.FACT)) +
              geom_histogram(aes( y = stat(width*density)), position = "dodge", binwidth = 50, alpha =0.4, show.legend=T)+
              ggtitle(paste0(levels(df.test$names)[i]))+
              geom_density_line(stat="density", aes(y=(..count..)/sum(..count..)*50), alpha=0.3, size=0.5, show.legend=F) +
              geom_vline(data=ddply(df.test[df.test$names == levels(df.test$names)[i],], ~ TSM.FACT,  numcolwise(mean)), mapping=aes(xintercept = Length2, group=TSM.FACT, colour=TSM.FACT),  linetype=2, size=1, show.legend=F) +
              scale_y_continuous(labels = percent_format()) +
              ylab("relative frequency") +
              scale_color_manual(values= c("#00B2EE",  "#1E90FF",  "#104E8B")) +
              scale_fill_manual(values= c("#00B2EE",  "#1E90FF", "#104E8B")) +
            theme_bw() + theme(
              plot.title = element_text(lineheight=0.5, hjust= 0.5, size=10),
              strip.text.y = element_text(hjust = 1, angle = 0),
              strip.text.x = element_text(size=10, vjust = 0.9), 
              strip.text=element_text(margin = margin(t=0.3,r=1,b=0.3,l=1), size=8, debug = F, vjust=0.2),
              strip.background = element_blank(),
              axis.text.x = element_text(size=8, angle=0, vjust=0.2, margin = margin(t=0.3,r=0.1,b=0.3,l=0.1)),
              axis.title.x=element_blank(),
              axis.title.y=element_blank(),
              axis.line.x=element_line(colour="black"),
              axis.line.y=element_line(colour="black"),
              panel.grid.minor = element_blank(), 
              panel.border=element_blank(),
              panel.background=element_blank(),  
              legend.position=(c(0.9,0.9)),
              legend.title = element_blank(), 
              legend.key = element_blank()),
            list(i = i)))
          print(i)
          print(p1)
          myplots[[i]] <- p1 
          plot(p1)
        }

#Join the plots
panelplot=grid.arrange(plotlist = myplots, grobs = myplots, shared.legend=T)

很遗憾,我无法重现您的示例。我可以建议添加一个列,其中包含每个柱的总和(我们将其命名为 "Bar")

ggplot 代码所需的添加包括:

geom_text(aes(label = Bar), position = position_stack(vjust = 1)) +

栏上方的文本高度可以用 vjust 调整