gpt4 book ai didi

r - 如何使用 ggplot 绘制多个 ecdfs?

转载 作者:行者123 更新时间:2023-12-03 18:18:17 24 4
gpt4 key购买 nike

我有一些数据格式如下:

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

但我无法理解如何绘制多条曲线。有什么建议?

最佳答案

如果你远离 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用于标签。

multiple ecdfs
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 )

using 2 grouping variables

关于r - 如何使用 ggplot 绘制多个 ecdfs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6839956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com