【问题标题】:Histogram, Density Plot, Quantile Line直方图、密度图、分位数线
【发布时间】:2014-12-07 06:03:15
【问题描述】:

我有一个回归系列,我已经绘制了直方图,但我需要在其中添加密度曲线和 0.05 分位数线。我怎样才能简单地做到这一点?

我的直方图代码:

ggplot(MyData, aes(x=Returns)) + geom_histogram(binwidth=1, colour="black", fill="white")

我尝试添加分位数线的方法:

quantile <- quantile(Returns, prob = 0.05)

ggplot(MyData, aes(x=Returns)) + geom_histogram(binwidth=1, colour="black", fill="white") + 
  geom_vline(aes(xintercept=quantile), color="red", linetype="dashed", size=1)

【问题讨论】:

    标签: r histogram density-plot


    【解决方案1】:

    试试这个:

    分位数线

    geom_vline(xintercept=quantile, color="red", linetype="dashed", size=1)

    密度

    geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white") + geom_density(fill=NA, colour="royalblue")

    希望对你有帮助

    【讨论】:

    • 这给了我密度图,但我的 y 轴值现在被密度值覆盖。如何确保 y 轴继续对应于频率(当我仅绘制直方图时就是这种情况)
    • 只需从 ..density.. 切换到 ..count..:geom_histogram(binwidth=1, colour="black", fill="white") + geom_density(aes(y = ..count..), fill=NA, colour="royalblue") obs:这将起作用,因为您的 binwidth 设置为 1。如果设置为 x,则必须将y = ..count..*x 传递给geom_density()
    猜你喜欢
    • 2012-12-07
    • 2022-01-17
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多