格子图仅具有正斜率的线

lattice plot only lines with positive slope

是否有一种简单有效的方法来定义像 panel.xyplot(或者 panel.lines)这样的函数,如果 x1 < = x2 且 y1 <= y2? (理想情况下,label.xyplot(...)保留所有其他属性)

一个月前我问过同样的问题,解决方案很棒:

现在最好有一个 real panel.xyplot 之类的功能,这样我就可以使用自己的组了。它应该像下面这样工作和绘制,除了交叉线。 欢迎提出建议。

我不确定我是否理解您的需求,但如果我理解了,那么我认为这应该适用于任何给定的群体:

library(dplyr)

set.seed(1)

dat <- data.frame(x=1:10,y=sample(1:10))
dat <- mutate(dat, x0 = x, y0 = y, x1 = lead(x), y1 = lead(y), slope = (x1 - x0)/(y1 - y0))

with(dat, plot(x, y))
with(dat[1:nrow(dat) - 1,], segments(x0 = x0, y0 = y0, x1 = x1, y1 = y1,
    col = ifelse(slope >= 0, "black", "white"))) # This bit gets makes line-drawing conditional

这是我从中得到的:

这是一个不依赖于 lattice 的分组数据版本:

dat2 <- data.frame(x = rep(seq(10), 10),
                   y = sample(1:10, size = 100, replace = TRUE),
                   indx = rep(seq(10), each = 10))

dat2g <- dat2 %>%
    group_by(indx) %>%
    mutate(., x0 = x, y0 = y, x1 = lead(x), y1 = lead(y), slope = (x1 - x0)/(y1 - y0))

plotit <- function(group) {
    require(dplyr)
    datsub <- filter(dat2g, indx == group)
    with(datsub, plot(x, y, main = group))
    with(datsub[1:nrow(datsub) - 1,], segments(x0 = x0, y0 = y0, x1 = x1, y1 = y1, col = ifelse(slope >= 0, "black", "white")))
}

par(mfrow=c( floor(sqrt(max(dat2g$indx))), ceiling(sqrt(max(dat2g$indx)))))
par(mai=c(0.5,0.5,0.5,0.25))
for (i in 1:length(unique(dat2g$indx))) { plotit(i) }

这是该过程的绘图输出。它可以使用微调,但我认为这就是您所追求的?