如何将动态 geom_segment 与计数变量一起使用

发布于 2024-11-26 10:29:22 字数 2959 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会发光的星星闪亮亮i 2024-12-03 10:29:22

吃完晚饭休息一会儿后,我找到了答案。我不知道这是否是最好的解决方案,但它对我有用。

我们生成一个矩阵“lines”

lines <- numeric()

,如果 active[x,y] 为 1(见上文),则将不同矩阵中的线的坐标放入新矩阵中。

for (i in 1:13){
    for (j in 1:maxcl){
        if (active[j,i]==1){
          lines <- rbind(lines, c(clVrd[i,1], clVrd[i,2], mid[j,1], mid[j,2]))
        }
    }
}

现在我们可以用线条绘制图

g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2], col=factor(clUrd[,3])), geom = "point", stat="identity", size = I(1), alpha = I(0.2))

但是我仍然必须检查一切是否正确,因为它似乎有点,但它应该

现在通过以下调用生成整个图片:

g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2], col=factor(clUrd[,3])), geom = "point", stat="identity", size = I(1.5), alpha = I(0.2)) +
  scale_colour_brewer(palette="Paired") +
  geom_segment(data=data.frame(test), aes(x=lines[,1], y=lines[,2], xend=lines[,3], yend=lines[,4]), color='grey', alpha = I(0.2)) +
  layer(data=data.frame(clVrd), mapping=aes(x=clVrd[,1], y=clVrd[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=2) +
  layer(data=data.frame(mid), mapping=aes(x=mid[,1], y=mid[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=22) +
  opts(legend.position = "none") +
  scale_x_continuous('Dimension 1') +
  scale_y_continuous('Dimension 2') +
  opts(axis.title.y = theme_text(vjust=0.2, angle=90)) +
  opts(axis.title.x = theme_text(vjust=0.2, angle=0))

with clUrd is a 3x5982 matrix like

[1,] -0.1454078  0.26175441    1
[2,]  0.4567384  0.46036161    2
[3,] -0.9269251  0.39196787    3
[4,] -0.8959093  0.06884224    3
[5,]  0.1618442 -1.12006536    4
[6,] -1.4021955  0.34594349    5

结果如下图!其中点是受访者,三角形是基础设施,正方形是集群的中点,颜色是不同的集群。

  • 抱歉,无法发布图片,我不允许,因为我没有足够的积分...那太愚蠢了。

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"

lines <- numeric()

and put the coordinates for the lines from the different matrices in our new one if active[x,y] is 1 (see above).

for (i in 1:13){
    for (j in 1:maxcl){
        if (active[j,i]==1){
          lines <- rbind(lines, c(clVrd[i,1], clVrd[i,2], mid[j,1], mid[j,2]))
        }
    }
}

Now we can plot the plot with lines with

g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2], col=factor(clUrd[,3])), geom = "point", stat="identity", size = I(1), alpha = I(0.2))

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:

g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2], col=factor(clUrd[,3])), geom = "point", stat="identity", size = I(1.5), alpha = I(0.2)) +
  scale_colour_brewer(palette="Paired") +
  geom_segment(data=data.frame(test), aes(x=lines[,1], y=lines[,2], xend=lines[,3], yend=lines[,4]), color='grey', alpha = I(0.2)) +
  layer(data=data.frame(clVrd), mapping=aes(x=clVrd[,1], y=clVrd[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=2) +
  layer(data=data.frame(mid), mapping=aes(x=mid[,1], y=mid[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=22) +
  opts(legend.position = "none") +
  scale_x_continuous('Dimension 1') +
  scale_y_continuous('Dimension 2') +
  opts(axis.title.y = theme_text(vjust=0.2, angle=90)) +
  opts(axis.title.x = theme_text(vjust=0.2, angle=0))

with clUrd is a 3x5982 matrix like

[1,] -0.1454078  0.26175441    1
[2,]  0.4567384  0.46036161    2
[3,] -0.9269251  0.39196787    3
[4,] -0.8959093  0.06884224    3
[5,]  0.1618442 -1.12006536    4
[6,] -1.4021955  0.34594349    5

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.

  • Sorry can't post a picture, I'm not allowed, because I have not enough points... That's stupid.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文