如何避免密度曲线在图中被截断 [英] How To Avoid Density Curve Getting Cut Off In Plot

查看:82
本文介绍了如何避免密度曲线在图中被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R进行分配,并且直方图上覆盖的拟合密度曲线在峰值处被切除。

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.

示例:

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?

谢谢您的帮助!

推荐答案

这是对该问题的简单字面答案。制作一个对象来保存密度调用的结果,并使用它来设置直方图的ylim。

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)

(可能应该转到这样)

这篇关于如何避免密度曲线在图中被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