Here is a simple literal answer to the question. Make an object to hold the result of the density call and use it to set the ylim histogram.
x <- rexp(1000, 0.2)
tmp <- density(x)
hist(x, prob = TRUE, ylim = c(0, max(tmp$y)))
lines(tmp, col = "blue", lty = 3, lwd = 2)
(probably need to go to SO)
source
share