绘制具有重叠线ggplot的多组直方图

时间:2015-06-14 20:28:44

标签: r ggplot2

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

  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轴上覆盖密度?

1 个答案:

答案 0 :(得分:1)

如果没有reproducible example,其他人很难帮助你,但也许这就是你所追求的:

geom_histogram

enter image description here

如果您希望密度线与整个数据集相关,则需要将填充美学移到ggplot(data = mtcars, aes(x = mpg)) + geom_histogram(aes(y = ..density.., fill = factor(cyl))) + geom_line(data = mtcars, stat = "density") 函数中:

{{1}}

R plot