Small multiples, or the science and art of combining graphs

Post on 09-Jan-2016

30 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Small multiples, or the science and art of combining graphs. Nicholas J. Cox Department of Geography Durham University, UK. 1. Small multiples. Good graphics often exploit one simple design that is repeated for different parts of the data. - PowerPoint PPT Presentation

Transcript

11

Small multiples, or the science and art of combining graphs

Nicholas J. Cox Department of Geography Durham University, UK

2

Small multiples

Good graphics often exploit one simple design that is repeated for different parts of the data.

Edward Tufte called this the use of small multiples.

Well-designed small multiples are inevitably comparative, deftly multivariate, shrunken, high-

density graphics…. Edward Rolf Tufte (1942–)

3

…in Stata

In Stata, small multiples are supported for different subsets of the data with by() or over() options of many graph commands.

Users can emulate this in their own programs by writing wrapper programs that call twoway or  graph bar and its siblings.

Otherwise, specific machinery offers repetition of a design for different variables, such as the graph matrix command. 

4

Users can always put together their own composite graphs by saving individual graphs and then combining them using graph combine.

This presentation offers further modest automation of the same design repeated for different data.

55

Original programs discussed

sparkline crossplot combineplot designplot

and with cameo roles aaplotsepscatter

All may be installed from SSC.

6

What’s in a name? roseplot by any other name…A minor theme here is that definite names are

needed for programs, even if kinds of graphs do not have distinct agreed names.

As in advertising, a good name attracts and keeps users.

As in politics, a bad name can be fatal.

7

sparkline

The purpose of visualization is insight, not picturesBen Shneiderman (1947–)

88

Sparklines The name “sparkline” was suggested by Edward

Tufte for intense text-like graphics.

Sparklines are typically simple in design, sparing of space and rich in data, but they include several quite different kinds of graph otherwise.

The most common kind shows several time series stacked vertically.

sparkline is a Stata implementation.

9

Sparklines have long been standard in several fields, including physics and chemistry (spectroscopy), seismology, climatology, ecology, archaeology and physiology (notably encephalography and cardiography).

Tufte provided an memorable and evocative new name and an excellent provocative discussion.

The Grunfeld data (webuse grunfeld) are a classic dataset in panel-based economics. Ten companies were monitored for 1935–54. They give us a simple sandpit.

1010

What are we doing here? The problem of time series graphicsComparisons of time series are a rich and

challenging area of statistical graphics.

The widespread term spaghetti plot hints immediately at the difficulties.

As always, we want to combine a grasp of general patterns with access to individual details.

With this in mind, we look at some sparklines of the Grunfeld dataset.

11

257.7

1486.7

2792.2

6241.7

2.8

2226.3

invest

mvalue

kstock

1935 1940 1945 1950 1955year

company 1

1212

invest

mvalue

kstock

invest

mvalue

kstock

invest

mvalue

kstock

1935 1940 1945 1950 1955 1935 1940 1945 1950 1955

1935 1940 1945 1950 1955 1935 1940 1945 1950 1955

1 2 3 4

5 6 7 8

9 10

Graphs by company

1313

257.7

1486.72792.2

6241.72.8

2226.3

209.9

645.51362.4

2676.350.5

669.7

33.1

189.61170.6

2803.397.8

888.9

40.29

174.93410.9

1001.510.2

414.9

39.67

91.9151.2

398.4183.2

804.9

20.36

135.72197

927.36.5

238.7

23.21

89.51

210.1

98.1

100.2

511.3

12.93

90.08191.5

1193.5.8

213.5

20.89

66.11213.3

496162

468

.93

6.53

87.94

58.12

3.23

14.33

invest

mvalue

kstock

invest

mvalue

kstock

invest

mvalue

kstock

1935 1940 1945 1950 1955 1935 1940 1945 1950 1955

1935 1940 1945 1950 1955 1935 1940 1945 1950 1955

1 2 3 4

5 6 7 8

9 10

1414

Vertical and horizontal

By default sparkline stacks small graphs vertically.

If several graphs are combined, it is typical to cut down on axis labels and rely on differences in shape to convey information.

Horizontal stacking is also supported, which can be useful for archaeological or environmental problems focused on variations with depth or height.

Here is an archaeological dataset as example.

15

12

13

14

15

16

17

