R 如何使用带计数变量的动态geom_段
我是ggplot2新手,希望在现有绘图中动态绘制线,但这不起作用。它只是在绘制整个图片时进行最后一次计数 我有两个矩阵: “clVrd”与 “中”与 和一个矩阵“active”,它告诉循环是否绘制一条线(1)或不绘制一条线(0) 此调用生成主绘图R 如何使用带计数变量的动态geom_段,r,plot,ggplot2,R,Plot,Ggplot2,我是ggplot2新手,希望在现有绘图中动态绘制线,但这不起作用。它只是在绘制整个图片时进行最后一次计数 我有两个矩阵: “clVrd”与 “中”与 和一个矩阵“active”,它告诉循环是否绘制一条线(1)或不绘制一条线(0) 此调用生成主绘图 g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2]), geom = "point", stat="identity", size
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))
这就解释了为什么他只画了i=13和j=12的线。但我怎样才能解决这个问题呢?难道不应该有157层吗?这里只有132个,还是我理解错了
谢谢你的帮助
多米尼克在我吃过晚饭休息了一会儿后,我找到了答案。我不知道这是否是最好的解决方案,但它对我有效 我们生成一个矩阵“线” 结果如下图!点是回应者,三角形是基础设施,正方形是簇的中点,不同簇的颜色
- 抱歉,无法发布图片,我不被允许,因为我没有足够的分数。。。那太愚蠢了
?dput
)?此外,添加一些图像可能会带来一些额外的关注。这是真的,但对于那些试图帮助的人来说,如何将这些矩阵加载到R中并不明显。如果您已经像我建议的那样粘贴了R对象的导出结构(使用dput
),那么会容易得多。谢谢,我以后会这样做……您能告诉我们晚餐吃了什么,休息了多长时间,以便其他人可以复制您的解决方案吗?:-)在geom_段中
什么是test
对象?如果我没记错的话,这只是一个复制粘贴错误
[,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
[,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))
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')
}
}
}
$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)
lines <- numeric()
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))
[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