[英]How to plot segments or arrows with Lattice in R? (from a Wide format dataset)
I would like to create a plot with segements or arrows. 我想创建带有场景或箭头的图。
Let's say I have this toy example 假设我有这个玩具的例子
temp <- data.frame(posi=c(1,2,3,3,2,1,5), from=c("A", "B", "C", "D", "D", "B", "A"),
to=c( "C", "D", "D", "C", "A", "A", "B"))
posi from to
1 A C
2 B D
3 C D
3 D C
2 D A
1 B A
5 A B
And I want to plot segments or arrows from the points defined by "from" and its starting position to the points defined by "to" and position+1. 我想绘制从“ from”定义的点及其起始位置到“ to”和position + 1定义的点的线段或箭头。
I got to create the plot with ggplot 我必须用ggplot创建剧情
ggplot() + geom_point(data=temp, aes(x=posi, y=from, size=10),show.legend = FALSE) +
geom_point(data=temp, aes(x=posi+1, y=to, size=10),show.legend = FALSE) +
geom_segment(data=temp, aes(x=posi, y=from, xend=posi+1, yend=to),
arrow=arrow(type="closed", angle=10), size=1.5, color="blue") + theme_bw()
But I would like to do it with Lattice because my real dataset is much bigger (millions of rows) and lattice works faster. 但是我想用Lattice做到这一点,因为我的真实数据集更大(数百万行)并且晶格工作更快。 I know I can reduce the number of lines removing duplicates but that's another story.
我知道我可以减少删除重复项的行数,但这是另一回事了。
How can I do it with lattice? 如何使用晶格?
I've been researchig and I think I need to use a panel.segments or lsegments, but things seem to be much more complicated and it's difficult to find examples. 我一直在研究,我认为我需要使用panel.segments或lsegments,但是事情似乎要复杂得多,很难找到示例。
xyplot(from ~ posi , type="p", col="black", data=temp, pch=16,
panel = function(x, y, ...){ panel.segments(x,y,) })
I don't know what parameters to write inside function nor inside panel. 我不知道要在函数内部或面板内部编写哪些参数。
You could use the following code: 您可以使用以下代码:
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", )
}
)
yielding the following graph: 产生下图:
Please let me know whether this is what you want. 请让我知道这是否是您想要的。
I posted a question concerning the problem that @skan identified and described in the comments: when a "extreme" level (like "D") is not present in temp$from
then the "D" will not part of the graph, even when "D" will be needed later for temp$to
. 我发布了一个有关@skan识别并在评论中描述的问题的问题:当
temp$from
不存在“极端”级别(如“ D”)时,则即使在以下情况temp$from
,“ D”也不会成为图形的一部分: temp$to
稍后将需要“ D”。 The question with answer from @Konn may be found here . @Konn回答的问题可以在这里找到。
As I understand it now, we need a factor
that is ordered
, and we need an addition to the code specifying drop.unused.levels = FALSE
in the call to xyplot
. 据我所了解,我们需要一个
ordered
的factor
,并且需要在对xyplot
的调用中指定drop.unused.levels = FALSE
的代码中进行xyplot
。 In the example we show the full set with "extremes" in "from" and as subset where the extreme "D" is absent: The full code is: 在示例中,我们在“ 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" )
})
yielding the following pics: 产生以下图片:
I think we have solved this question. 我认为我们已经解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.