18

19

20

21

22

23

24

25

12

13

14

15

16

17

18

19

20

21

22

23

24

25

leve

lscores blanks tools

3.8 17.7 25.6 74.7 18.6 56.9

1616

Nightingale’s data

Florence Nightingale (1820–1910) is well remembered for her nursing in the Crimean war and (within statistical science) for use of quantitative arguments.

Her most celebrated dataset is often reproduced using her polar diagram, but is easier to think about as time series.

Zymotic (loosely, infectious) disease mortality dominates other kinds, so much so that a square root scale helps comparison. (A logarithmic scale over-transforms here.)

17

18

Source of image:

http://understandinguncertainty.org/coxcombs

1919

0

200

400

600

800

1000

18551854 1856

zymotic disease

wounds and injuriesall other causes

annualised rates per 1000

Nightingale's data on mortality in the Crimea

2020

0

200

400

600

800

1000

18551854 1856

zymotic disease

wounds and injuriesall other causes

annualised rates per 1000

Nightingale's data on mortality in the Crimea

0

25

100

225

400

625

900

18551854 1856

zymotic disease

wounds and injuriesall other causes

annualised rates per 1000

Nightingale's data on mortality in the Crimea

2121

Would sparkline help?

A sparkline display is useful to show relative shape, such as times of peaks.

We see that seasonality is only part of what is being seen.

The harsh winter of 1854–5 coincided with some of the hardest battles of the war, but 1855–6 was quite different.

But, as often happens, no one graph dominates others here.

2222

1.4

1022.8

.4

115.8

2.5

140.1

zymotic disease

wounds and injuries

all other causes

18551854 1856annualised rates per 1000

Nightingale's data on mortality in the Crimea

23

crossplot

The scatter plot is the workhorse of statistical graphics.

John McKinley Chambers (1941– )

24

crossplot

crossplot is designed as a quick-and-easy way to combine scatter plots.

The basic syntax is crossplot (yvarlist) (xvarlist) and the idea is to plot every y in yvarlist against every x in xvarlist.

The use of two varlists gives greater flexibility than does graph matrix, which produces every

possible scatter plot for a single varlist.

25

Scatter plot matrices

Scatter plot matrices are great, but they can be excessive.

Their main feature is also a limitation. p variables mean p2 plots all at once, so 10

means 100, and so forth.

(The half option just controls which plots you see. )

26

crossplot design

crossplot was developed in teaching, especially of regression, with the aim of encouraging focused comparisons.

Originally (1999) crossplot was called cpyxplot, cp meaning Cartesian product, but the name was ugly, cryptic and easily forgotten.

The syntax had to be as simple as possible.

27

crossplot examples

Versions of a response variable versus a key predictor.

A response variable versus versions of a key predictor.

Each output versus each input. Principal components versus original variables.

First, let us look at four versions of mpg versus weight in the auto dataset.

28

10

20

30

40

Mile

ag

e (

mp

g)

2,000 3,000 4,000 5,000Weight (lbs.)

34

56

7rt

_m

pg

2,000 3,000 4,000 5,000Weight (lbs.)

2.5

33

.54

ln_

mp

g

2,000 3,000 4,000 5,000Weight (lbs.)

24

68

rec_

mp

g

2,000 3,000 4,000 5,000Weight (lbs.)

29

Next we look at an audiometric dataset used as a multivariate example in the Stata manuals.

There are 8 response variables, 4 for left ears and 4 for right ears. Here we just focus on the 16 plots pairing left and right.

Another graph could be the 4 plots comparing left and right ears at the same frequency, the diagonal here.

30

-10

-50

51

01

5le

ft e

ar

at

50

0H

-10 0 10 20 30right ear at 500H

-10

-50

51

01

5le

ft e

ar

at

50

0H

-10 0 10 20right ear at 1000H

-10

-50

51

01

5le

ft e

ar

at

50

0H

-10 0 10 20 30 40right ear at 2000H

-10

-50

51

01

5le

ft e

ar

at

50

0H

-20 0 20 40 60 80right ear at 4000H

-10

01

02

0le

ft e

ar

at

10

00

H

-10 0 10 20 30right ear at 500H

-10

01

02

0le

ft e

ar

at

10

00

H-10 0 10 20

right ear at 1000H

-10

01

02

0le

ft e

ar

at

10

00

H

-10 0 10 20 30 40right ear at 2000H

-10

01

02

0le

ft e

ar

at

10

00

H

-20 0 20 40 60 80right ear at 4000H

-20

02

04

0le

ft e

ar

at

20

00

H

-10 0 10 20 30right ear at 500H

-20

02

04

0le

ft e

ar

at

20

00

H

-10 0 10 20right ear at 1000H

-20

02

04

0le

ft e

ar

at

20

00

H

-10 0 10 20 30 40right ear at 2000H

-20

02

04

0le

ft e

ar

at

20

00

H

-20 0 20 40 60 80right ear at 4000H

-20

02

04

06

08

0le

ft e

ar

at

40

00

H

-10 0 10 20 30right ear at 500H

-20

02

04

06

08

0le

ft e

ar

at

40

00

H

-10 0 10 20right ear at 1000H

-20

02

04

06

08

0le

ft e

ar

at

40

00

H

-10 0 10 20 30 40right ear at 2000H

-20

02

04

06

08

0le

ft e

ar

at

40

00

H

-20 0 20 40 60 80right ear at 4000H

31

crossplot syntax for examples crossplot (mpg rt_mpg ln_mpg rec_mpg) weight, combine(imargin(small))

crossplot (lft*) (rght*), jitter(1)

32

crossplot syntax extras

By default, crossplot is just calling twoway scatter followed by graph combine.

It follows that recast() is available to recast to twoway line or twoway connected.

crossplot has an extra sequence() option to label graphs to ease preparation of graphics for papers e.g. sequence(a b c d)

33

combineplot

The greatest value of a picture is when it forces us to notice what we never expected to see.

John Wilder Tukey (1915–2000)

34

combineplot combineplot is a generalisation of crossplot,

more flexible and inevitably more complicated in syntax.

The general problem of combining plots of similar kind reduces to a loop producing individual plots and a call to graph combine. That is bound to be a challenge to beginning users.

The idea is to avoid that by encapsulating the predictable syntax within one command.

35

combineplot examples

We will look at a series of univariate examples followed by a series of bivariate examples.

A great variety is possible, as we can loop over user-written graphics commands as well as official commands.

36

10

20

30

40

Mile

ag

e (

mp

g)

1 2 3 4 5

05

,00

01

0,0

00

15

,00

0P

rice

1 2 3 4 5

2,0

00

3,0

00

4,0

00

5,0

00

We

igh

t (l

bs.

)

1 2 3 4 5

12

34

5H

ea

dro

om

(in

.)

1 2 3 4 5

37

10

20

30

40

Mile

ag

e (

mp

g)

1 2 3 4 5Repair Record 1978

05

,00

01

0,0

00

15

,00

0P

rice

1 2 3 4 5Repair Record 1978

2,0

00

3,0

00

4,0

00

5,0

00

We

igh

t (l

bs.

)

1 2 3 4 5Repair Record 1978

1.0

2.0

3.0

4.0

5.0

He

ad

roo

m (

in.)

1 2 3 4 5Repair Record 1978

38

05

,00

01

0,0

00

15

,00

0P

rice

0 5,000 10,000 15,000Inverse Normal

10

20

30

40

Mile

ag

e (

mp

g)

10 15 20 25 30 35Inverse Normal

1,0

00

2,0

00

3,0

00

4,0

00

5,0

00

We

igh

t (l

bs.

)

1,000 2,000 3,000 4,000 5,000Inverse Normal

14

01

60

18

02

00

22

02

40

Le

ng

th (

in.)

140 160 180 200 220 240Inverse Normal

39

10

20

30

40

Mile

ag

e (

mp

g)

1 2 3 4 5

a

10

20

30

40

Mile

ag

e (

mp

g)

Domestic Foreign

b0

5,0

00

10

,00

01

5,0

00

Pri

ce

1 2 3 4 5

c

05

,00

01

0,0

00

15

,00

0P

rice

Domestic Foreign

d

40

05

,00

01

0,0

001

5,0

00

Pri

ce (

US

D)

10 20 30 40Mileage (mpg)

Domestic Foreign

05

,00

01

0,0

001

5,0

00

Pri

ce (

US

D)

2,000 3,000 4,000 5,000Weight (lbs.)

Domestic Foreign

05

,00

01

0,0

001

5,0

00

Pri

ce (

US

D)

140 160 180 200 220 240Length (in.)

Domestic Foreign

05

,00

01

0,0

001

5,0

00

Pri

ce (

US

D)

100 200 300 400 500Displacement (cu. in.)

Domestic Foreign

41

A digression on sepscatter

The last example used sepscatter, a program automating separation of data points on a scatter plot by a categorical variable.

The repetition of the legend needs some kind of fix. In this and similar examples, the legend could be deleted and explaining symbols left as a task for the text caption.

42

sepscatter and scatter plot matricescombineplot with sepscatter meets a felt need,

scatter plot matrices with categorisation of data points.

Here is an example with “size” variables from the auto dataset. The diagonal scatter plots have meaning, yet are not conventional. But not every graph need be immediately publishable.

43

2,00

03,

000

4,00

05,

000

Wei

ght (

lbs.

)

2,000 3,000 4,000 5,000Weight (lbs.)

2,00

03,

000

4,00

05,

000

Wei

ght (

lbs.

)

140 160 180 200 220 240Length (in.)

2,00

03,

000

4,00

05,

000

Wei

ght (

lbs.

)

100 200 300 400 500Displacement (cu. in.)

140

160

180

200

220

240

Leng

th (

in.)

2,000 3,000 4,000 5,000Weight (lbs.)

140

160

180

200

220

240

Leng

th (

in.)

140 160 180 200 220 240Length (in.)

140

160

180

200

220

240

Leng

th (

in.)

100 200 300 400 500Displacement (cu. in.)

100

200

300

400

500

Dis

plac

emen

t (cu

. in.

)

2,000 3,000 4,000 5,000Weight (lbs.)

100

200

300

400

500

Dis

plac

emen

t (cu

. in.

)

140 160 180 200 220 240Length (in.)

100

200

300

400

500

Dis

plac

emen

t (cu

. in.

)100 200 300 400 500

Displacement (cu. in.)

44

050

0010

000

1500

0P

rice

(U

SD

)

10 20 30 40Mileage (mpg)

n = 74 RMSE = 2,623.7

price = 11253 - 238.89 mpg R2 = 22.0%

050

0010

000

1500

0P

rice

(U

SD

)

2000 3000 4000 5000Weight (lbs.)

n = 74 RMSE = 2,502.3

price = -6.7074 + 2.0441 weight R2 = 29.0%

050

0010

000

1500

0P

rice

(U

SD

)

140 160 180 200 220 240Length (in.)

n = 74 RMSE = 2,678.7

price = -4584.9 + 57.202 length R2 = 18.6%

050

0010

000

1500

0P

rice

(U

SD

)

100 200 300 400 500Displacement (cu. in.)

n = 74 RMSE = 2,580.6

price = 3029 + 15.896 displace~t R2 = 24.5%

45

A digression on aaplot

The last example used aaplot.

aaplot customises automatic annotation of scatter plots with fitted regressions with text for key results.

Originally, it was written following a request by my Ph.D. student Alona Armstrong.

46

Back to combineplot

Some examples of its syntax will make clearer how it works. First look at a univariate example:

combineplot mpg price weight headroom: graph box @y, over(rep78)

Here we have one varlist and the syntax @y is a placeholder for the variable name.

47

Next look at a bivariate example:

combineplot price (mpg weight length displacement): sepscatter @y @x, ytitle("Price (USD)") sep(foreign)

Here we have two varlists and the syntax elements @y and @x are placeholders for the variable names.

48

The two varlists may each contain a single variable and they may be identical.

When both are presented, the combination is the Cartesian product of the varlists.

Naturally, you can reach through to control the options of graph combine as well as those of the particular graph command used.

49

Quirk or quick?

The quirky syntax of combineplot might cause some queasiness.

Some might recall the obsolete for command.

Confident users would (should) be happy to write their own loops, topped by graph combine, and that is fine too.

The justification for combineplot is just convenience: it can be quicker than writing your own script.

50

designplot

Real life is both complicated and short, and we make no mockery of honest adhockery.

Irving John Good (1916–2009)

51

designplot

Here more than anywhere arbitrariness of names can bite.

If you have used S or S-Plus or R much, you may have come across “design plots”.

But as implemented there they do not look much like the graphs you are going to see. Nor are they plots showing fitted results; nor do they imply experimental design.

To understand designplot, we need to creep up on it step by step.

52

0 10 20 30mean

 

 

5

4

3

2

1

(all)

Mileage (mpg)

53

0 10 20 30mean

 

 

 

 

5 Foreign5 Domestic

4 Foreign4 Domestic

3 Foreign3 Domestic2 Domestic1 Domestic

ForeignDomestic

54321

(all)

Mileage (mpg)

54

designplot syntax

Minimal syntax specifies a response first, then one or more predictors.

The predictors should in practice be categorical, meaning taking on only a small or moderate number of distinct levels (“factors”, if you like).

The examples were designplot mpg rep78 designplot mpg rep78 foreign

55

designplot defaultThe statistics shown are means.

Given one, two, … predictors, the means are shown for all the data, each one-way breakdown, each two-way breakdown, ….

designplot uses a syntax of way being 0, 1, 2, …

graph dot is the default vehicle. statsby underpins calculations.

In essence, we can get a multiscale breakdown. In practice, we might want to restrict what is

shown.

56

0 10 20 30mean

 

 

 

5

4

3

2

1

Foreign

Domestic

(all)

Mileage (mpg)

57

Restricting designplot

Here we restricted the scope by

designplot mpg foreign rep78, maxway(1)

Let us look at a different dataset. The response variable for these data on the Titanic is a binary variable survived, so its mean is the fraction survived.

We restrict using maxway(2).

580 .2 .4 .6 .8 1

mean

 

 

 

 

 

 

 

adult maleadult female

child malechild female

third malethird female

second malesecond female

first malefirst femalecrew male

crew female

third adultthird child

second adultsecond child

first adultfirst child

crew adult

malefemale

adultchild

thirdsecond

firstcrew

(all)

survived

59

So we have here:

the overall mean

one-way breakdowns for three predictors class, adult, male

two-way breakdowns for combinations class×adult, class×male,

adult×male

60

This kind of graph is for detailed scrutiny, rather than delivering shock.

Logically similar displays are often used for reporting opinion poll or electoral results.

61

That reminds us of…

The structure echoes analysis of variance, used descriptively.

Similar ideas appear in ANOVA and other literature going back to J.W. Tukey in 1977.

It also echoes the little used official command grmeanby.

By default, grmeanby also shows means. (Medians are allowed.) It allows one-way breakdowns only.

62

female

male

child

adult

second

third

first

crew

.2.3

.4.5

.6.7

class adult male

Means of survived

63

Foreign

Domestic

1

3

4

2

5

18

20

22

24

26

28

rep78 foreign

Means of mpg, Mileage (mpg)

64

grmeanby

In these examples, grmeanby shows different means distinctly, but that is not guaranteed.

Using graph dot as a default within designplot ensures more readability, although that too has its limits.

65

designplot can show other statisticsYou can show any summarize result.

In practice, you would only want to plot results sharing the same units of measurement (including none at all, as with skewness and kurtosis).

66

0 10 20 30 40

 

 

 

5

4

3

2

1

Foreign

Domestic

(all)

Mileage (mpg)

min p25 median mean p75 max

67

More to say…

Although based on graph dot by default, designplot can be recast to graph bar or graph hbar.

Although based on summarizing single variables, what could be simpler than putting different designplots side-by-side?

68

0 10 20 30mean

 

 

 

missing54321

ForeignDomestic

(all)

Mileage (mpg)

0 2,000 4,000 6,000mean

 

 

 

missing54321

ForeignDomestic

(all)

Price

0 1,000 2,000 3,000 4,000mean

 

 

 

missing54321

ForeignDomestic

(all)

Weight (lbs.)

511

1830

82

2252

74

0 20 40 60 80count

 

 

 

missing54321

ForeignDomestic

(all)

counts

69

Is this just a reinvention of graph dot? No.

graph dot and its siblings are restricted in offering only one-way or two-way or three-way breakdowns given, respectively, one or two or three “factors”.

designplot gives scope for saving results for separate graphing or tabulation.

7070

The main players againsparkline crossplot combineplot designplotAll may be installed from SSC.

Our attraction to images as a source of understanding

is both primal and pervasive.Stephen Jay Gould (1941–2002)

top related