venerdì 11 maggio 2007

Density curve over a histogram

This post was updated.

x <- rnorm(1000)
hist(x, freq = FALSE, col = "grey")
curve(dnorm, col = 2, add = TRUE)

This thread (specifically the Ted Harding answer) from the r-help-list augments the usefulness of this simple tip.

This kind of plots cab be easily produced using the lessR package.

For example, from ?color.density:

# generate 100 random normal data values
y <- rnorm(100)
# normal curve and general density curves superimposed over histogram
# all defaults

7 commenti:

  1. Your code doesn't work. Even with hist() on another line. I took out the freq and col options to get a histogram plotted, but the curve function plots a horizontal line at approximately 0. I'm using ver2.9.

  2. It does work. My guess:
    1) you should divide each command using the ; character or type each command in a different row.
    2) the " character of "grey" after copy & paste became “, so you have to substitute it with the " of your keyboard.
    Hope it helps.

  3. Great, Thank you!

  4. Say I have 3 sets of data, how to do 3 histograms grouped by bin and overlaid by 3 density curves on one graph?
    mutlhist in plotrix can do the 3 histograms together , but how to add density? Thanks!

  5. I think that both lattice and ggplot2 packages should be capable to solve your problem. Please take a look at the information you can find at the following link:
    In particular, you can download a pdf comparing the two packages in all kind of common visualizations: it can be an useful starting point.


  6. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. R Programming Training in Bangalore