lattice, connect points only if the connection has a positive slope

落花浮王杯 提交于 2019-12-23 08:05:07

问题


is there a comfortable way to connect points only if the connection has a positive slope? (otherwise the function should behave exactly as 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",...)
       }
)

so the result shoud be a plot like this, but without crossed lines:

Thank you Christof


回答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, ...)
       }
)



来源:https://stackoverflow.com/questions/27984087/lattice-connect-points-only-if-the-connection-has-a-positive-slope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!