【问题标题】:Plot multiple group histogram with overlaid line ggplot用重叠线 ggplot 绘制多组直方图
【发布时间】:2015-06-14 20:28:43
【问题描述】:

我正在尝试使用重叠线绘制多组直方图,但我无法为直方图获得正确的缩放比例。 例如:

  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()

我将如何缩放它以便在直方图上覆盖一条黑线并且在 y 轴上是密度?

【问题讨论】:

  • 您是否尝试过更改位置参数?什么位置=闪避给你?另外,您确定要生成叠加直方图吗?对我来说更多的是堆叠的。尝试使用 alpha 参数来查看这一点。另外,stackoverflow.com/questions/5963269/…

标签: r ggplot2


【解决方案1】:

如果没有reproducible example,其他人将很难为您提供帮助,但也许您所追求的是这样的:

library(ggplot2)

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

如果您希望密度线与整个数据集相关,则需要将填充美学移动到 geom_histogram 函数中:

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

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 2019-08-19
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多