我有一个非常冗长的网络图。我基本上是想让它像一种流程图一样工作。我试图让它的美学既实用又令人愉悦,但我正经历着一段艰难的时期。图例的文本太大了,网络中没有足够的空间让所有内容都适当地隔开。另外,我想让背景变成不同的颜色。
然而,我所能找到的唯一解决方案是需要ggnet2包,但是当我尝试安装ggnet2包时,系统提示我无法在此版本的rStudio上安装该包。当我尝试更新我的rStudio时,它显示我有最新版本的rStudio。我不知道还能尝试什么。
任何帮助都将不胜感激!这是我的代码:
library(igraph)
library(RColorBrewer)
links <- data.frame(
source=c("Pubertal Hormones, Timing, and Development","Pubertal Hormones, Timing, and Development","Pubertal Hormones, Timing, and Development","Pubertal Hormones, Timing, and Development","Genetic Vulnerability","Genetic Vulnerability","Temperament","Temperament","Depressogenic Vulnerability","Negative Cognitive Style","Negative Cognitive Style","Objectified Body Consciousness","Objectifed Body Consciousness","Rumination","Peer Sexual Harassment","Peer Sexual Harassment"),
target=c("Genetic Vulnerability","Objectified Body Consciousness","Peer Sexual Harassment","Depressogenic Vulnerability","Temperament","Depressogenic Vulnerability","Negative Cognitive Style","Depressogenic Vulnerability","Gender Difference in Depression","Rumination","Depressogenic Vulnerability","Rumination","Depressogenic Vulnerability","Depressogenic Vulnerability","Objectified Body Consciousness","Gender Difference in Depression"),
importance=(sample(1:4, 16, replace=T))
)
V = c(links$source,
links$target) %>%
unique()
nodes <- data.frame( name=V,
carac=c(
rep("Biological Vulnerability",2),rep("Affective Vulnerability",3),rep("Cognitive Vulnerability",3),rep("Negative Life Events",2)) )
network <- graph_from_data_frame(d=links, vertices=nodes, directed=F)
coul <- brewer.pal(4, "Set3")
my_color <- coul[as.numeric(as.factor(V(network)$carac))]
plot(network, vertex.color=my_color,vertex.shape=c("vrectangle"),vertex.size=c(150),vertex.label.cex=c(0.5))
legend("bottom", legend=levels(as.factor(V(network)$carac)) , col = coul , bty = "n", pch=20 , pt.cex = 3, cex = 1, text.col=coul , horiz = TRUE, inset = c(0.1, 0.1))

发布于 2020-12-17 10:58:23
使用像这样的小图形,您可以自定义布局,使其看起来更好。我们将逐一介绍这些问题。让我们从你的plot语句开始,但让我们通过设置随机种子来使其可重现。
set.seed(123)
plot(network, vertex.color=my_color,vertex.shape=c("vrectangle"),
vertex.size=c(150),vertex.label.cex=c(0.5))

最大的问题是绘图没有使用图形窗口中的所有空间。您可以使用margin参数对其进行一些调整。当我这样做的时候,框看起来太大了,所以我会同时把它们变小一点,并改变文本大小以匹配新的框。
set.seed(123)
LO = layout_nicely(network)
plot(network, layout=LO, margin=-0.6, vertex.color=my_color,
vertex.shape=c("vrectangle"), vertex.size=c(100),
vertex.label.cex=c(0.7))

好了,这样好多了,但还是有一些问题。首先,“青春期激素、时机和发育”的盒子太小了。我们可以通过使用顶点大小的向量来调整它。当我们改变这一点时,在图中还有一些其他的改变,所以让我们先做一个改变。
VS = rep(100, vcount(network))
VS[1] = 150
plot(network, layout=LO, margin=-0.7, vertex.color=my_color,
vertex.shape=c("vrectangle"), vertex.size=VS,
vertex.label.cex=c(0.7))

下一步,一些框重叠。我们可以通过编辑布局矩阵来解决这些问题。这个矩阵就是绘制节点的x,y坐标。我只是看了看矩阵,稍微调整了一下位置。
LO[1,] = c(6.3,7.3)
LO[4,] = c(5.4,6.7)
LO[5,] = c(5,5.5)
LO[7,] = c(7.4,6.8)
LO[8,] = c(5.1,6.2)
LO[9,] = c(5.2,8.4)
LO[10,] = c(7,8.3)
plot(network, layout=LO, margin=-0.7, vertex.color=my_color,
vertex.shape=c("vrectangle"), vertex.size=VS,
vertex.label.cex=c(0.7))

这是非常好的。还可以做更多的工作,但我认为方法是明确的,所以我将把节点的任何额外的美学调整留给您。
现在,您需要调整背景颜色,以完成此操作。可以通过使用par设置bg来执行此操作(在打印之前)。最终版本是:
par(bg="lightblue1")
plot(network, layout=LO, margin=-0.7, vertex.color=my_color,
vertex.shape=c("vrectangle"), vertex.size=VS,
vertex.label.cex=c(0.7))

https://stackoverflow.com/questions/65333374
复制相似问题