2017-07-17 98 views
1

我试图只生成一个ggplot C.D.F.绘制我的一些数据。我也希望能够绘制任意数量的百分点作为顶部的点。我有一个解决方案,用于向我的曲线添加单个点,但失败了多个值。将多个点添加到ggplot ecdf图

这适用于绘制一个百分位值

TestDf <- as.data.frame(rnorm(1000)) 
names(TestDf) <- c("Values") 
percentiles <- c(0.5) 

ggplot(data = TestDf, aes(x = Values)) + 
    stat_ecdf() + 
    geom_point(aes(x = quantile(TestDf$Values, percentiles), 
       y = percentiles)) 

Here is the output

但是失败

TestDf <- as.data.frame(rnorm(1000)) 
names(TestDf) <- c("Values") 
percentiles <- c(0.25,0.5,0.75) 

ggplot(data = TestDf, aes(x = Values)) + 
    stat_ecdf() + 
    geom_point(aes(x = quantile(TestDf$Values, percentiles), 
       y = percentiles)) 

有了错误

Error: Aesthetics must be either length 1 or the same as the data (1000): x, y

如何Ç我为stat_ecdf()图添加任意数量的点?

+1

'ggplot(数据= TestDf,AES(X =值))+ stat_ecdf()+ geom_point(数据= data.frame(X =位数(TestDf $值,百分位数) , y =百分位数),aes(x = x,y = y))' – Masoud

+0

您好@Masoud,这正是我所期待的,非常感谢! –

回答

1

您需要定义一个新的数据集,而不是美学。 aes引用您用于制作CDF的原始数据帧(在原始ggplot参数中)。

ggplot(data = TestDf, aes(x = Values)) + 
    stat_ecdf() + 
    geom_point(data = data.frame(x=quantile(TestDf$Values, percentiles), 
       y=percentiles), aes(x=x, y=y)) 

enter image description here