带格子的曲线下的颜色区域

Wor*_*ice 2 plot r lattice

我想对正态曲线下两个区间之间的区域进行着色/着色,例如示例中生成的曲线。间隔可以是 125:140 或超过 140 的段。

library(lattice)

e4a <- seq(60, 170, length = 10000)
e4b <- dnorm(e4a, 110, 15)
xyplot(e4b ~ e4a,
   type = "l",
   scales = list(x = list(at = seq(60, 170, 5)), rot = 45),
   panel = function(x, ...){
           panel.xyplot(x, ...)
           panel.abline( v = c(110, 125, 95, 140, 80, 95), lty = 2)
   })
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我为R 图形基础系统找到了一个非常简单的解决方案,但我没有找到 Lattice 的等效解决方案。

Mat*_*erg 5

panel.polygon 存在:

xyplot(e4b ~ e4a,
       type = "l",
       scales = list(x = list(at = seq(60, 170, 5)), rot = 45),
       panel = function(x,y, ...){
           panel.xyplot(x,y, ...)
           panel.abline( v = c(110, 125, 95, 140, 80, 95), lty = 2)

           xx <- c(125, x[x>=125 & x<=140], 140) 
           yy <- c(0,   y[x>=125 & x<=140], 0) 
           panel.polygon(xx,yy, ..., col='red')
       })
Run Code Online (Sandbox Code Playgroud)

使多边形正确有点混乱。

请注意,它e4a已在您的数据中排序。如果不是这样,您可能需要在呈现给panel.polygon.

在此处输入图片说明