Customize plots of incidence

Thibaut Jombart

2018-08-24

This vignette provides some tips for the most common customisations of graphics produced by plot.incidence. Our graphics use ggplot2, which is a distinct graphical system from base graphics. If you want advanced customisation of your incidence plots, we recommend following an introduction to ggplot2.


Example data: simulated Ebola outbreak

This example uses the simulated Ebola Virus Disease (EVD) outbreak from the package outbreaks: ebola_sim_clean.

First, we load the data:

library(outbreaks)
library(ggplot2)
library(incidence)

onset <- ebola_sim_clean$linelist$date_of_onset
class(onset)
#> [1] "Date"
head(onset)
#> [1] "2014-04-07" "2014-04-15" "2014-04-21" "2014-04-27" "2014-04-26"
#> [6] "2014-04-25"

We compute the weekly incidence:

i <- incidence(onset, interval = 7)
i
#> <incidence object>
#> [5829 cases from days 2014-04-07 to 2015-04-27]
#> [5829 cases from ISO weeks 2014-W15 to 2015-W18]
#> 
#> $counts: matrix with 56 rows and 1 columns
#> $n: 5829 cases in total
#> $dates: 56 dates marking the left-side of bins
#> $interval: 7 days
#> $timespan: 386 days
#> $cumulative: FALSE

i.sex <- incidence(onset, interval = 7, group =  ebola_sim_clean$linelist$gender)
i.sex
#> <incidence object>
#> [5829 cases from days 2014-04-07 to 2015-04-27]
#> [5829 cases from ISO weeks 2014-W15 to 2015-W18]
#> [2 groups: f, m]
#> 
#> $counts: matrix with 56 rows and 2 columns
#> $n: 5829 cases in total
#> $dates: 56 dates marking the left-side of bins
#> $interval: 7 days
#> $timespan: 386 days
#> $cumulative: FALSE

i.hosp <- incidence(onset, interval = 7, group =  ebola_sim_clean$linelist$hospital)
i.hosp
#> <incidence object>
#> [5829 cases from days 2014-04-07 to 2015-04-27]
#> [5829 cases from ISO weeks 2014-W15 to 2015-W18]
#> [6 groups: Connaught Hospital, Military Hospital, NA, Princess Christian Maternity Hospital (PCMH), Rokupa Hospital, other]
#> 
#> $counts: matrix with 56 rows and 6 columns
#> $n: 5829 cases in total
#> $dates: 56 dates marking the left-side of bins
#> $interval: 7 days
#> $timespan: 386 days
#> $cumulative: FALSE


The plot.incidence function

When calling plot on an incidence object, the function plot.incidence is implicitely used. To access its documentation, use ?plot.incidence. In this section, we illustrate existing customizations.

Default behaviour

By default, the function uses grey for single time series, and colors from the color palette incidence_pal1 when incidence is computed by groups:

However, some of these defaults can be altered through the various arguments of the function:

Changing colors

The default palette

A color palette is a function which outputs a specified number of colors. By default, the color used in incidence is called incidence_pal1. Its behaviour is different from usual palettes, in the sense that the first 4 colours are not interpolated:

This pallette also has a light and a dark version:

Using different palettes

Other color palettes can be provided via col_pal. Various palettes are part of the base R distribution, and many more are provided in additional packages. We provide a couple of examples:

Specifying colors manually

Colors can be specified manually using the argument color; note that whenever incidence is computed by groups, the number of colors must match the number of groups, otherwise color is ignored.

Example 1: changing a single color

Example 2: changing several colors (note that naming colors is optional)

Useful ggplot2 tweaks

Numerous tweaks for ggplot2 are documented online. In the following, we merely provide a few useful tips in the context of incidence.

Changing dates on the x-axis

Changing date format

By default, the dates indicated on the x-axis of an incidence plot may not have the suitable format. The package scales can be used to change the way dates are labelled (see ?strptime for possible formats):

And for another example, with a subset of the data (first 50 days), using more detailed dates and rotating the annotations:

Note that you can save customisations for later use:

Changing the grid

The last example above illustrates that it can be useful to have denser annotations of the x-axis, especially over short time periods. Here, we provide an example where we try to zoom on the peak of the epidemic, using the data by hospital:

Let us look at the data 40 days before and after the 1st of October:

Changing the legend

The previous plot has a fairly large legend which we may want to move around. Let us save the plot as a new object p and customize the legend: