2015-06-14 148 views
1

我想绘制叠加直线的多组直方图,但我无法获得直方图的正确缩放比例。 例如:用叠加线绘制多组直方图ggplot

ggplot() + geom_histogram(data=df8,aes(x=log(Y),y=..density..),binwidth=0.15,colour='black') + 
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw() 

产生正确的比例。但是当我尝试按组进行填充时,每个组分别缩放。

ggplot() + geom_histogram(data=df8,aes(x=log(Y),fill=vec8,y=..density..),binwidth=0.15,colour='black') + 
    geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw() 

Picture

我怎么会缩放以黑线重叠在直方图和y轴密度?

+2

您是否尝试更改位置参数?什么职位=闪避给你?另外,你确定你正在生成覆盖直方图吗?是更多的堆叠给我。尝试玩alpha参数来看到这一点。另外,http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

回答

1

这将是很难让别人来帮助你没有reproducible example,但也许这样的事情是你追求的:

library(ggplot2) 

ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) + 
    geom_histogram(aes(y = ..density..)) + 
    geom_line(stat = "density") 

R plot

如果你宁愿密度线涉及到整个数据集,你需要填充的审美移动到geom_histogram功能:

ggplot(data = mtcars, aes(x = mpg)) + 
    geom_histogram(aes(y = ..density.., fill = factor(cyl))) + 
    geom_line(data = mtcars, stat = "density") 

R plot 2