使用 ggplot2 在同一图上使用 ECDF

发布于 2024-11-29 01:57:14 字数 505 浏览 0 评论 0原文

我有一个数据框,在应用融化函数后,它看起来类似于:

 var       val
1 a 0.6133426
2 a 0.9736237
3 b 0.6201497
4 b 0.3482745
5 c 0.3693730
6 c 0.3564962

......

初始数据框有 3 列,列名称为 a、b、c以及它们的相关值。 我需要在同一张图上绘制,使用 ggplot 为每一列关联 ecdf (ecdf(a)、ecdf(b)、ecdf(c)),但我未能做到这一点。我尝试过:

p<-ggplot(melt_exp,aes(melt_exp$val,ecdf,colour=melt_exp$var))
pg<-p+geom_step()

但我收到错误:参数意味着不同的行数:34415, 0。

有谁知道如何做到这一点?该图应类似于plot(ecdf(x)) 返回的图,而不是阶梯状的图。

谢谢你!

I have a data frame, which after applying the melt function looks similar to:

 var       val
1 a 0.6133426
2 a 0.9736237
3 b 0.6201497
4 b 0.3482745
5 c 0.3693730
6 c 0.3564962

..................

The initial dataframe had 3 columns with the column names, a,b,c and their associated values.
I need to plot on the same graph, using ggplot the associated ecdf for each of these columns (ecdf(a),ecdf(b),ecdf(c)) but I am failing in doing this. I tried:

p<-ggplot(melt_exp,aes(melt_exp$val,ecdf,colour=melt_exp$var))
pg<-p+geom_step()

But I am getting an error :arguments imply differing number of rows: 34415, 0.

Does anyone have an idea on how this can be done? The graph should look similar to the one returned by plot(ecdf(x)), not a step-like one.

Thank you!

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

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

发布评论

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

评论(3

浮光之海 2024-12-06 01:57:14

我的第一个想法是尝试使用 stat_function ,但由于 ecdf 返回一个函数,我无法快速使其工作。相反,这里有一个解决方案,要求您首先将计算值附加到数据框(使用 Ramnath 的示例数据):

library(plyr) # function ddply()
mydf_m <- ddply(mydf_m, .(variable), transform, ecd = ecdf(value)(value))

ggplot(mydf_m,aes(x = value, y = ecd)) + 
    geom_line(aes(group = variable, colour = variable))

如果您想要平滑地估计 ECDF,您还可以将 geom_smoothspline 包中的函数 ns() 一起使用

library(splines) # function ns()
ggplot(mydf_m, aes(x = value, y = ecd, group = variable, colour = variable)) + 
    geom_smooth(se = FALSE, formula = y ~ ns(x, 3), method = "lm")

<图片src="https://i.sstatic.net/WWbuv.png" alt="在此处输入图像描述">

正如上面评论中所述,从版本 0.9.2.1 开始,ggplot2 有一个用于此目的的特定统计数据: < a href="http://docs.ggplot2.org/0.9.2.1/stat_ecdf.html" rel="nofollow noreferrer">stat_ecdf。使用它,我们只需做这样的事情:

ggplot(mydf_m,aes(x = value)) + stat_ecdf(aes(colour = variable))

My first thought was to try to use stat_function, but since ecdf returns a function, I couldn't get that working quickly. Instead, here's a solution the requires that you attach the computed values to the data frame first (using Ramnath's example data):

library(plyr) # function ddply()
mydf_m <- ddply(mydf_m, .(variable), transform, ecd = ecdf(value)(value))

ggplot(mydf_m,aes(x = value, y = ecd)) + 
    geom_line(aes(group = variable, colour = variable))

enter image description here

If you want a smooth estimate of the ECDF you could also use geom_smooth together with the function ns() from the spline package:

library(splines) # function ns()
ggplot(mydf_m, aes(x = value, y = ecd, group = variable, colour = variable)) + 
    geom_smooth(se = FALSE, formula = y ~ ns(x, 3), method = "lm")

enter image description here

As noted in a comment above, as of version 0.9.2.1, ggplot2 has a specific stat for this purpose: stat_ecdf. Using that, we'd just do something like this:

ggplot(mydf_m,aes(x = value)) + stat_ecdf(aes(colour = variable))
少钕鈤記 2024-12-06 01:57:14

基于 Ramnath,采用上述方法,您可以通过执行以下操作从 ggplot2 获取 ecdf:

require(ggplot2)
mydf = data.frame(
   a = rnorm(100, 0, 1),
   b = rnorm(100, 2, 1),
   c = rnorm(100, -2, 0.5)
)

mydf_m = melt(mydf)

p0 = ggplot(mydf_m, aes(x = value)) + 
   stat_ecdf(aes(group = variable, colour = variable)) 
print(p0)

Based on Ramnath, approach above, you get the ecdf from ggplot2 by doing the following:

require(ggplot2)
mydf = data.frame(
   a = rnorm(100, 0, 1),
   b = rnorm(100, 2, 1),
   c = rnorm(100, -2, 0.5)
)

mydf_m = melt(mydf)

p0 = ggplot(mydf_m, aes(x = value)) + 
   stat_ecdf(aes(group = variable, colour = variable)) 
print(p0)
︶葆Ⅱㄣ 2024-12-06 01:57:14

这是一种方法

require(ggplot2)
mydf = data.frame(
  a = rnorm(100, 0, 1),
  b = rnorm(100, 2, 1),
  c = rnorm(100, -2, 0.5)
)

mydf_m = melt(mydf)

p0 = ggplot(mydf_m, aes(x = value)) + 
  geom_density(aes(group = variable, colour = variable)) +
  opts(legend.position = c(0.85, 0.85))

Here is one approach

require(ggplot2)
mydf = data.frame(
  a = rnorm(100, 0, 1),
  b = rnorm(100, 2, 1),
  c = rnorm(100, -2, 0.5)
)

mydf_m = melt(mydf)

p0 = ggplot(mydf_m, aes(x = value)) + 
  geom_density(aes(group = variable, colour = variable)) +
  opts(legend.position = c(0.85, 0.85))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文