Visualise contact matrices

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.height = 10,
  fig.width = 10
)
library(contactdata)

In this vignette, we are going to try to visualise the contact matrices for 9 Western Europe countries. The list of these countries is:

countries <- c("Portugal", "Spain", "Italy", "France", "Ireland", "Belgium", "Luxembourg", "Germany", "Switzerland")

We can get the contact data for all these countries in a single function call with:

contact_data <- contact_df_countries(countries, location = "all")

For the visualisation itself, we use the graphical package ggplot2, which offers a large flexibility in terms of customisation:

library(ggplot2)
ggplot(contact_data, aes(x = age_from, y = age_to, fill = contact)) + 
  geom_tile() +
  facet_wrap(~ country) +
  coord_equal()

The default colour palette and theme are not very suitable for our data but we can easily edit them with:

ggplot(contact_data, aes(x = age_from, y = age_to, fill = contact)) + 
  geom_tile() +
  facet_wrap(~ country) +
  coord_equal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  scale_fill_viridis_c()

You can also visualise the contact data at a specific location, e.g. at school:

contact_data <- contact_df_countries(countries, location = "school")
ggplot(contact_data, aes(x = age_from, y = age_to, fill = contact)) + 
  geom_tile() +
  facet_wrap(~ country) +
  coord_equal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  scale_fill_viridis_c()


Try the contactdata package in your browser

Any scripts or data that you put into this service are public.

contactdata documentation built on Feb. 19, 2021, 9:06 a.m.