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

时间:2011-07-26 15:06:45

标签: r plot ggplot2

我是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))

运作良好。现在我想从每个条目y(行)到相应矩阵clVrd中的每个x(col)绘制一条线,如果在active中,则为mid。

我尝试了以下循环:

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的线条。如果我用头(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或者我理解错了吗?

感谢您的帮助。

多米尼克

1 个答案:

答案 0 :(得分:1)

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

我们生成矩阵“lines”

lines <- numeric()

如果活动[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))

与clUrd是一个3x5982矩阵,如

[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

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

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