Phaml Phaml - 2 months ago 11
R Question

plot theoretic distribution against the real data histogram on one figure

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

My output

Desired output

Desire output


Assuming that ystar is a vector, you should change this:

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


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.