我有一些格式如下的数据:
2 2
2 1
2 1
2 1
2 1
2 1
2 2
2 1
2 1
2 1
2 2
2 2
2 1
2 1
2 2
2 2
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 1
3 1
3 1
3 3
3 2
3 2
4 4
4 2
4 4
4 2
4 4
4 2
4 2
4 4
4 2
4 2
4 1
4 1
4 2
4 3
4 1
4 3
6 1
6 1
6 2
7 1
7 1
7 1
7 1
7 1
8 2
8 2
8 2
8 2
8 2
8 2
12 1
12 1
12 1
12 1
12 1我尝试为第一列中的每个不同值绘制此数据集的ecdf。因此,在本例中,我希望在图上绘制7条ecdf曲线(一条曲线用于第一列中包含2的所有点,一条曲线用于第一列中包含3的所有点,依此类推……)。对于一列,我可以使用以下命令绘制ecdf:
data = read.table("./test", header=F)
data1 = data[data$V1 == 2,]
qplot(unique(data1$V2), ecdf(data1$V2)(unique(data1$V2)), geom='step')但是我不能理解如何绘制多条曲线。有什么建议吗?
发布于 2011-07-27 14:51:49
如果不使用qplot(),就会更容易:
library(plyr)
library(ggplot2)
d.f <- data.frame(
grp = as.factor( rep( c("A","B"), each=40 ) ) ,
val = c( sample(c(2:4,6:8,12),40,replace=TRUE), sample(1:4,40,replace=TRUE) )
)
d.f <- arrange(d.f,grp,val)
d.f.ecdf <- ddply(d.f, .(grp), transform, ecdf=ecdf(val)(val) )
p <- ggplot( d.f.ecdf, aes(val, ecdf, colour = grp) )
p + geom_step()您还可以轻松地为多个组添加facet_wrap,并为标签添加xlab/ylab。

d.f <- data.frame(
grp = as.factor( rep( c("A","B"), each=120 ) ) ,
grp2 = as.factor( rep( c("cat","dog","elephant"), 40 ) ) ,
val = c( sample(c(2:4,6:8,12),120,replace=TRUE), sample(1:4,120,replace=TRUE) )
)
d.f <- arrange(d.f,grp,grp2,val)
d.f.ecdf <- ddply(d.f, .(grp,grp2), transform, ecdf=ecdf(val)(val) )
p <- ggplot( d.f.ecdf, aes(val, ecdf, colour = grp) )
p + geom_step() + facet_wrap( ~grp2 )

发布于 2013-04-09 02:39:01
自2012年底以来,ggplot2包含了一个用于打印ecdfs的专用函数:ggplot2 docs。
这里的示例甚至比Ari的优秀解决方案还要短:
df <- data.frame(x = c(rnorm(100, 0, 3), rnorm(100, 0, 10)),
g = gl(2, 100))
ggplot(df, aes(x, colour = g)) + stat_ecdf()

https://stackoverflow.com/questions/6839956
复制相似问题