调整条上的geom_histogram标签与y轴上的正确比例

时间:2018-11-21 12:41:26

标签: r ggplot2

我有这个情节

ggplot(data, aes(D3)) + 
geom_histogram(aes(fill=(..count..)/sum(..count..)), stat = "count", na.rm = TRUE, binwidth =1) +
geom_text(aes(y = ((..count..)), label = scales::percent( (..count..)/sum(..count..)) ), stat = "count", vjust = -0.25) +
scale_fill_gradient("", low = "green", high = "red")

输出一个很好的直方图 enter image description here

如您所见,y轴显示计数而不是百分比。

另一方面,如果我切换到

ggplot(data, aes(D3)) + 
    geom_histogram(aes(y = (..density..)), binwidth = 1) + 
    geom_text(aes( label = format(100*..density.., digits=1,), y= ..density.. ), stat= "bin", binwidth =1, vjust = -0.2 ) + 
    scale_y_continuous(labels = scales::percent)

它输出这个(非常难看)的图,在y轴上为零

enter image description here

如何在第一幅图上调整y轴,即显示百分比而不是计数?或在第二个秒中删除那些零?

0 个答案:

没有答案