正火y轴中的R ggplot直方图比例(Normalizing y-axis in histogra

2019-06-17 14:52发布

我有一个非常简单的问题使我一声我的头挂在墙上。

我想是按比例的我的直方图的y轴,以反映比例(0到1),该每个区间构成,代替具有条的面积之和为1,作为使用Y = .. ..密度确实,或具有最高杆1时,为y = .. .. NCOUNT一样。

我输入名称和值,格式化像这样的列表:

name    value
A   0.0000354
B   0.00768
C   0.00309
D   0.000123

我的一个失败的尝试:

library(ggplot2)
mydataframe < read.delim(mydata)
ggplot(mydataframe, aes(x = value)) +
geom_histogram(aes(x=value,y=..density..))

这使我区1直方图,但2000和1000的高度:

和y = .. NCOUNT ..让我用最高的酒吧1.0直方图和休息缩放到它:

但我想有第一杆具有0.5的高度,并且另外两个0.25。

R没有任何认识的scale_y_continuous这些用途。

scale_y_continuous(formatter="percent")
scale_y_continuous(labels = percent)
scale_y_continuous(expand=c(1/(nrow(mydataframe)-1),0)

感谢您的任何帮助。

Answer 1:

需要注意的是..ncount..再缩放到最大1.0,而..count..是非缩放库计数。

ggplot(mydataframe, aes(x=value)) +
  geom_histogram(aes(y=..count../sum(..count..)))

这使:



Answer 2:

作为GGPLOT2 0.9的,许多的格式化器的功能已被移动到秤包,包括percent_format()

library(ggplot2)
library(scales)

mydataframe <- data.frame(name = c("A", "B", "C", "D"),
                          value = c(0.0000354, 0.00768, 0.00309, 0.000123))

ggplot(mydataframe) + 
  geom_histogram(aes(x = value, y = ..ncount..)) +
  scale_y_continuous(labels = percent_format())


Answer 3:

由于采用最新和最伟大的GGPLOT2 3.0.0版本,格式已经改变。 现在,您可以包装y价值stat()而不是搞乱..东西。

ggplot(mydataframe, aes(x = value)) +
  geom_histogram(aes(y = stat(count / sum(count))))


文章来源: Normalizing y-axis in histograms in R ggplot to proportion