r - 带有格子的曲线下的颜色区域

标签 r plot 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)
   })

enter image description here

我找到了一个非常简单的解决方案 R graphic base system ,但我没有找到 Lattice 的等效解决方案。

最佳答案

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')
       })

正确获取多边形有点困惑。

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

enter image description here

关于r - 带有格子的曲线下的颜色区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226956/

相关文章:

r - 有没有办法从 j 部分中分配 R data.table 列的类

r - ggplot 保留组合图的重复颜色

matlab - 有没有办法从现有轴上删除单个图?

r - R 中的 3d 图 - 补丁

R - 如果某个值出现在第 2 列中,则从第 1 列中提取多行

r - 将抖动数据点添加到带有误差线的晶格 xYplot 中

r - ggplot直方图的难点

python - 使用 Matplotlib 注释正态分布图中的四分位数

r - 如何在维基百科页面上绘制扇形图

r - R格子xyplot中的精确轴刻度和标签