【发布时间】:2014-07-15 08:58:56
【问题描述】:
我正在尝试使用 geom_bar 创建结果的直方图,并希望将结果计数作为文本包含在与 x 轴对齐的每个条形中。
这是我目前所拥有的:
df <- data.frame(results = rnorm(100000))
require(ggplot2)
p = ggplot(df, aes(x = results))
p = p + geom_bar(binwidth = 0.5, colour = "black", fill = "white", drop = T)
p = p + scale_y_log10()
p = p + geom_hline(aes(yintercept = 1))
p = p + stat_bin(geom = "text", binwidth = 0.5,
aes(x = results, angle = 90, y = 2,
label = gsub(" ", "",format(..count.., big.mark = ",",
scientific=F))))
p
如您所见,文本未与 x 轴对齐,并且由于我的真实数据要大得多(以百万计),这个问题会变得更糟:
当前图:
想要的图:
注意:通过在 stat_bin 中设置 y = 3,我会收到一条警告,指出“将变量映射到 y 并使用 stat="bin".etc...”,但我不确定如何强制文本的位置位于图表底部而不定义 y 值。
【问题讨论】:
-
附注:当我运行您的代码时,我收到一个错误(不是警告):“
Error : Mapping a variable to y and also using stat="bin”。 ggplot2_1.0.0