2013-04-06 53 views
3

我是ggplot2的新手,我试图弄清楚如何将一条线添加到已创建的已有图中。原始图是来自数据帧x的数据列T1的累积分布,其具有约100,000个元素。我已经成功地绘制了这个使用ggplot2和stat_ecdf()与我在下面发布的代码。现在,我想添加使用一组(X,Y)坐标另一条线,但是当我尝试这个使用geom_line()我得到的错误信息:添加到ggplot不同长度的元素

Error in data.frame(x = c(0, 7.85398574631245e-07, 3.14159923334398e-06, : 
arguments imply differing number of rows: 1001, 100000 

这里是我想要使用的代码:

> set.seed(42) 
> x <- data.frame(T1=rchisq(100000,1)) 
> ps <- seq(0,1,.001) 
> ts <- .5*qchisq(ps,1) #50:50 mixture of chi-square (df=1) and 0 
> p <- ggplot(x,aes(T1)) + stat_ecdf() + geom_line(aes(ts,ps)) 

这就是从上面产生的错误。现在,这里的使用基本图形,我以前用的是代码,但我现在想从搬开:我还没有看到

plot(ecdf(x$T1),xlab="T1",ylab="Cum. Prob.",xlim=c(0,4),ylim=c(0,1),main="Empirical vs. Theoretical Distribution of T1") 
lines(ts,ps) 

我已经看到了一些其他职位对一般加线,但是当两个始发矢量的长度不相同时如何添加一条线。 (注意:我不想仅使用100,000(x,y)坐标。)

作为奖励,有没有简单的方法,类似于使用abline在ggplot2图上添加放置线?

任何意见将不胜感激。

data.frames

回答

0

ggplot交易,你需​​要做tsps一个data.frame然后指定你的电话本额外data.framegeom_line

set.seed(42) 
x <- data.frame(T1=rchisq(100000,1)) 
ps <- seq(0,1,.001) 
ts <- .5*qchisq(ps,1) #50:50 mixture of chi-square (df=1) and 0 
tpdf <- data.frame(ts=ts,ps=ps) 
p <- ggplot(x,aes(T1)) + stat_ecdf() + geom_line(data=tpdf, aes(ts,ps)) 

enter image description here