R 曲线下的阴影区域

R 曲线下的阴影区域,r,R,我正试图在R的曲线下给一个区域加上阴影。我不能完全正确,我也不知道为什么。曲线由以下公式定义: # Define the Mean and Stdev mean=1152 sd=84 # Create x and y to be plotted # x is a sequence of numbers shifted to the mean with the width of sd. # The sequence x includes enough values to show +/-3.

我正试图在R的曲线下给一个区域加上阴影。我不能完全正确,我也不知道为什么。曲线由以下公式定义:

# Define the Mean and Stdev
mean=1152
sd=84

# Create x and y to be plotted
# x is a sequence of numbers shifted to the mean with the width of sd.  
# The sequence x includes enough values to show +/-3.5 standard deviations in the data set.
# y is a normal distribution for x
x <- seq(-3.5,3.5,length=100)*sd + mean
y <- dnorm(x,mean,sd)
我用来尝试在x>=1250的曲线下着色的代码

polygon(c( x[x>=1250], max(x) ),  c(y[x==max(x)], y[x>=1250] ), col="red")
但这是我得到的结果
如何正确着色曲线下x>=1250

的部分您需要跟随多边形曲线的x,y点,然后沿x轴返回(从最大x值到x=1250,y=0处的点)以完成形状。最终垂直边将自动绘制,因为多边形通过返回其起点来关闭形状

polygon(c(x[x>=1250], max(x), 1250), c(y[x>=1250], 0, 0), col="red")

如果不希望将着色一直放置到x轴,而是希望将其放置在曲线的级别,则可以使用以下选项。尽管在给出的示例中,曲线几乎下降到x轴,因此很难从视觉上看到差异

polygon(c(x[x>=1250], 1250), c(y[x>=1250], y[x==max(x)]), col="red")
polygon(c(x[x>=1250], 1250), c(y[x>=1250], y[x==max(x)]), col="red")