首页 > 解决方案 > 如何将自定义标签添加到没有零的分组频率 stat_bin 图中?

问题描述

我有一个频率直方图,有 42 个组,每个框代表一个单独的观察/行。我需要用原始x值(即estimate)标记每个“单元格”。然而,ggplot2 似乎在每个单元格的底部和顶部添加了大量多余的标签(见下文)。

错误的输出

我假设 ggplot2 在..count.. == 0. 确实,添加label=ifelse(..count.. == 0, "", ..x..)正确绘制..x..变量,但这..x..不是原始估计。看:

在此处输入图像描述

生成它的代码在这里:

library(ggplot2)

mydata = structure(list(estimate = c(cor = 0.325795456913319, cor = 0.562197877060912, 
                                     cor = 0.440719760612754, cor = -0.0936850084700603, cor = 0.0360156238340214, 
                                     cor = 0.290449045144756, cor = 0.351442182968952, cor = 0.282652330413659, 
                                     cor = 0.484382008605981, cor = 0.555190439953125, cor = 0.153963602626727, 
                                     cor = 0.389799442186418, cor = 0.102658050525012, cor = 0.539213427685732, 
                                     cor = 0.599952880067505, cor = 0.353135730646411, cor = 0.5459587711875, 
                                     cor = 0.380085983041004, cor = 0.494013540678857, cor = 0.506029397264374, 
                                     cor = 0.796184962852028, cor = 0.152349436981737, cor = 0.474356676277947, 
                                     cor = 0.585975728042781, cor = 0.278773851537417, cor = 0.380637414940095, 
                                     cor = 0.392275909026939, cor = 0.419554193309306, cor = 0.488358015824324, 
                                     cor = 0.199407247922171, cor = 0.260254145583898, cor = 0.349291291301302, 
                                     cor = 0.464177992152635, cor = 0.0747318120424813, cor = 0.60432048579698, 
                                     cor = 0.295662258461811, cor = 0.0278690641141737, cor = -0.0337558821556421, 
                                     cor = 0.211670641689536, cor = 0.285200869849266, cor = 0.51828476555577, 
                                     cor = 0.44882613302634), groupid = 1:42, 
                        magnitiude = structure(c(4L, 5L, 4L, 1L, 2L, 3L, 4L, 3L, 4L, 5L, 3L, 4L, 3L, 5L, 5L, 4L, 5L, 
                                                 4L, 4L, 5L, 5L, 3L, 4L, 5L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 
                                                 2L, 5L, 3L, 2L, 1L, 3L, 3L, 5L, 4L), 
                       .Label = c("Negative", "Negligible", "Small", "Medium", "Large"), class = "factor")), 
                   row.names = c(NA, -42L), class = c("tbl_df", "tbl", "data.frame"))


ggplot(data = mydata, aes(estimate)) +
  stat_bin(aes(fill = magnitiude, group = groupid, label=estimate), color = "#424242", binwidth = 0.05) +
  stat_bin(binwidth=0.05, geom="text", aes(label=round(estimate,2), group = groupid), position=position_stack(vjust=0.5))

谁能帮我在每个分组单元格中生成原始估计值?

标签: rggplot2plot

解决方案


这是该功能的合理用例stage()。它允许您设置一种美学,您可以在以后的绘图过程中对其进行修改。

library(ggplot2)

ggplot(data = mydata, aes(estimate)) +
  stat_bin(aes(fill = magnitiude, 
               group = groupid), 
           color = "#424242", binwidth = 0.05) +
  stat_bin(
    binwidth=0.05, geom="text", 
    aes(label = stage(mydata$estimate, 
                      after_stat = ifelse(count > 0, round(label, 2), "")), 
        group = groupid), 
    position=position_stack(vjust=0.5)
  )
#> Warning: Use of `mydata$estimate` is discouraged. Use `estimate` instead.

由于我不明白的原因,它告诉我它无法找到该列,除非我在暂存中estimate添加前缀。mydata$而根据文档,它应该能够找到该estimate列。


推荐阅读