compsciman
compsciman

Reputation: 379

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:

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.

I'm not sure what's up.

Upvotes: 0

Views: 2346

Answers (1)

Felipe Gerard
Felipe Gerard

Reputation: 1622

As was pointed out in the comments, the second argument to plot() (i.e. y) must be a vector:

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

Upvotes: 2

Related Questions