3

Possible Duplicate:
Fitting a density curve to a histogram in R

x is a NAs free numeric vector.

I run:

> hist(x,density(x), prob=TRUE)

Error Message I get:
Error in rank(x, ties.method = "min", na.last = "keep") : 
  unimplemented type 'list' in 'greater'

It was suggested that I set prob =TRUE when calling hist. If you can explain that as well, it will be great. Thank you.

Community
  • 1
  • 1
Selvam
  • 1,093
  • 7
  • 22
  • 30
  • 2
    `prob=TRUE` sets the y-axis to be in terms of probabilities rather than frequencies (counts), so that density estimates will be on the same vertical scale. As the help explains (under the entry for its counterpart, `freq`, `prob=TRUE` scales "`so that the histogram has a total area of one`". To add to Hong Ooi's good answer: see the last example under ?hist for a way to add a specified density rather than a kernel estimate. – Glen_b Oct 18 '12 at 01:56

1 Answers1

7

You need to call hist and density separately. Something like this:

hist(x, prob=TRUE)
lines(density(x))
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • Thank you for the solution. If you can further explain this, what is the difference between using the hist() argument density, similar to my failed attempt and the lines() function that you suggested. – Selvam Oct 18 '12 at 02:36
  • @Selvam `?hist` explains what its density argument is for - specifying the density of lines for shading of the bars. – Glen_b Oct 18 '12 at 04:19