如何将两个累积频率图绘制在一起

发布于 2024-08-26 03:40:15 字数 897 浏览 4 评论 0原文

我的数据如下所示:

#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”和“Freq1”作为 x 轴。 “频率2”为 y 轴,一起绘制在一张图中。

我有这个代码。但它创建了两个图而不是 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)

处理这个问题的正确方法是什么?

I have data that looks like this:

#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..

Full data can be found here:
http://dpaste.com/173536/plain/

What I intend to do is to have a cumulative graph
with "val" as x-axis with "Freq1" & "Freq2" as
y-axis, plot together in 1 graph.

I have this code. But it creates two plots instead of 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)

What's the right way to approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-09-02 03:40:15
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"))
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"))
枯叶蝶 2024-09-02 03:40:15

尝试基本 R 中的 ecdf() 函数 --- 如果内存充足的话,它会使用 plot.stepfun() --- 或 Ecdf() > Frank Harrell 的 Hmisc 中的函数。以下是来自 help(Ecdf) 的示例,它使用分组变量在一个图中显示两个 ecdf:

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

Try the ecdf() function in base R --- which uses plot.stepfun() if memory serves --- or the Ecdf() function in Hmisc by Frank Harrell. Here is an example from help(Ecdf) that uses a grouping variable to show two ecdfs in one plot:

 # 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))
风渺 2024-09-02 03:40:15

仅供记录,以下是如何“手动”在同一图中获得多条线:

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

Just for the record, here is how you get multiple lines in the same plot "by hand":

plot(cumfreq1, ylab="CumFreq",xlab="Loglik Ratio", type="l") 
          # or type="b" for lines and points
lines(cumfreq2, col="red") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文