1

I'd like to add a curve with it's x and y coordinates already defined to a histogram. Consider the histogram below:

set.seed(38593)
expRandom <- rexp(10000)
x <- seq(from = 0.05, to = 10, by = 0.001)
y <- exp(-x)
### Now I'd like to first draw my histogram and then 
### add my plot(x,y) to my existing histogram:
hist(expRandom, freq = FALSE)

### ?? How to add my plot(x,y) to my histogram above?

Thanks,

Sam
  • 4,357
  • 6
  • 36
  • 60
  • That has been asked and answered numerous times here and elsewhere. Did you try a search? – Dirk Eddelbuettel Nov 16 '12 at 04:51
  • Oh oh... I did a quick search and I didn't find. Should I delete the post? – Sam Nov 16 '12 at 04:55
  • That is not the purpose of SO (in my opinion) – mnel Nov 16 '12 at 04:56
  • But I thought that it works like a forum that people can ask their questions...Am I misunderstood? I apologize anyways. – Sam Nov 16 '12 at 04:58
  • 3
    Searched for `[r] curve histogram`, found these on the first page of results: http://stackoverflow.com/questions/1497539/fitting-a-density-curve-to-a-histogram-in-r http://stackoverflow.com/questions/12080139/histogram-and-pdf-in-the-same-graph http://stackoverflow.com/questions/8872525/r-plot-histogram-and-density-function-curve-on-one-chart http://stackoverflow.com/questions/9046664/how-to-use-the-function-curve-in-r-to-graph-a-normal-curve – thelatemail Nov 16 '12 at 06:26

1 Answers1

5

Sett freq = FALSE to plot the densities not frequencies in the histogram and use points (or lines) to add your data.

hist(expRandom, freq = FALSE)
points(x,y)

You could also avoid having to precaclulate the y values by using curve to add a curve

hist(expRandom, freq = FALSE)
curve(dexp, from = 0, to  = 10, add = TRUE)

enter image description here

mnel
  • 113,303
  • 27
  • 265
  • 254