如何使用 facet_grid 将计数标签添加到直方图的每个条形?

How can I add the counts label to each bar of a histogram with facet_grid?

我正在尝试为 Shiny 中直方图的每个条添加计数标签。我发现这是一个常见问题,我尝试了很多方法来解决这个问题,但对我的数据没有任何效果。

正如你在图片中看到的,计数在每个条形内部,并且有很多 1,我不知道为什么。

我想在每个条形图的 y 轴(频率)中显示它们的计数。但不在内部,在每个栏上方。

谁能帮帮我?

这是我的代码:

library(shiny)
library(ggplot2)
library(scales)

################### DATA ########################
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"

val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"

df3 <- rbind(df1, df2)


################ SHINY APP ########################
ui <- fluidPage(
  
  titlePanel("Histogram"),
  
  sidebarLayout(
    sidebarPanel(
    ),
    
    mainPanel(
      plotOutput("hist"),
    )
  )
)

server <- function(input, output) {
  
  output$hist <- renderPlot(height=700,{
    p <- ggplot(df3, aes(val, fill=type)) +
      geom_histogram(position = "identity", colour = "grey40", bins = 10) +
      ggtitle("Here must be a title") +
      xlab("Values") +
      ylab("Frequency") +
      facet_grid(type ~ .) + 
      scale_x_continuous(breaks=pretty(as.matrix(df3$val), n=10))
    
    p <- p + stat_count(geom="text", aes(label=..count..))
    p + theme(strip.text.x = element_blank(),
              strip.text.y = element_blank())
    
    
  })
  
} 

shinyApp(ui = ui, server = server)

非常感谢,

此致

你可以使用这个:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  
  titlePanel("Histogram"),
  
  sidebarLayout(
    sidebarPanel(
    ),
    
    mainPanel(
      plotOutput("hist"),
    )
  )
)

server <- function(input, output) {
  
  output$hist <- renderPlot(height=700,{
    ggplot(df3, aes(val, fill=type)) +
      geom_histogram(position = "identity", colour = "grey40", bins = 10) +
      ggtitle("Here must be a title") +
      xlab("Values") +
      ylab("Frequency") +
      facet_grid(type ~ .) + 
      stat_bin(bins = 10, geom="text", colour="black", size=3.5,
               aes(label=..count..), position=position_stack(vjust=1.15)) + 
      scale_x_continuous(breaks=pretty(as.matrix(df3$val), n=10)) + 
      theme(strip.text.x = element_blank(),
            strip.text.y = element_blank())
    
  })
  
} 

shinyApp(ui = ui, server = server)

根据您的喜好调整 vjust 位置。