您可以使用以下代码:
xyplot(from ~ posi , type="p", col="black", data=temp, pch=16, xlim = c(0,7),
panel = function(...){
panel.dotplot(x = (temp$posi+1), y = temp$to, col="black", cex=1.4)
panel.dotplot(x = temp$posi, y = temp$from, col ="black", cex=1.4)
panel.arrows(x0 = temp$posi, y0 = temp$from, x1 = temp$posi+1, y1 = temp$to, lwd=3, col="blue", )
}
)
产生下图:
请告诉我这是否是您想要的。
UPDATE
我发布了一个关于 @skan 在评论中识别和描述的问题的问题:当“极端”级别(如“D”)不存在于temp$from
那么“D”将不会成为图表的一部分,即使稍后需要“D”temp$to
。可以找到@Konn的答案的问题here.
据我现在的理解,我们需要一个factor
那是ordered
,我们需要添加指定的代码drop.unused.levels = FALSE
在通话中xyplot
。在示例中,我们显示了“from”中包含“extremes”的完整集合,以及作为不存在极端“D”的子集:完整代码为:
l <- c("A", "B", "C", "D")
temp <- data.frame(posi = c(1, 2, 3, 3, 2),
from= factor(c("A", "B", "C", "D", "D"), levels = l, ordered = TRUE),
to = factor(c("C", "D", "D", "C", "A"), levels = l, ordered = TRUE)
)
xyplot(from ~ posi , type="p", col="black", data=temp, pch=16, xlim = c(0,7),
drop.unused.levels = FALSE, ## the added code
panel = function(...){
panel.dotplot(x = temp$posi, y = temp$from, col ="green", cex=1.6)
panel.dotplot(x = (temp$posi+1), y = temp$to, col="black", cex=1.)
panel.arrows(x0 = temp$posi, y0 = temp$from, x1 = temp$posi+1, y1 = temp$to, lwd=2, col="blue" )
})
temp <- temp[1:3, ]
xyplot(from ~ posi , type="p", col="black", data=temp, pch=16, xlim = c(0,7),
drop.unused.levels = FALSE, ## the added code
panel = function(...){
panel.dotplot(x = temp$posi, y = temp$from, col ="green", cex=1.6)
panel.dotplot(x = (temp$posi+1), y = temp$to, col="black", cex=1.)
panel.arrows(x0 = temp$posi, y0 = temp$from, x1 = temp$posi+1, y1 = temp$to, lwd=2, col="blue" )
})
产生以下图片:
我想我们已经解决了这个问题。