本文介绍了ECDF在使用ggplot2的同一图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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(a),ecdf(b),ecdf(c))关联的ecdf,但我没有这样做。我试过:
$ b $ pre $ p <-ggplot(熔体表达式,aes(熔体表达式$ val,ecdf,color =熔体表达式$ var))
pg< -p + geom_step()

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



有没有人知道如何做到这一点?该图看起来应该与plot(ecdf(x))返回的类似,而不是像步骤一样。



谢谢!

解决方案

我的第一个想法是尝试使用 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,color = variable))


geom_smooth 和函数 ns() spline 包:

 库(样条曲线)#function ns() 
ggplot(mydf_m,aes(x = value,y = ecd,group = variable,color = variable))+
geom_smooth(se = FALSE,formula = y〜ns(x,3) =lm)

stat_ecdf 。使用它,我们只需要这样做:

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


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!

解决方案

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

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

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

这篇关于ECDF在使用ggplot2的同一图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:52