2010-03-19 38 views
1

我有一个看起来像这样的数据:HOWTO情节两个累积频率图一起

#val Freq1 Freq2 
0.000 178 202 
0.001 4611 5300 
0.002 99 112 
0.003 26 30 
0.004 17 20 
0.005 15 20 
0.006 11 14 
0.007 11 13 
0.008 13 13 
...many more lines.. 

全部数据可以在这里找到: http://dpaste.com/173536/plain/

我打算做的是有一个累积的图 以“val”作为x轴,其中“Freq1”为&“Freq2”为 y轴,在1图中一起绘制。

我有这段代码。但是,它会创建两个地块,而不是1

dat <- read.table("stat.txt",header=F); 
val<-dat$V1 
freq1<-dat$V2 
freq2<-dat$V3 

valf1<-rep(val,freq1) 
valf2<-rep(val,freq2) 

valfreq1table<- table(valf1) 
valfreq2table<- table(valf2) 
cumfreq1=c(0,cumsum(valfreq1table)) 
cumfreq2=c(0,cumsum(valfreq2table)) 

plot(cumfreq1, ylab="CumFreq",xlab="Loglik Ratio") 
lines(cumfreq1) 
plot(cumfreq2, ylab="CumFreq",xlab="Loglik Ratio") 
lines(cumfreq2) 

什么是处理这个正确的方式?

+0

关于一个设备上的两个情节:http://stackoverflow.com/questions/1801064/how-to-separate-two-plots-in-r。 – Marek 2010-03-19 06:23:06

+0

@Marek:我的意思是不同的事情。我的意思是两个曲线在一个阴谋。 – neversaint 2010-03-19 07:57:59

回答

6
data <- read.table("http://dpaste.com/173536/plain/", header = FALSE) 

sample1 <- unlist(apply(as.matrix(data),1,function(x) rep(x[1],x[2]))) 
sample2 <- unlist(apply(as.matrix(data),1,function(x) rep(x[1],x[3]))) 

plot(ecdf(sample1), verticals=TRUE, do.p=FALSE, 
main="ECDF plot for both samples", xlab="Scores", 
ylab="Cumulative Percent",lty="dashed") 

lines(ecdf(sample2), verticals=TRUE, do.p=FALSE, 
col.h="red", col.v="red",lty="dotted") 

legend(100,.8,c("Sample 1","Sample 2"), 
col=c("black","red"),lty=c("dashed","dotted")) 
+0

谢谢。令人惊讶的是,lines()的作品和points()没有。 – JohnRos 2012-02-02 13:54:20

3

尝试ecdf()功能在基地R ---使用plot.stepfun()如果内存服务---或Ecdf()功能在Hmisc由弗兰克哈雷尔。下面是从help(Ecdf)使用分组变量显示两个ecdfs积于一身的例子:

# Example showing how to draw multiple ECDFs from paired data 
pre.test <- rnorm(100,50,10) 
post.test <- rnorm(100,55,10) 
x <- c(pre.test, post.test) 
g <- c(rep('Pre',length(pre.test)),rep('Post',length(post.test))) 
Ecdf(x, group=g, xlab='Test Results', label.curves=list(keys=1:2)) 
+0

我测试了你的代码,但它给了我以下消息:“未使用的参数(组= g,xlab =”测试结果“,label.curves = list(keys = 1:2)) ” – neversaint 2010-03-19 03:41:41

+2

代码为我完美工作。确保你使用的是Ecdf而不是ecdf。如果你使用后者的功能,你会得到错误。 – 2010-03-19 04:15:40

+0

Ecdf中的y轴被归一化(即0到1)。有没有办法让它使用值“x”的“反向”累积频率? (即相当于什么=“1-f”的东西) – neversaint 2010-03-19 06:24:28

1

只是为了记录在案,这里是你如何获得同积多行“手工”:

plot(cumfreq1, ylab="CumFreq",xlab="Loglik Ratio", type="l") 
      # or type="b" for lines and points 
lines(cumfreq2, col="red")