Skip to Tutorial Content

## Today’s target

The aim of this tutorial is to show how we can measure and map degree, betweenness, closeness, eigenvector, and other types of centrality, explore their distributions and calculate the corresponding centralisation.

### Setting up

For this exercise, we’ll use the `ison_brandes` dataset in `{manynet}`. This dataset is in a ‘tidygraph’ format, but `manynet` makes it easy to coerce this into other forms to be compatible with other packages. We can create a two-mode version of the dataset by renaming the nodal attribute “twomode_type” to just “type”. Let’s begin by graphing these datasets using `graphr()`.

    # Let's graph the one-mode version
    graphr(____)

    # Now, let's create a two-mode version 'ison_brandes2' and graph it.
    ison_brandes2 <- ison_brandes %>% rename(type = twomode_type)
    graphr(____)

    # plot the one-mode version
    graphr(ison_brandes)
    ison_brandes2 <- ison_brandes %>% rename(type = twomode_type)
    # plot the two-mode version
    graphr(ison_brandes2, layout = "bipartite")

The network is anonymous, but I think it would be nice to add some names, even if it’s just pretend. Luckily, `{manynet}` has a function for this: `to_named()`. This makes plotting the network just a wee bit more accessible and interpretable:

    ison_brandes <- to_named(ison_brandes)

    # Now, let's graph using the object names: "ison_brandes"
    graphr(____)

    ison_brandes <- to_named(ison_brandes)
    # plot network with names
    graphr(ison_brandes)

Note that you will likely get a different set of names (though still alphabetical), as they are assigned randomly from a pool of (American) first names.

## Degree centrality

Let’s start with calculating degree . Remember that degree centrality is just the number of incident edges/ties to each node. It is therefore easy to calculate yourself. Just sum the rows or columns of the matrix!

    # We can calculate degree centrality like this:
    (mat <- as_matrix(ison_brandes))
    (degrees <- rowSums(mat))
    rowSums(mat) == colSums(mat)

    # Or by using a built in command in manynet like this:
    node_by_degree(ison_brandes, normalized = FALSE)

    # manually calculate degree centrality
    mat <- as_matrix(ison_brandes)
    degrees <- rowSums(mat)
    rowSums(mat) == colSums(mat)
    # You can also just use a built in command in manynet though:
    node_by_degree(ison_brandes, normalized = FALSE)

## Degree distributions

Often we are interested in the distribution of (degree) centrality in a network. `{autograph}` offers a way to get a pretty good first look at this distribution , though there are more elaborate ways to do this in base and grid graphics.

    # distribution of degree centrality scores of nodes
    plot(node_by_degree(ison_brandes))

What’s plotted here by default is both the degree distribution as a histogram, as well as a density plot overlaid on it in red.

## Other centralities

Other measures of centrality can be a little trickier to calculate by hand. Fortunately, we can use functions from `{netrics}` to help calculate the betweenness , closeness , and eigenvector centralities for each node in the network. Let’s collect the vectors of these centralities for the `ison_brandes` dataset:

    # Use the node_by_betweenness() function to calculate the
    # betweenness centralities of nodes in a network
    node_by_betweenness(ison_brandes)

    # Use the node_by_closeness() function to calculate the
    # closeness centrality of nodes in a network
    node_by_closeness(ison_brandes)

    # Use the node_by_eigenvector() function to calculate
    # the eigenvector centrality of nodes in a network
    node_by_eigenvector(ison_brandes)

    node_by_betweenness(ison_brandes)
    node_by_closeness(ison_brandes)
    node_by_eigenvector(ison_brandes)

What is returned here are vectors of betweenness, closeness, and eigenvector scores for the nodes in the network. But what do they mean?

Try to answer the following questions for yourself:

- what does Bonacich mean when he says that power and influence are not the same thing?
- can you think of a real-world example when an actor might be central but not powerful, or powerful but not central?

Note that all centrality measures in `{netrics}` return normalized scores by default – for the raw scores, include `normalized = FALSE` in the function as an extra argument.

Now, can you create degree distributions for each of these?

    plot(node_by_betweenness(ison_brandes))
    plot(node_by_closeness(ison_brandes))
    plot(node_by_eigenvector(ison_brandes))

## Plotting centrality

