R 密度图:如何添加从 x 轴到密度曲线顶部的实线
我有一个密度图,使用以下方法绘制:
plot(density(x))
我感兴趣的是创建一条从 x 轴到曲线上相应点的线,例如 x = 5。
像这样:
I have a density plot graphed using:
plot(density(x))
What I am interested in doing is creating a line for something like x = 5 from the x-axis to the corresponding spot on the curve.
Like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您可以首先将密度值存储在对象中,然后从该对象中检索
x
和y
元素。在以下示例中,我使用findInterval
检索给定 x 值的 y 值:You can do this by first storing the density values in an object, and then retrieving the
x
andy
elements from this object. In the following example I usefindInterval
to retrieve the y-value for the given x-value:如果它确实是 z 分数,那么只需实际绘制密度函数
dnorm()
即可。您似乎还希望实际的 x 轴位于 0 处。要绘制线条,您可以再次使用 dnorm 函数。
您甚至还可以很好地遮蔽它...:)
您还可以使用
segments
和text
命令将标签放在您的图形上,显示阴影和未阴影的内容地区的意思。If it's really z-scores then just actually plot the density function
dnorm()
. It also looks like you'd like to have your actual x-axis at 0.To draw in your line you can use the
dnorm
function again.You could also even shade it out nicely... :)
You can also use
segments
and thetext
command to put the labels right on your graph of what the shaded and unshaded areas mean.