This week I’ve been attending the Functional Data and Beyond workshop at the Matrix centre in Creswick.
I spoke yesterday about using ggplot2
for functional data graphics, rather than the custom-built plotting functionality available in the many functional data packages, including my own rainbow package written with Hanlin Shang.
It is a much more powerful and flexible way to work, so I thought it would be useful to share some examples.
French mortality data
We will use the French mortality data from the demography
package, but we need to convert it into a tibble to begin.
1 |
|
1 |
|
The first thing to do is to re-create the rainbow plots that are popular for this type of data (introduced in my paper with Hanlin in JCGS in 2010). Here the year is mapped to colour. This works quite well for mortality data because it has trended consistently over time, allowing the colors to separate. It is one of the few situations where a rainbow palette is preferred to other palettes.
1 |
|
Another plot that has proved popular is to animate this rainbow plot by mapping year to animation time. With the new gganimate
package (still only on github), that is as easy as adding a few more lines to the end of the above code.
1 |
|
Another way of looking at the data is using an image map. Again, this is extremly easy using ggplot2.
1 |
|
Note the various wars and epidemics (seen as vertical lines), and the decrease in mortality rates over time (seen as the growing light-coloured area).
Since this is time series data, we should also look at the autocorrelation function. Because the data are functions of age, the autocorrelation is a surface for each lag value. The function facf
below computes a functional ACF surface (giving correlations between different ages and across lagged years). There is some tricky non-standard evaluation used here to allow for non-quoted variables to be used when the function is called.
1 |
|
Here there is a striking difference between males and females, with relatively low correlations between mortality rates of males aged 18-35 and males of other ages. This is largely driven by the wars where males of those ages die at much greater rates than other males, but only for a few years. If we start the analysis from 1950, the effect is much reduced.
1 |
|
There is still a section of low correlation around ages 18-22, with the correlations being lower for males than females. I suspect this is to do with the well-known accident bump, where young people tend to have higher mortality due to accidents and suicides than people of other ages.
Jim Ramsay pointed out in my talk that it would be nice to remove the redundancy due to symmetry and show the males in the top left triangles, with the females below. It turns that this is also very easy to do.
1 |
|
Finally, the diagonals where age1
=age2
are of particular interest, as these correspond to the ACFs of the univariate time series comprising each age group.
I will plot them in three different ways – against age, against lag, and as a 2-d image plot.
1 |
|
1 |
|
1 |
|
Melbourne pedestrian data
My second example involves pedestrian traffic near Flinders St Station in Melbourne city. The data can be downloaded using the rwalkr
package, but some data is pre-packaged in the sugrrants
package, which we will use here.
Again, the first task is to put the data into a suitable form. We will use only data from Flinders St Station Underpass in 2016, and add in holiday information to the data set.
1 |
|
1 |
|
The differences between days is clearly seen. It is also apparent that there were a handful of very unusual days.
1 |
|
For sub-daily data, a calendar plot is extremely useful for identifying them, along with other interesting features in the data. The public holidays on weekdays are clearly marked here in a different colour. Can you spot deviations from the regular pattern that are not explained by holidays?
1 |
|
For the ACF, I will look only at the “diagonal surface” — the equivalent of the univariate ACFs for each hour, plotted for different lags.
1 |
|
Here it is interesting to note that the weekly seasonality is strongest at hours 6-9am and around 4-5pm, corresponding to the peak hours for workers. There is relatively weak correlation between 10am and 3pm, when workers are mostly working.
Related