如何在R中用莱迪思绘制线段或箭头? (来自宽格式数据集) [英] How to plot segments or arrows with Lattice in R? (from a Wide format dataset)
问题描述
我想说我有这个玩具的例子
pre>
temp < - data.frame(posi = c(1,2,3,3,1,5),from = c(A,B, C,D,D,B,A),
to = c(C,D,D,C,A A,B))
posi至
1 AC
2 BD
3 CD
3 DC
2 DA
1 BA
5 AB
我想从点由from定义,其起始位置为由to和位置+1定义的点。
我用ggplot创建了绘图
pre $
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()
但我想用莱迪思来做,因为我的真实数据集要大得多(数百万行)并且格子工作得更快。
我知道我可以减少删除重复项目的行数,但这是另一回事。
我怎样才能用格子做到这一点?
我一直在研究,我认为我需要使用panel.segments或lsegments,但事情似乎要复杂得多,而且很难找到示例。
xyplot(from〜posi,type =p,col =black,data = temp,pch = 16,
panel = function x,y,...){panel.segments(x,y,)})
I不知道在函数内部面板里写什么参数。
<您可以使用以下代码:
xyplot(from数据= 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,
panel.arrows(x0 = temp $ posi,y0 = temp $ from,x1 = temp $ posi + 1,y1 = temp $ to,lwd = 3,col = blue,)
}
)
产生如下图: / p>
请让我知道这是你
更新
我发表了一篇关于@skan在评论中标识和描述的问题:当 temp $从
中不存在极端级别(如D)时,则D不会成为图的一部分,即使D会稍后需要 temp $ to
。从@Konn回答的问题可能发现
我想我们已经解决了这个问题。
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.
I got to create the plot with 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. 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.
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.
UPDATE
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
. The question with answer from @Konn may be found here.
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
. 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:
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.
这篇关于如何在R中用莱迪思绘制线段或箭头? (来自宽格式数据集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!