开发者

Howto use dynamic geom_segment with counting variables

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   开发者_如何学Python  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


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