Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot条形图中规范化分组_R_Ggplot2 - Fatal编程技术网

R 在ggplot条形图中规范化分组

R 在ggplot条形图中规范化分组,r,ggplot2,R,Ggplot2,我的问题和这个问题中描述的一样:(我把这个问题作为一个新问题发布,因为我的声誉太低了,无法发表评论。)我希望蓝色的条和红色的条整合在一起,我认为这就是公认的答案。但它对我不起作用,因为我得到了以下错误: Aesthetics must be valid computed stats. Problematic aesthetic(s): y = ..density... Did you map your stat in the wrong layer? 当我完全按照vpipkt的建议做时: g

我的问题和这个问题中描述的一样:(我把这个问题作为一个新问题发布,因为我的声誉太低了,无法发表评论。)我希望蓝色的条和红色的条整合在一起,我认为这就是公认的答案。但它对我不起作用,因为我得到了以下错误:

Aesthetics must be valid computed stats. Problematic aesthetic(s): y = ..density... 
Did you map your stat in the wrong layer?
当我完全按照vpipkt的建议做时:

ggplot(dat, aes(x = bin, y = ..density.., group = source, fill = source)) +
geom_bar(alpha = 0.5, position = 'identity')
使用用户3396385提供的示例数据:

set.seed(47)
BG.restricted.hs = round(runif(100, min = 47, max = 1660380))
FG.hs = round(runif(1000, min = 0, max = 1820786))

dat = data.frame(x = c(BG.restricted.hs, FG.hs), 
            source = c(rep("BG", length(BG.restricted.hs)),
            rep("FG", length(FG.hs))))
dat$bin = cut(dat$x, breaks = 200)
使用
.count..
而不是
.density..
但是,没有错误:

ggplot(dat, aes(x = bin, y = ..count.., group = source, fill = source)) +
geom_bar(alpha = 0.5, position = 'identity')

但它与原始问题中的示例存在相同的问题,总体上权重较少的数据集不太可见。既然答案被接受了,我想它一定在某个时候起了作用。。你知道从那以后会出什么问题吗?如果有人能告诉我他们是否有同样的问题,我会非常高兴。感谢您的帮助:)

错误消息指出,
.density..
不可用,因此无法映射到
y
。我认为这是因为
.density..
不是由
geom_bar()
计算的

改用
.prop..

ggplot(dat,aes(x=bin,group=source,fill=source))+
几何图形条(alpha=0.5,位置='identity',aes(y=…prop.)

谢谢!也许你也可以把这个答案贴到原来的问题上?然后我的可以被标记为重复或删除。。