Fredrik
Fredrik

Reputation: 13

How to add custom vertex labels to a ggnet graph in R?

I know it is possible to add vertex labels (printed out on the vertex) to ggnet network graphs (see for example the city network here), but I cannot figure out how. Here is a minimal example:

require(network); require(GGally)
nw <- network(matrix(c(0,1,1,0),2))
ggnet(nw,node.group="g",node.color="black",label.nodes=c("a","b"))

This gives me a network with nodes labelled 1 and 2, not "a" and "b". The only thing I can adjust with label.nodes is which of the nodes get their number labels printed out, that is, if I write label.nodes=2, then the first label lacks a label and the second one gets the label 2.

I suspect I may need to set some vertice names parameter when creating the network, and have experimented with this, but without results. Does anyone know what I do wrong?

Upvotes: 1

Views: 916

Answers (1)

MrFlick
MrFlick

Reputation: 206411

The label.nodes parameter can either be set to TRUE to label all vertices, or it can be a character vector of vertex names to label only those nodes. It does not change the name of the vertex.

So where do the names come from? Well, they come from the network object itself. You can see the current node names with

network.vertex.names(nw)
# [1] 1 2

and can set them with

network.vertex.names(nw) <- c("a","b")

now if you draw the graph, you will get

ggnet(nw,node.group=c("A","B"),label.nodes=T, col="white")

enter image description here

Note that the node.groups define the colors and the legend (those have capital letters) while the labels use the vertex names form the nw object.

Upvotes: 1

Related Questions