## 2.6 Scatterplots

The graphs discussed so far are useful for visualising individual time series. It is also useful to explore relationships between time series.

Figures 2.6 and 2.7 shows two time series: half-hourly electricity demand (in Gigawatts) and temperature (in degrees Celsius), for 2014 in Victoria, Australia. The temperatures are for Melbourne, the largest city in Victoria, while the demand values are for the entire state.

vic_elec %>%
filter(year(Time) == 2014) %>%
autoplot(Demand) +
xlab("Year: 2014") + ylab(NULL) +
ggtitle("Half-hourly electricity demand: Victoria, Australia") Figure 2.6: Half hourly electricity demand in Victoria, Australia, for 2014.

vic_elec %>%
filter(year(Time) == 2014) %>%
autoplot(Temperature) +
xlab("Year: 2014") + ylab(NULL) +
ggtitle("Half-hourly temperatures: Melbourne, Australia") Figure 2.7: Half hourly temperature in Melbourne, Australia, for 2014.

We can study the relationship between demand and temperature by plotting one series against the other.

vic_elec %>%
filter(year(Time) == 2014) %>%
ggplot(aes(x = Temperature, y = Demand)) +
geom_point() +
ylab("Demand (GW)") + xlab("Temperature (Celsius)") Figure 2.8: Half-hourly electricity demand plotted against temperature for 2014 in Victoria, Australia.

This scatterplot helps us to visualise the relationship between the variables. It is clear that high demand occurs when temperatures are high due to the effect of air-conditioning. But there is also a heating effect, where demand increases for very low temperatures.

### Correlation

It is common to compute correlation coefficients to measure the strength of the relationship between two variables. The correlation between variables $$x$$ and $$y$$ is given by $r = \frac{\sum (x_{t} - \bar{x})(y_{t}-\bar{y})}{\sqrt{\sum(x_{t}-\bar{x})^2}\sqrt{\sum(y_{t}-\bar{y})^2}}.$ The value of $$r$$ always lies between $$-1$$ and 1 with negative values indicating a negative relationship and positive values indicating a positive relationship. The graphs in Figure 2.9 show examples of data sets with varying levels of correlation. Figure 2.9: Examples of data sets with different levels of correlation.

The correlation coefficient only measures the strength of the linear relationship, and can sometimes be misleading. For example, the correlation for the electricity demand and temperature data shown in Figure 2.8 is 0.28, but the non-linear relationship is stronger than that.

The plots in Figure 2.10 all have correlation coefficients of 0.82, but they have very different relationships. This shows how important it is to look at the plots of the data and not simply rely on correlation values. Figure 2.10: Each of these plots has a correlation coefficient of 0.82. Data from FJ Anscombe (1973) Graphs in statistical analysis. American Statistician, 27, 17–21.

### Scatterplot matrices

When there are several potential predictor variables, it is useful to plot each variable against each other variable. Consider the eight time series shown in Figure 2.11, showing quarterly visitor numbers across states and territories of Australia.

visitors <- tourism %>%
group_by(State) %>%
summarise(Trips = sum(Trips))
visitors %>%
ggplot(aes(x = Quarter, y = Trips)) +
geom_line() +
facet_grid(vars(State), scales = "free_y") +
ylab("Number of visitor nights each quarter (millions)") Figure 2.11: Quarterly visitor nights for the states and territories of Australia.

To see the relationships between these eight time series, we can plot each time series against the others. These plots can be arranged in a scatterplot matrix, as shown in Figure 2.12. (This plot requires the GGally package to be installed.)

visitors %>%
GGally::ggpairs(columns = 2:9) 