简体   繁体   English

R:如何将线与多边形对齐

[英]R: How to align lines with polygon

I'm unable to get the polygon to align with the curve.我无法让多边形与曲线对齐。 I'll put the code here:我将代码放在这里:

critvalmax <- qt(0.975,df=4)
critvalmin <- qt(0.025,df=4)
xvals <- seq(-5, 5, length=100) 
fx.samp.t <- dt(xvals, df=4)

plot(xvals, dnorm(xvals), col="white") 
lines(xvals, fx.samp.t, lty=1, lwd=2)
abline(v= critvalmin, lty=2)
abline(v= critvalmax, lty=2)
abline(h=0, lty=3)
polygon(cbind(c(critvalmin, xvals[xvals>=critvalmin & xvals<=critvalmax], critvalmax, critvalmax), c(0, dt(critvalmin, df=4), fx.samp.t[xvals>=critvalmin & xvals<=critvalmax], 0)), density=10, lty=3)

有问题的情节 The result is that the polygon is drawn a bit to the right and I can't find a solution by myself.结果是多边形被画到了右边,我自己找不到解决方案。 Also, the left bottom corner doesn't seem to fill properly.此外,左下角似乎没有正确填充。

I removed some variables in your polygon like the second critvalmax and dt(critvalmin, df=4) and cbind .我删除了polygon中的一些变量,例如第二个critvalmaxdt(critvalmin, df=4)cbind The dt(critvalmin, df=4) seems to have slightly moved your polygon by 0.02558082 to right. dt(critvalmin, df=4)似乎将您的多边形向右移动了0.02558082 You can use the following code:您可以使用以下代码:

critvalmax <- qt(0.975,df=4)
critvalmin <- qt(0.025,df=4)
xvals <- seq(-5, 5, length=100) 
fx.samp.t <- dt(xvals, df=4)

plot(xvals, dnorm(xvals), col="white") 
lines(xvals, fx.samp.t, lty=1, lwd=2)
abline(v= critvalmin, lty=2)
abline(v= critvalmax, lty=2)
abline(h=0, lty=3)
polygon(c(critvalmin, xvals[xvals>=critvalmin & xvals<=critvalmax], critvalmax), c(0, fx.samp.t[xvals>=critvalmin & xvals<=critvalmax], 0), density=10, lty=3)

Created on 2022-08-31 with reprex v2.0.2使用reprex v2.0.2创建于 2022-08-31

polygon only needs your x and y: polygon只需要你的 x 和 y:

vectors containing the coordinates of the vertices of the polygon.包含多边形顶点坐标的向量。

And the density :density

the density of shading lines, in lines per inch.阴影线的密度,以每英寸线数为单位。 The default value of NULL means that no shading lines are drawn. NULL 默认值表示不绘制阴影线。 A zero value of density means no shading nor filling whereas negative values and NA suppress shading (and so allow color filling).密度为零意味着没有阴影或填充,而负值和 NA 抑制阴影(因此允许颜色填充)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM