문제

What I want to do sounds simple. I want to plot a normal IQ curve with R with a mean of 100 and a standard deviation of 15. Then, I'd like to be able to overlay a scatter plot of data on top of it.

Anybody know how to do this?

도움이 되었습니까?

해결책

I'm guessing what you want to do is this: you want to plot the model normal density with mean 100 and sd = 15, and you want to overlay on top of that the empirical density of some set of observations that purportedly follow the model normal density, so that you can visualize how well the model density fits the empirical density. The code below should do this (here, x would be the vector of actual observations but for illustration purposes I'm generating it with a mixed normal distribution N(100,15) + 15*N(0,1), i.e. the purported N(100,15) distribution plus noise).

require(ggplot2)
x <- round( rnorm( 1000, 100, 15 )) + rnorm(1000)*15
dens.x <- density(x)
empir.df <- data.frame( type = 'empir', x = dens.x$x, density = dens.x$y )
norm.df <-  data.frame( type = 'normal', x = 50:150, density = dnorm(50:150,100,15))
df <- rbind(empir.df, norm.df)
m <- ggplot(data = df, aes(x,density))
m + geom_line( aes(linetype = type, colour = type))

alt text

다른 팁

Well, it's more like a histogram, since I think you are expecting these to be more like an integer rounded process:

x<-round(rnorm(1000, 100, 15))
y<-table(x)
plot(y)
par(new=TRUE)
plot(density(x), yaxt="n", ylab="", xlab="", xaxt="n")

If you want the theoretic value of dnorm superimposed, then use one of these:

lines(sort(x), dnorm(sort(x), 100, 15), col="red")

alt text -or

points(x, dnorm(x, 100, 15))

You can generate IQ scores PDF with:

curve(dnorm(x, 100, 15), 50, 150)

But why would you like to overlay scatter over density curve? IMHO, that's very unusual...

In addition to the other good answers, you might be interested in plotting a number of panels, each with its own graph. Something like this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top