【问题标题】:lattice, connect points only if the connection has a positive slope格,仅当连接具有正斜率时才连接点
【发布时间】:2015-01-16 12:34:16
【问题描述】:

只有当连接具有正斜率时,是否有一种舒适的方法来连接点? (否则该函数的行为应与 xyplot(...) 完全相同)

library(lattice)

dat <- data.frame(x=1:10,y=sample(1:10))

xyplot(y ~ x, data=dat,
       panel = function(x, y,...) { 
         panel.xyplot(x, y, type="o",...)
       }
)

所以结果应该是这样的情节,但没有交叉线:

谢谢 克里斯托夫

【问题讨论】:

  • 虽然 Roland 的解决方案非常好,但我不得不问这是否是将您的信息传达给读者的最佳方式。你能解释一下为什么你想要显示任何行吗?标准规则是仅为实验数据绘制点。我建议,而不是线,识别y[j]&gt;y[j-1] 的每个实例并以不同的颜色绘制这些点以指示“传入”斜率。

标签: r plot lattice


【解决方案1】:
dat <- dat[order(dat[, "x"]),]
dat$group <- cumsum(c(1, diff(dat$y) < 0))


xyplot(y ~ x, data = dat, groups = group,
       panel = function(x, y,...) { 
         panel.xyplot(x, y, type = "o", col = trellis.par.get("plot.line")$col, ...)
       }
)

【讨论】:

  • 太棒了,非常感谢。如果有额外的分组因素,我会尝试找出是否可以使用类似的解决方案。
猜你喜欢
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多