# Visualizing two scalar variables over time

What would be the best way to display changes in two scalar variables (x,y) over time (z), in one visualization?

One idea that I had was to plot x and y both on the vertical axis, with z as the horizontal.

Note: I'll be using R and likely ggplot2

• You mean like any one of the numerous ggplot2 example charts such as this or did you have something more specific in mind? Commented Aug 5, 2010 at 21:46
• For what it is worth, I like plotting with xts and zoo objects where different series can be merged easily. Commented Aug 5, 2010 at 21:52

The other idea is to plot one series as x and the second as y -- the time dependency will be hidden, but this plots shows correlations pretty well. (Yet time can be shown to some extent by connecting points chronologically; if the series are quite short and continuous it should be readable.)

• Here's an example of your suggestion from the NYTimes nytimes.com/imagepages/2010/05/02/business/02metrics.html Commented Aug 6, 2010 at 0:57
• That's an interesting visualization. JoFrhwld, thanks for the example. Commented Aug 6, 2010 at 2:14

I sometimes make the x-axis time and plot both scalar variables on the y-axis. When the two scalar variables are on a different metric, I rescale one or both of the scalar variables so they can be displayed on the same plot. I use things like colour and shape to discriminate the two scalar variables. I've often used xyplot from lattice for this purpose.

Here's an example:

require(lattice)
xyplot(dv1 + dv2  ~ iv, data = x, col = c("black", "red"))

A method that can be very effective--one I have found extremely useful--is to sort the data by time and draw a connected X,Y scatterplot. (That is, successive points are connected by line segments or a spline.) This much is straightforward in almost any statistical plotting package. If the result is too confusing, add graphical indications of directionality. Depending on the density of the points and their pattern of temporal evolution, options include using arrows on the line segments or otherwise applying a graduated color or thickness to the segments to indicate their times. You can even dispense with the connecting lines and just color or size the points to indicate time: that works better when there are many points on the plot. In addition to displaying the bivariate relationship among the data in a conventional form, this method supplies a clear visual indication of temporally local correlations, changes that run counter to the prevailing correlation, etc.

An example of this appears in my reply at Forecasting time series based on a behavior of other one :

Because the points follow clear paths through this plot, I dropped the connecting lines in favor of letting hue represent time. This plot reveals far more detail about corresponding behaviors than the two original graphs do. The time dependency is qualitatively clear from the changes in hue, but a legend (matching colors to times) would help the reader.

• Offhand, do you know of anyways in which you would still be able to effectively distinguish time in a similar plot even if the density of points was high (such that overplotting would effectively render the scatter difficult if not impossible to interpret?) Small multiples is an obvious option, but I was wondering if there is anyway else. Commented Aug 18, 2011 at 21:14
• @Andy This plot actually started out as a 3D scatterplot with time as elevation: you're looking down from the top. Spinning or manipulating such a plot clearly reveals the pattern. In other cases I have resorted to dynamically slicing such plots over narrower time ranges: you can watch the temporal evolution as the time window moves. That sounds similar to your small multiple idea.
– whuber
Commented Aug 19, 2011 at 1:43