Search code examples
rprobability

Unexpected ',' when using lines function in R


I am trying to plot multiple distributions on the same plot using the lines function but I am getting an error. The error reads:

Error in parse(text = x, srcfile = src): :8:16: unexpected ','
8: lines <- (xvals,

xvals <- seq(-5, 10,, length.out=501)  

pdf1 <- pnorm(xvals, mean=0, sd=1)
plot(xvals, pdf1, type='l', xlab='x', ylab='f(x)', main='PDF of N(0,1), N(2,2^2) and N(-1,0.5^2)')

pdf2 <- pnorm(xvals, mean=2, sd=sqrt(2))
pdf3 <- pnorm(xvals, mean=-1, sd=sqrt(0.5))


lines <- (xvals, pdf2, col = 'red')
lines <- (xvals, pdf3, col = 'blue')

Solution

  • This should work:

    xvals <- seq(-5, 10, length.out=501)  
    
    pdf1 <- pnorm(xvals, mean=0, sd=1)
    plot(xvals, pdf1, type='l', xlab='x', ylab='f(x)', main='CDF of N(0,1), N(2,2^2) and N(-1,0.5^2)')
    
    pdf2 <- pnorm(xvals, mean=2, sd=sqrt(2))
    pdf3 <- pnorm(xvals, mean=-1, sd=sqrt(0.5))
    
    lines(xvals, pdf2, col = 'red')
    lines(xvals, pdf3, col = 'blue')
    

    Resulting plot:

    enter image description here