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.
library(tidyverse)
library(demography)
# Combine age groups above 100
frmort %
ggplot(aes(x = age, y = mortrate, group = year, col = year)) +
geom_line() +
facet_grid(~sex) +
scale_y_log10() +
xlab(&quot;Age&quot;) + ylab(&quot;Log mortality&quot;) +
scale_color_gradientn(colours = rainbow(10))
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.
# This requires the transformr package on CRAN and the gganimate package on github
# Run the following two lines if you don't already have them.
#install.packages(c(&quot;transformr&quot;, &quot;devtools&quot;))
#devtools::install_github(&quot;thomasp85/gganimate&quot;)
library(gganimate)
frmort %__%
filter(year __ 1900) %__%
ggplot(aes(x = age, y = mortrate, group = year, col = year)) +
geom_line() +
xlab(&quot;Age&quot;) + ylab(&quot;Log mortality&quot;) +
facet_grid(~sex) +
scale_y_log10() +
scale_color_gradientn(colours = rainbow(10)) +
transition_time(year) +
ease_aes('linear') +
shadow_mark(colour = &quot;grey70&quot;) +
labs(title = 'Year: {frame_time}')
Another way of looking at the data is using an image map. Again, this is extremly easy using ggplot2.
frmort %__%
ggplot(aes(x = year, y = age, fill = log(mortrate))) +
geom_raster() +
facet_grid(~sex) +
scale_fill_viridis_c(option = &quot;A&quot;, direction = -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.
facf %
acf(plot=FALSE, lag.max=lag.max, na.action=na.pass)
nx %
filter(lag __ 4) %__%
ggplot(aes(x = age1, y = age2, fill = acf)) +
geom_raster() +
facet_grid(sex~lag) +
scale_fill_viridis_c(option = &quot;A&quot;, direction = -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.
fracf %
filter(year __ 1950) %__%
nest(-sex) %__%
mutate(
acf = map(data, ~ facf(df=., xvar=age, yvar=mortrate, time=year))
) %__%
select(-data) %__%
unnest()
fracf %__%
filter(lag __ 4) %__%
ggplot(aes(x = age1, y = age2, fill = acf)) +
geom_raster() +
facet_grid(sex~lag) +
scale_fill_viridis_c(option = &quot;A&quot;, direction = -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.
fracf %__%
filter(
lag __ 4,
(sex==&quot;male&quot; &amp; age2 __ age1) | (sex==&quot;female&quot; &amp; age2 __ age1)
) %__%
ggplot(aes(x = age1, y = age2, fill = acf)) +
geom_raster() +
facet_grid(~lag) +
scale_fill_viridis_c(option = &quot;A&quot;, direction = -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.
fracf %__%
filter(age1==age2) %__%
ggplot(aes(x = age1, y = acf, group = lag, col = lag)) +
facet_grid(~sex) +
geom_line() +
scale_color_gradientn(colours = rainbow(10))
fracf %__%
filter(age1==age2) %__%
ggplot(aes(x = lag, y = acf, group = age1, col = age1)) +
geom_line() +
facet_grid(~sex) +
scale_color_gradientn(colours = rainbow(10))
fracf %__%
filter(age1==age2) %__%
ggplot(aes(x = lag, y = age1, fill = acf)) +
geom_raster() +
facet_grid(~sex) +
scale_fill_viridis_c(option = &quot;A&quot;, direction = -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.
library(sugrrants)
pedestrian %
filter(
Sensor_Name == &quot;Flinders Street Station Underpass&quot;,
Date %
rename_all(tolower) %__%
rename(
hour = &quot;time&quot;,
number = &quot;hourly_counts&quot;
) %__%
left_join(tsibble::holiday_aus(2016, state = &quot;VIC&quot;)) %__%
mutate(
daytype = ifelse(
day %in% c(&quot;Saturday&quot;, &quot;Sunday&quot;) | !is.na(holiday),
&quot;Holiday&quot;, &quot;Workday&quot;
)
) %__%
select(date, hour, day, daytype, month, number)
pedestrian
## # A tibble: 8,783 x 6
## date hour day daytype month number
## ## 1 2016-01-01 0 Friday Holiday January 3643
## 2 2016-01-01 1 Friday Holiday January 2009
## 3 2016-01-01 2 Friday Holiday January 3238
## 4 2016-01-01 3 Friday Holiday January 2164
## 5 2016-01-01 4 Friday Holiday January 1161
## 6 2016-01-01 5 Friday Holiday January 682
## 7 2016-01-01 6 Friday Holiday January 388
## 8 2016-01-01 7 Friday Holiday January 373
## 9 2016-01-01 8 Friday Holiday January 275
## 10 2016-01-01 9 Friday Holiday January 545
## # ... with 8,773 more rows
The differences between days is clearly seen. It is also apparent that there were a handful of very unusual days.
pedestrian %__%
ggplot(aes(x = hour, y = number, group = date)) +
geom_line() +
facet_grid(~day)
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?
p %
frame_calendar(x = hour, y = number, date = date) %__%
ggplot(aes(x = .hour, y = .number, group = date, colour = daytype)) +
geom_line() +
theme(legend.position = &quot;bottom&quot;)
prettify(p)
For the ACF, I will look only at the “diagonal surface” — the equivalent of the univariate ACFs for each hour, plotted for different lags.
pedestrian %__%
facf(xvar=hour, yvar=number, time=date, lag.max=20) %__%
filter(hour1==hour2) %__%
ggplot(aes(x = lag, y = hour1, fill = acf)) +
geom_raster() +
scale_fill_viridis_c(option = &quot;A&quot;, direction = -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.