[英]Smooth curve through points and include the origin in R
I am a beginner in R and started with graphics recently. 我是R的初学者,最近刚开始使用图形。
I have managed to program a working empirical cumulative distribution function (user-generated, not using the standard ecdf() function) and to generate a plot. 我设法编写了一个有效的经验累积分布函数(用户生成,未使用标准ecdf()函数)并生成了一个图。 However, the plot is not as it should be, there are two issues with it and I am not sure on how to solve them (I have done my 'research' but have not found a solution).
但是,该图不是应该的,它有两个问题,我不确定如何解决(我已经完成了“研究”,但未找到解决方案)。
This is my code: 这是我的代码:
set.seed(1)
n = 50
x = rpois(n, 2.2)
cdf = function(x,n)
{
v=c()
for(z in 1:max(x))
{
a = length(x[x<=z])/n
v = c(v, a)
}
plot(v,type="l", main="empirical cumulative distribution function", xlab="x", ylab="cumulative probability", xlim=c(0,6), ylim=c(0,1.0))
}
cdf(x, n)
There are two issues with this plot: 该图有两个问题:
The lines are straight but it should be a smooth curve through all points. 线是直的,但所有点都应该是平滑的曲线。
The origin is not included (now the curve starts at x = 1). 不包括原点(现在曲线从x = 1开始)。
How can these issues be resolved in an elegant way? 如何以一种优雅的方式解决这些问题?
尝试使用以下样条插值器:
plot(spline(c(0, v)), type = "l")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.