在ggplot中向geom_histogram添加文本的正确方法是什么?

我已经绘制了一个直方图,x 轴和 y 轴显示工资,显示数据集中具有此特定工资的个人百分比。现在我希望各个条形显示每个条形中有多少个观察结果。例如,在我提供的 sample_data 中,10% 的条形中有多少工资,20% 的条形中有多少?

这是我的数据的一个小样本:


sample_data<- structure(list(wage = c(81L, 77L, 63L, 84L, 110L, 151L, 59L, 
                                109L, 159L, 71L), school = c(15L, 12L, 10L, 15L, 16L, 18L, 11L, 
                                                             12L, 10L, 11L), expr = c(17L, 10L, 18L, 16L, 13L, 15L, 19L, 20L, 
                                                                                      21L, 20L), public = c(0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L), 
                       female = c(1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), industry = c(63L, 
                                                                                        93L, 71L, 34L, 83L, 38L, 82L, 50L, 71L, 37L)), row.names = c("1", 
                                                                                                                                                     "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame")

这是我的 R 脚本

library(ggplot2)
library(dplyr)

ggplot(data = sample_data) +
  geom_histogram(aes(x = wage, y = stat(count) / sum(count)), binwidth = 4, color = "black") + 
  scale_x_continuous(breaks = seq(0, 300, by = 20)) +
  scale_y_continuous(labels = scales::percent_format()) 

我基本上对此很满意,但无论我尝试什么——我都无法在我的专栏顶部显示文字。这是许多使用 stat_count 不起作用的示例:

ggplot(data = sample_data) +
  geom_histogram(aes(x = wage, y = stat(count) / sum(count)), binwidth = 4, color = "black") + 
  scale_x_continuous(breaks = seq(0, 300, by = 20)) +
  scale_y_continuous(labels = scales::percent_format()) + 
  stat_count(aes(y = ..count.., label =..count..), geom = "text")

我也试过使用 geom_text 无济于事。

编辑:回答!

也非常感谢回答的人。我最终使用了 teunbrand 的解决方案,并稍作修改,将 after_stat(density) 更改为 after_stat(count) / sum(count)。

这是“最终”代码:

ggplot(sample_data) +
  geom_histogram(
    aes(x = wage,
        y = after_stat(count) / sum(count)),
    binwidth = 4, colour = "black"
  ) +
  stat_bin(
    aes(x = wage,
        y = after_stat(count) / sum(count),
        label = after_stat(ifelse(count == 0, "", count))),
    binwidth = 4, geom = "text", vjust = -1) + 
  scale_x_continuous(breaks = seq(0, 300, by = 20)) +
  scale_y_continuous(labels = scales::percent_format()) 

回答

不同的层通常不共享状态信息,因此您可以使用与直方图 ( stat_bin())相同的统计数据来显示标签。然后,您可以使用after_stat()图层的 stat 部分的计算变量来制作标签。

library(ggplot2)

sample_data<- structure(list(
  wage = c(81L, 77L, 63L, 84L, 110L, 151L, 59L, 109L, 159L, 71L), 
  school = c(15L, 12L, 10L, 15L, 16L, 18L, 11L, 12L, 10L, 11L), 
  expr = c(17L, 10L, 18L, 16L, 13L, 15L, 19L, 20L, 21L, 20L), 
  public = c(0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L),
  female = c(1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), 
  industry = c(63L, 93L, 71L, 34L, 83L, 38L, 82L, 50L, 71L, 37L)), 
  row.names = c("1","2", "3", "4", "5", "6", "7", "8", "9", "10"), 
  class = "data.frame")

ggplot(sample_data) +
  geom_histogram(
    aes(x = wage,
        y = after_stat(density)),
    binwidth = 4, colour = "black"
  ) +
  stat_bin(
    aes(x = wage,
        y = after_stat(density),
        label = after_stat(ifelse(count == 0, "", count))),
    binwidth = 4, geom = "text", vjust = -1
  )

由reprex 包(v1.0.0)于 2021 年 3 月 28 日创建


以上是在ggplot中向geom_histogram添加文本的正确方法是什么?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>