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

时间:2019-04-09 11:34:26

标签: r plot

我正在尝试绘制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")

但是,出现以下错误:

  

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

我不确定怎么回事。

1 个答案:

答案 0 :(得分:2)

正如评论中指出的那样,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")
相关问题