Search code examples
rnormal-distributiondata-visualization

Unsmooth lines when using the dnorm and plot functions in R


I have some troubles when I use the dnorm and plot functions in R. When I execute the following code in Rcommander:

> x <- 50:225
> 
> a< - dnorm(x,165,15); b <- dnorm(x,149,18); c <- dnorm(x,134,25)
> 
> plot(x, a, type="l", lwd=3, ylim=c(0,1.2*max(a,b,c)), ylab="Probability
>      Density", xlab="Perikymata") lines(x,b, type="l", lwd=3, col="Red")
>      lines(x,c, type="l", lwd=3, col="Blue")

I get the next image:

enter image description here

For now, perfect. The problem arrives when I change these values, for example, using the following code:

> x <- 0:15
> 
> a <- dnorm(x,5.28,0.91); b <- dnorm(x,8.45,1.36)
> 
> plot(x,a, type="l", lwd=3, ylim=c(0,1.2*max(a,b)), ylab="Probability
>      Density", xlab="Perikymata") lines(x,b, type="l", lwd=3, col="Red")

Using this text I get the next image:

enter image description here

How can I do to smooth these two lines in the last graph, taking as comparison the first image, and doing the two lines to resemble to normal distributions?


Solution

  • R is calculating Y values ("Probability Density") only at certain increments in X ("Perikymata"). In the original plot, your X values ranged from 50 to 225, i.e., 175 units of Perikymata. In your new plot, the X values only range from 0 to 15, i.e., 15 units of Perikymata. You can get a smoother plot by ensuring that there are more Y values calculated. For example, instead of 15, you could get 175. You can do that by using:

    x <- seq(from=0, to=15, length.out=175)