r - 如何将自定义标签添加到没有零的分组频率 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))
谁能帮我在每个分组单元格中生成原始估计值?
解决方案
这是该功能的合理用例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
列。
推荐阅读
- regex - 正则表达式的行首错误 486 但行尾有效?
- android - Android OpenGL 2.0 Low FPS 如何解决?
- sql - 通过使用另一个表中的计数进行初始化来更改表
- python - 继续获取:找不到“password_reset_confirm”的反向。'password_reset_confirm' 不是有效的视图函数或模式名称
- web - 无法在所有浏览器上访问所有谷歌相关网站,例如 google.com、YouTube、gmail 等,但所有其他网站都可以
- redux - 如何使用自定义中间件进行异步操作
- elasticsearch - 过滤对象数组上的不同字段
- flutter - Flutter Provider,在哪里放置函数调用以从 firestore 数据库加载数据
- r-markdown - !LaTeX 错误:包 fontspec 的选项冲突
- c++ - 从客户端接收到第一个数据包后,C ++ UDP Socket无法从服务器发送回客户端