如何使用 ggplot 绘制多个 ecdf?

发布于 2024-11-26 13:02:33 字数 1141 浏览 2 评论 0原文

我有一些数据格式如下:

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

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

I have some data formatted like the following:

    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

I am trying to plot the ecdf of this dataset for each distinct value in the first column. Therefore in this case, I want to plot 7 ecdf curves on a graph (one for all points that have 2 in their first column, one for all points that have 3 in their first column and so on...). For one column, I am able to plot the ecdf using the following:

data = read.table("./test", header=F)
data1 = data[data$V1 == 2,]
qplot(unique(data1$V2), ecdf(data1$V2)(unique(data1$V2)), geom='step')

But I am not able to understand how to plot multiple curves. Any suggestions?

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

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

发布评论

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

评论(2

冷了相思 2024-12-03 13:02:33

如果您不再使用 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 来表示标签。

多个 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 )

使用 2分组变量

Easier if you move away from 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()

You can also easily add in facet_wrap for more than one group, and xlab/ylab for labels.

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

⊕婉儿 2024-12-03 13:02:33

自 2012 年底以来,ggplot2 包含了用于打印 ecdfs 的专用函数:ggplot2 文档

那里的示例甚至比 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()

ecdf

Since the end of 2012, ggplot2 includes a dedicated function for printing ecdfs: ggplot2 docs.

The example from there is even shorter than the good solution by 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()

ecdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文