到目前为止,我已经错过了一个直方图函数,在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)
解决方案是使用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.

| 归档时间: |
|
| 查看次数: |
4647 次 |
| 最近记录: |