How To Avoid Density Curve Getting Cut Off In Plot

别说谁变了你拦得住时间么 提交于 2019-11-27 07:56:04

问题


I am working on an assignment using R and the fitted density curve that is overlaid on the histogram is cut off at it's peak.

Example:

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

I have done a search on the internet for this but didn't find anything addressing this problem. I have tried playing with the margins, but that doesn't work. Am I missing something in my code?

Thank you for your help!


回答1:


Here's the simple literal answer to the question. Make an object to hold the result of your density call and use that to set the ylim of the 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)

(should probably go to SO)



来源:https://stackoverflow.com/questions/32548705/how-to-avoid-density-curve-getting-cut-off-in-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!