3

I'm trying to overlay a normal distribution curve onto a histogram in R. I know it's a question that's been asked before, but I'm having trouble getting the solutions to work for me.

This is my code:

hist(input_data$"X109_scalesraw_23", freq = TRUE, breaks = 30, 
     col = "cadetblue", xlim = c(0,30), ylim = c(0,150), 
     main = "023", xlab = "score")
Roland
  • 127,288
  • 10
  • 191
  • 288
Mart.L.
  • 31
  • 1
  • 1
  • 2
  • How are you having trouble? It would be helpful if you could edit your post with a description of what's going wrong. – fractalwrench May 19 '16 at 10:13
  • does it have to be base r graphics or is ggplot2 ok too? – Manuel R May 19 '16 at 10:17
  • A standard histogram and a probability density curve have very different y-axis scales. The former displays frequency whereas the latter displays density values. You might want to set `freq = FALSE` in your `hist` call, to ensure that it displays density values. – Roland May 19 '16 at 10:17

1 Answers1

5

You can always use curve with add=TRUE (telling R to add the curve to existing plot):

data <- rnorm(100, 0, 1)
hist(data, freq = FALSE)
x<-seq(-4,+4,by=0.02)
curve(dnorm(x), add=TRUE)

which produces

enter image description here

rbm
  • 3,243
  • 2
  • 17
  • 28
  • Hi all, firstly, thanks for the answers so far. – Mart.L. May 23 '16 at 12:17
  • Hi all, firstly, thanks for the answers so far. The trouble is this: I simply can't add a normal curve... The data I'm working with are test results. Each candidate has answered several questions pertaining to scale_23 and thus has a collated raw average score that includes decimal points. There are over 500 candidates in the data so the rawdata results are pretty much continuous. – Mart.L. May 23 '16 at 12:25
  • I am checking through the data to create a normgroup with which I can compare later individual test results. Therefore I at the moment want to check to see if the data I have is normally distributed, which, theoretically, it ought to be. I can look at the histogram and make an educated guess, but for the sake of showing my bosses (and for me, yes) I want to add a normal curve "on top" of the histogram. I am new to R. I have not used ggplots. I am not familiar with density histograms. The scores were averaged for each scale so there are no discrete values. – Mart.L. May 23 '16 at 12:25
  • Hi, not sure what you mean by "I simply can't add a normal curve" - why not? If you're not sure the data is normal, there are tests that can tell you whether the data is (reasonably) normal: Jarque-Bera, Shapiro-Wilk etc. You need to show your data and show some code, then it'll be possible to give some definite answer. – rbm May 23 '16 at 15:25