Avoiding a Density Curve

I am working on an assignment using R, and the established density curve superimposed on the histogram is cropped at its peak.

Example:

x <- rexp(1000, 0.2)
hist(x, prob = TRUE)
lines(density(x), col = "blue", lty = 3, lwd = 2)

enter image description here

I did an online search for this, but did not find anything that could solve this problem. I tried playing with fields, but this does not work. Am I missing something in my code?

Thank you for your help!

+4
source share
1 answer

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)

+4
source

Source: https://habr.com/ru/post/1607125/


All Articles