It is straightforward in `{autograph}` to highlight nodes and ties with maximum or minimum (e.g. degree) scores. If the vector is numeric (i.e. a “measure”), then this can be easily converted into a logical vector that identifies the node/tie with the maximum/minimum score using e.g. `node_is_max()` or `tie_is_min()`. By passing this attribute to the `graphr()` argument “node_color” we can highlight which node or nodes hold the maximum score in red.

    # plot the network, highlighting the node with the highest centrality score with a different colour
    ison_brandes %>%
      mutate_nodes(color = node_is_max(node_by_degree())) %>%
      graphr(node_color = "color")

    ison_brandes %>%
      mutate_nodes(color = node_is_max(node_by_betweenness())) %>%
      graphr(node_color = "color")

    ison_brandes %>%
      mutate_nodes(color = node_is_max(node_by_closeness())) %>%
      graphr(node_color = "color")

    ison_brandes %>%
      mutate_nodes(color = node_is_max(node_by_eigenvector())) %>%
      graphr(node_color = "color")

How neat! Try it with the two-mode version. What can you see?

    # Instead of "ison_brandes", use "ison_brandes2"

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_by_degree(ison_brandes2))) %>%
      graphr(node_color = "color", layout = "bipartite")

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_by_betweenness(ison_brandes2))) %>%
      graphr(node_color = "color", layout = "bipartite")

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_by_closeness(ison_brandes2))) %>%
      graphr(node_color = "color", layout = "bipartite")

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_by_eigenvector(ison_brandes2))) %>%
      graphr(node_color = "color", layout = "bipartite")

## Centralization

`{netrics}` also implements network centralization functions. Here we are no longer interested in the level of the node, but in the level of the whole network, so the syntax replaces `node_` with `net_`:

    net_by_degree(ison_brandes)
    net_by_betweenness(ison_brandes)
    net_by_closeness(ison_brandes)
    print(net_by_eigenvector(ison_brandes), digits = 5)

By default, scores are printed up to 3 decimal places, but this can be modified and, in any case, the unrounded values are retained internally. This means that even if rounded values are printed (to respect console space), as much precision as is available is used in further calculations.

Note that for centralization in two-mode networks, two values are given (as a named vector), since normalization typically depends on the (asymmetric) number of nodes in each mode.

What if we want to have a single image/figure with multiple plots? This can be a little tricky with gg-based plots, but fortunately the `{patchwork}` package is here to help.

    ison_brandes <- ison_brandes %>%
      add_node_attribute("degree", node_is_max(node_by_degree(ison_brandes))) %>%
      add_node_attribute("betweenness", node_is_max(node_by_betweenness(ison_brandes))) %>%
      add_node_attribute("closeness", node_is_max(node_by_closeness(ison_brandes))) %>%
      add_node_attribute("eigenvector", node_is_max(node_by_eigenvector(ison_brandes)))
    gd <- graphr(ison_brandes, node_color = "degree") +
      ggtitle("Degree", subtitle = round(net_by_degree(ison_brandes), 2))
    gc <- graphr(ison_brandes, node_color = "closeness") +
      ggtitle("Closeness", subtitle = round(net_by_closeness(ison_brandes), 2))
    gb <- graphr(ison_brandes, node_color = "betweenness") +
      ggtitle("Betweenness", subtitle = round(net_by_betweenness(ison_brandes), 2))
    ge <- graphr(ison_brandes, node_color = "eigenvector") +
      ggtitle("Eigenvector", subtitle = round(net_by_eigenvector(ison_brandes), 2))
    (gd | gb) / (gc | ge)
    # ggsave("brandes-centralities.pdf")

## Free play

Choose another dataset included in `{manynet}`. Name a plausible research question you could ask of the dataset relating to each of the four main centrality measures (degree, betweenness, closeness, eigenvector). Plot the network with nodes sized by each centrality measure, using titles or subtitles to record the question and/or centralization measure.

## Glossary

Here are some of the terms that we have covered in this module:

Betweenness
  The betweenness centrality of a node is the proportion of shortest paths between all pairs of nodes that pass through that node.
Centralization
  A measure of how unequal the centralities of the nodes in a network are.
Closeness
  The closeness centrality of a node is the reciprocal of the sum of its distances to all other nodes.
Degree
  The degree of a node is the number of connections it has.
Distribution
  A degree distribution is the frequency distribution of the degrees of the nodes in a network.
Eigenvector
  The eigenvector centrality of a node is the corresponding value in the dominant eigenvector of the adjacency matrix.
Power
  An eigenvector-style centrality that allows being connected to more peripheral nodes to contribute centrality.

# Centrality

#### by James Hollway