在qplot/ggplot中具有分数的直方图

Mat*_*ert 4 r ggplot2

在此输入图像描述到目前为止,我已经错过了一个直方图函数,在y轴上有一个分数.像这样:

require(ggplot2)
data(diamonds)
idealD <- diamonds[diamonds[,"cut"]=="Ideal",]

fracHist <- function(x){
  frac <- (hist(x,plot=F)$counts) / (sum(hist(x,plot=F)$counts))
  barplot(frac)
}

### call
fracHist(idealD$carat)
Run Code Online (Sandbox Code Playgroud)

它不漂亮,但基本上应该解释我想要的东西:酒吧高度应该加起来一个.此外,断裂应标记x轴.我很想创建具有相同ggplot2 but can't figure out how to get around plotting the frequencies of压裂instead of plotting压裂itself.

all I get with `ggplot` is density...

m <- ggplot(idealD, aes(x=carat)) 
m + geom_histogram(aes(y = ..density..)) + geom_density()
Run Code Online (Sandbox Code Playgroud)

And*_*rie 8

解决方案是使用stat_bin和绘制美学y=..count../sum(..count..)

library(ggplot2)
ggplot(idealD, aes(x=carat)) + stat_bin(aes(y=..count../sum(..count..)))
Run Code Online (Sandbox Code Playgroud)

从快速扫描?hist我无法找到值如何分箱hist.这意味着图表将不相同,除非你摆弄了binwidth论证stat_bin.

在此输入图像描述