Phaml - 4 months ago 27

R Question

I want to plot the histogram with real data and compare it with a theoretical normal distribution in one plot. But the scale looks different. **Two plots have different scale**

`# you can generate some ramdom data on ystar which is realy data.`

x<-seq(-4,4,length=200)

y<-dnorm(x,mean=0, sd=1)

plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5),ylim=c(0,0.7))

par(new = TRUE)

hist(ystar,xlim = c(-10,10),freq = FALSE,ylim=c(0,0.7),breaks = 50)

Answer

Assuming that `ystar`

is a vector, you should change this:

```
y<-dnorm(x,mean=0, sd=1)
```

To:

```
y<-dnorm(x,mean=mean(ystar), sd=sd(ystar))
```

This will produce a distribution function that approximately matches the histogram.

You should then be able to use the same x-limits for both the histogram and the theoretical distribution, which will eliminate the strange overlapping axis labels you have in your current version.