how to plot this in R

Viewed 30

how would I go about plotting this in R?

Here is the code I have already tried but it doesn't work!

x <- seq(-5, 5, length=200)
y <- dnorm(x, mean= -1, sd=1)

t = sqrt(0.5)

z <- seq(-5, 5, length=200)
a <- dnorm(z, mean=1, sd=t)

plot(z, a, type="l", lwd=2, col='red', xlim = c(-5,5), ylim = c(0,0.65),  xlab = "Observed Value", ylab = "Probability Density")
lines(x,y, col='blue', lwd=2)
abline(v = 0, col = "darkgreen")

enter image description here

1 Answers

Modify your call to plot as follows:

plot(
  z,
  a,
  type = "l",
  lwd = 2,
  col = 'red',
  xlim = c(-5, 5),
  ylim = c(0, 0.65),
  xlab = "Observed Value",
  ylab = "Probability Density",
  main = expression(paste(pi[1], "= .3, ", pi[2], "= .7"))
)

lines(x, y, col = 'blue', lwd = 2)
abline(v = 0, col = "darkgreen", lty=2, lwd = 1.5)
Related