1

I have a really wordy network plot. I am basically trying to have it function as a sort of flow chart. I am trying to make its aesthetics both functional and pleasing, but I am having a difficult time. The legend's text is way too large and there is not enough space in the network for everything to be spaced out properly. Also, I want to make the background a different color.

The only solutions I have been able to find, however, require the ggnet2 package, but when I try to install the ggnet2 package, it says I cannot install that on this version of rStudio. When I tried to update my rStudio, it says that I have the most recent version of rStudio. I do not know what else to try.

Any help would be much appreciated! This is my code:

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

this is what the network looks like!

Danielle
  • 35
  • 3

1 Answers1

1

With a small graph like this, you can customize the layout so that things look nicer. We'll just step through the problems one at a time. Let's start from your plot statement, but let's make it reproducible by setting the random seed.

set.seed(123)
plot(network, vertex.color=my_color,vertex.shape=c("vrectangle"),
    vertex.size=c(150),vertex.label.cex=c(0.5))

First plot

The biggest problem is that the plot does not use all of the space in the graphics window. You can adjust that some with the margin parameter. When I do that, the boxes seem too big, so I will make them a bit smaller at the same time and also change the text size to match the new boxes.

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

Better margins and box size.

OK, this is better, but there still are some problems. First, the box for "Pubertal Hormones, Timing, and Development" is too small. We can adjust that by using a vector of vertex sizes. When we change that, there are some other changes in the plot, so let's just make that one change first.

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

Adjusted box size

Next, some of the boxes overlap. We can fix those by editing the layout matrix. This matrix is just the x,y coordinates at which to plot the nodes. I just looked at the matrix and adjusted the positions a little.

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

network with adjust node positions

This is pretty good. More could be done, but I think the method is clear, so I will leave any additional aesthetic adjustments of the nodes to you.

Now to finish up, you wanted to adjust the background color. You can do that (before plotting) by setting bg using par. The final version is:

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

Plot with background color

G5W
  • 36,531
  • 10
  • 47
  • 80
  • hi! thank you so much! this is such a detailed walkthrough--i appreciate it so much and learned a lot!!! i have one question: when i put in some of this code, i get an error message that says "Error in i.parse.plot.params(graph, list(...)) : object 'LO' not found" I'm not sure what's wrong and why LO doesn't work? I tried googling and came up empty-handed. If you could explain why I might be getting that error, it would be much appreciated. – Danielle Dec 17 '20 at 21:22
  • 1
    Doh. I left out that line of code early on. I will add in the LO line. – G5W Dec 17 '20 at 21:33
  • thank you so much! I just have one last question--I noticed in some other non-ggplot2 methods that you can add arrows to the ends of lines. I was wondering if that's possible with ggplot2 and if you could show me how to do that? when I searched elsewhere online, i could only find it done using a different method than ggplot2. – Danielle Dec 17 '20 at 21:39
  • 1
    Sorry, I am not a big `ggplot2` user. Here in `igraph`, you can use `directed=T` in your `graph_from_data_frame` statement. – G5W Dec 17 '20 at 21:41
  • thank you so much for your help!!!! I learned a ton about igraph thanks to you! – Danielle Dec 17 '20 at 21:46