如何使用 ggplot 绘制多个 ecdf?
我有一些数据格式如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不再使用 qplot(),会更容易:
您还可以轻松添加
facet_wrap
来表示多个组,并添加xlab
/ylab
来表示标签。Easier if you move away from qplot():
You can also easily add in
facet_wrap
for more than one group, andxlab
/ylab
for labels.自 2012 年底以来,ggplot2 包含了用于打印 ecdfs 的专用函数:ggplot2 文档。
那里的示例甚至比 Ari 的好解决方案还要短:
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: