简体   繁体   English

如何在R中绘制概率密度函数?

[英]How to plot probability density function in R?

I'm attempting to plot a probability density function with an x restriction from -1 to 0, and 0 to 1 so I'm making two plots: 我试图绘制一个x限制从-1到0,从0到1的概率密度函数,所以我要绘制两个图:

x1 = seq(-1, 0, 0.01)
x2 = seq(0, 1, 0.01)

eq1 = function(x) {(1+x)^2}
eq2 = function(x) {(1+x)^3}

plot(x1, eq1, col="red")
par(new = TRUE)
plot(x2, eq2, type = "l", col = "green")

However, I get the following error: 但是,出现以下错误:

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ. xy.coords(x,y,xlabel,ylabel,log)中的错误:'x'和'y'的长度不同。

I'm not sure what's up. 我不确定发生了什么。

As was pointed out in the comments, the second argument to plot() (ie y ) must be a vector: 正如评论中指出的那样, plot()的第二个参数(即y )必须是向量:

x1 = seq(-1, 0, 0.01)
x2 = seq(0, 1, 0.01)

eq1 = function(x) {(1+x)^2}
eq2 = function(x) {(1+x)^3}

plot(x1, eq1(x1), col="red")
par(new = TRUE)
plot(x2, eq2(x2), type = "l", col = "green")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM