2

I have a set of node/edge files to generate the network using ggnet2. I am facing two issues:

  1. Some might only have 5-10 nodes while others might have 1000-10000 nodes which makes the plot too crowd. So I am curious if there is any option to automatically adjust the node and label size.

  2. While some label string are long (ie.e >20 chars), part of the label will be out of the image. Is there any option to avoid that?

The code is as bellow

v = read.csv(vfile, sep="\t", na.string="nnn")
e = read.csv(efile, sep="\t", na.string="nnn")

# build the network
net = network(e, directed = TRUE, mode="hall")

# group affiliation
x = data.frame(Entity = network.vertex.names(net))
x = merge(x, v, by.x = 'Entity', by.y='ID', sort = FALSE)
net %v% "Group" = as.character(x$Group)

# color palette
y = RColorBrewer::brewer.pal(9, "Set2")[ c(1:length(levels(x$Group))) ]
names(y) = levels(x$Group)

# network plot
png(graphFile,width = 8, height = 6, units = 'in', res = 300)
ggnet2(
  net, color = "Group", palette = y,  
  node.size = 12,
  edge.size = 1, edge.alpha = 0.5,
  label = TRUE, label.size = 3, label.alpha = 0.8, 
  legend.size = 12, legend.position = "right",
  arrow.size=5, arrow.gap=0.01)
dev.off()
Mavershang
  • 1,266
  • 2
  • 15
  • 27

0 Answers0