如何将动态 geom_segment 与计数变量一起使用
我是 ggplot2 的新手,想动态地在现有绘图中绘制线条,但这不起作用。它只是在绘制整个图片时进行最后一次计数。
我有2个矩阵: “clVrd”
[,1] [,2]
[1,] 0.6618725 -0.04065907
[2,] 0.4646620 0.09859806
[3,] 0.9388307 0.05681554
[4,] 1.1809942 0.12906415
[5,] 1.5476428 0.49644973
[6,] -0.1855485 0.30445869
[7,] 0.4525888 0.49559198
[8,] -0.4004534 -0.06419374
[9,] -1.0669191 0.17292748
[10,] -0.9372038 0.02601539
[11,] 0.5617849 -5.21857716
[12,] -0.9370099 -0.05539107
[13,] 0.6803453 0.21223368
[14,] 1.3040601 0.47598799
和“mid”
[,1] [,2]
[1,] -0.1958772 0.3012428
[2,] 0.5115807 0.4142237
[3,] -0.6585965 0.2623573
[4,] 0.4680863 -1.4964873
[5,] -1.2431780 0.2383014
[6,] -2.3507773 0.0954886
[7,] -0.5547284 -2.1393520
[8,] 0.1314092 0.3408999
[9,] 0.7592055 -0.8161825
[10,] 0.8247861 0.5152814
[11,] -1.8667328 0.1344475
[12,] -0.4825223 -4.0975561
以及一个矩阵“active”,告诉循环是否画一条线(1)或不画一条线(0)。
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[2,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[3,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[4,] 1 1 1 1 1 1 1 1 1 1 1 1 1
[5,] 1 1 1 1 0 1 1 1 1 1 0 1 1
[6,] 0 0 0 0 0 0 0 0 1 1 0 1 0
[7,] 1 1 1 1 1 1 1 1 1 1 1 1 1
[8,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[9,] 1 1 1 1 1 1 1 1 1 1 1 1 1
[10,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[11,] 0 0 0 0 0 1 0 1 1 1 0 1 0
[12,] 1 1 1 1 1 1 1 1 1 1 1 1 1
主图
g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2]), geom = "point", stat="identity", size = I(1), alpha = I(0.2))
这个调用产生了运行良好的 。现在我想在相应的矩阵 clVrd 和 mid 中从每个条目 y(行)到每个 x(列)画一条线,如果处于活动状态是 1。
我尝试了以下循环:
for (i in 1:13){ # Draw the lines between middlepoints and infrastructures
for (j in 1:maxcl){
if (active[j,i]==1){
g <- g + geom_segment(aes(x=clVrd[i,1], y=clVrd[i,2], xend=mid[j,1], yend=mid[j,2]), color='grey')
}
}
}
这不起作用。它只是画出 i=13 和 j=12 的线。如果我用 head(g) 查看 g,我可以看到他添加了标签,
$layers[[132]]
mapping: x = clVrd[i, 1], y = clVrd[i, 2], xend = mid[j, 1], yend = mid[j, 2]
geom_segment: colour = grey
stat_identity:
position_identity: (width = NULL, height = NULL)
这解释了为什么他只画 i=13 和 j=12 的线。但我该如何解决这个问题呢?而且不应该有157层吗?这里只有132个还是我理解有问题?
感谢您的帮助。
多米尼克
I'm new to ggplot2 and would like to plot lines in an existing plot dynamicly, which doesn't work. It just take the last count while plotting the whole picture.
I've 2 matrixes:
"clVrd" with
[,1] [,2]
[1,] 0.6618725 -0.04065907
[2,] 0.4646620 0.09859806
[3,] 0.9388307 0.05681554
[4,] 1.1809942 0.12906415
[5,] 1.5476428 0.49644973
[6,] -0.1855485 0.30445869
[7,] 0.4525888 0.49559198
[8,] -0.4004534 -0.06419374
[9,] -1.0669191 0.17292748
[10,] -0.9372038 0.02601539
[11,] 0.5617849 -5.21857716
[12,] -0.9370099 -0.05539107
[13,] 0.6803453 0.21223368
[14,] 1.3040601 0.47598799
and "mid" with
[,1] [,2]
[1,] -0.1958772 0.3012428
[2,] 0.5115807 0.4142237
[3,] -0.6585965 0.2623573
[4,] 0.4680863 -1.4964873
[5,] -1.2431780 0.2383014
[6,] -2.3507773 0.0954886
[7,] -0.5547284 -2.1393520
[8,] 0.1314092 0.3408999
[9,] 0.7592055 -0.8161825
[10,] 0.8247861 0.5152814
[11,] -1.8667328 0.1344475
[12,] -0.4825223 -4.0975561
and a matrix "active" that tells the loop if to draw a line (1) or not (0)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[2,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[3,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[4,] 1 1 1 1 1 1 1 1 1 1 1 1 1
[5,] 1 1 1 1 0 1 1 1 1 1 0 1 1
[6,] 0 0 0 0 0 0 0 0 1 1 0 1 0
[7,] 1 1 1 1 1 1 1 1 1 1 1 1 1
[8,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[9,] 1 1 1 1 1 1 1 1 1 1 1 1 1
[10,] 1 1 1 1 1 1 1 1 1 1 0 1 1
[11,] 0 0 0 0 0 1 0 1 1 1 0 1 0
[12,] 1 1 1 1 1 1 1 1 1 1 1 1 1
This call produces the main plot
g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2]), geom = "point", stat="identity", size = I(1), alpha = I(0.2))
which works well. Now i want to draw a line from each entry y (row) to each x (col) in the corresponding matrices clVrd and mid if in active is a 1.
I tried the following loop:
for (i in 1:13){ # Draw the lines between middlepoints and infrastructures
for (j in 1:maxcl){
if (active[j,i]==1){
g <- g + geom_segment(aes(x=clVrd[i,1], y=clVrd[i,2], xend=mid[j,1], yend=mid[j,2]), color='grey')
}
}
}
which doesn't work. It just draw the lines for i=13 and j=12. If I look into g with head(g) I can see that he adds the lables with
$layers[[132]]
mapping: x = clVrd[i, 1], y = clVrd[i, 2], xend = mid[j, 1], yend = mid[j, 2]
geom_segment: colour = grey
stat_identity:
position_identity: (width = NULL, height = NULL)
which explains why he just draw the lines for i=13 and j=12. But how I can fix that? And shouldn't there be 157 layers? Here there are only 132 or do I understand something wrong?
Thanks for your help.
Dominik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
吃完晚饭休息一会儿后,我找到了答案。我不知道这是否是最好的解决方案,但它对我有用。
我们生成一个矩阵“lines”
,如果 active[x,y] 为 1(见上文),则将不同矩阵中的线的坐标放入新矩阵中。
现在我们可以用线条绘制图
但是我仍然必须检查一切是否正确,因为它似乎有点,但它应该
现在通过以下调用生成整个图片:
with clUrd is a 3x5982 matrix like
结果如下图!其中点是受访者,三角形是基础设施,正方形是集群的中点,颜色是不同的集群。
After I eat dinner and rest a bit, I found the answer. I don't know if it is the best solution, but it works for me.
We generate a matrix "lines"
and put the coordinates for the lines from the different matrices in our new one if active[x,y] is 1 (see above).
Now we can plot the plot with lines with
But I still have to check if everything is right, because it seems to be a bit of, but it should
The whole picture is now generated with the following call:
with clUrd is a 3x5982 matrix like
The result is the following picture! Where the dots are respondents, the triangle the infrastructures, the squares the midpoints of the clusters and the color the different clusters.