【问题标题】:Shade area under a curve [duplicate]曲线下的阴影区域[重复]
【发布时间】:2016-04-29 22:47:13
【问题描述】:

我正在尝试对 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)

剧情是

# Plot x vs. y as a line graph
plot(x, y, type="l")

我用来尝试在 x >= 1250 的曲线下着色的代码

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

但这是我得到的结果 如何正确着色曲线下 x >= 1250 的部分

【问题讨论】:

    标签: r


    【解决方案1】:

    您需要跟随多边形的曲线的 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")
    

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多