【发布时间】:2015-07-26 09:04:55
【问题描述】:
如果 x1 label.xyplot(...) 保留)
一个月前我问了同样的问题,解决方案很棒: lattice, connect points only if the connection has a positive slope
现在有一个类似 real panel.xyplot 的函数就可以了,这样我就可以使用自己的组了。它应该像下面那样工作和绘制,除了交叉线。 欢迎提出建议。
【问题讨论】:
如果 x1 label.xyplot(...) 保留)
一个月前我问了同样的问题,解决方案很棒: lattice, connect points only if the connection has a positive slope
现在有一个类似 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) }
这是该过程的绘图输出。它可以使用微调,但我认为这是你所追求的?
【讨论】: