2010-03-19 63 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")