直方图,密度图,分位数线

时间:2014-10-12 15:55:20

标签: r histogram density-plot

我有一个返回系列,我已经绘制了直方图但我需要添加密度曲线和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)

1 个答案:

答案 0 :(得分:0)

试试这个:

分位数线

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

希望有所帮助