Centrality
Author: by James Hollway

Skip to Tutorial Content

## Calculating centrality

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 `manynet::autographr()`.

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

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

    # plot the one-mode version
    autographr(ison_brandes)
    ison_brandes2 <- ison_brandes %>% rename(type = twomode_type)
    # plot the two-mode version
    autographr(ison_brandes2)

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. 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"
    autographr(____)

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

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

### Degree centrality

Let’s start with calculating degree, as it is 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 migraph like this:
    node_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 migraph though:
    node_degree(ison_brandes, normalized = FALSE)

Often we are interested in the distribution of (degree) centrality in a network. `{migraph}` 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_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. What kind of shape does this have?

### Other centralities

Other measures of centrality can be a little trickier to calculate by hand. Fortunately, we can use functions from `{migraph}` 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_betweenness() function to calculate the
    # betweenness centralities of nodes in a network
    node_betweenness(ison_brandes)

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

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

    node_betweenness(ison_brandes)
    node_closeness(ison_brandes)
    node_eigenvector(ison_brandes)
    # TASK: Can you create degree distributions for each of these?

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:

- in what ways is a higher degree actor more ‘central’?
- can you explain why a node that has the smallest sum of geodesic distances to all other nodes is said to be ‘central’?
- why would an actor lying ‘between’ two other actors be ‘central’?
- 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 `{migraph}` return normalized scores by default – for the raw scores, include `normalized = FALSE` in the function as an extra argument.

## Plotting centrality

It is straightforward in `{migraph}` 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 `autographr()` 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 %>%
      add_node_attribute("color", node_is_max(node_degree(ison_brandes))) %>%
      autographr(node_color = "color")

    ison_brandes %>%
      add_node_attribute("color", node_is_max(node_betweenness(ison_brandes))) %>%
      autographr(node_color = "color")

    ison_brandes %>%
      add_node_attribute("color", node_is_max(node_closeness(ison_brandes))) %>%
      autographr(node_color = "color")

    ison_brandes %>%
      add_node_attribute("color", node_is_max(node_eigenvector(ison_brandes))) %>%
      autographr(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_degree(ison_brandes2))) %>%
      autographr(node_color = "color")

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_betweenness(ison_brandes2))) %>%
      autographr(node_color = "color")

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_closeness(ison_brandes2))) %>%
      autographr(node_color = "color")

    ison_brandes2 %>%
      add_node_attribute("color", node_is_max(node_eigenvector(ison_brandes2))) %>%
      autographr(node_color = "color")

## Calculating centralization

`{migraph}` 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 `network_`:

    network_degree(ison_brandes)
    network_betweenness(ison_brandes)
    network_closeness(ison_brandes)
    network_eigenvector(ison_brandes)

By default, scores are printed 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, 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_degree(ison_brandes))) %>%
      add_node_attribute("betweenness",
                                  node_is_max(node_betweenness(ison_brandes))) %>%
      add_node_attribute("closeness",
                                  node_is_max(node_closeness(ison_brandes))) %>%
      add_node_attribute("eigenvector",
                                  node_is_max(node_eigenvector(ison_brandes)))
    gd <- autographr(ison_brandes, node_color = "degree") +
      ggtitle("Degree", subtitle = round(network_degree(ison_brandes), 2))
    gc <- autographr(ison_brandes, node_color = "closeness") +
      ggtitle("Closeness", subtitle = round(network_closeness(ison_brandes), 2))
    gb <- autographr(ison_brandes, node_color = "betweenness") +
      ggtitle("Betweenness", subtitle = round(network_betweenness(ison_brandes), 2))
    ge <- autographr(ison_brandes, node_color = "eigenvector") +
      ggtitle("Eigenvector", subtitle = round(network_eigenvector(ison_brandes), 2))
    (gd | gb) / (gc | ge)
    # ggsave("brandes-centralities.pdf")

## Tasks

1. Name a plausible research question you could ask of this data for each of the four main centrality measures (degree, betweenness, closeness, eigenvector) You may want to add these as titles or subtitles to each plot.

# Centrality

#### by James Hollway