使用 ggplot2 在同一图上使用 ECDF
我有一个数据框,在应用融化函数后,它看起来类似于:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的第一个想法是尝试使用 stat_function ,但由于 ecdf 返回一个函数,我无法快速使其工作。相反,这里有一个解决方案,要求您首先将计算值附加到数据框(使用 Ramnath 的示例数据):
如果您想要平滑地估计 ECDF,您还可以将
geom_smooth
与spline
包中的函数ns()
一起使用<图片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。使用它,我们只需做这样的事情:
My first thought was to try to use
stat_function
, but sinceecdf
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):If you want a smooth estimate of the ECDF you could also use
geom_smooth
together with the functionns()
from thespline
package: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:
基于 Ramnath,采用上述方法,您可以通过执行以下操作从 ggplot2 获取 ecdf:
Based on Ramnath, approach above, you get the ecdf from ggplot2 by doing the following:
这是一种方法
Here is one approach