stat 545a class meeting #7 monday, september 30, 2013jenny/stat545a/2013-lectures… ·  ·...

48
STAT 545A Class meeting #7 Monday, September 30, 2013 Dr. Jennifer (Jenny) Bryan Department of Statistics and Michael Smith Laboratories

Upload: vuongtuyen

Post on 07-Mar-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 2: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

xyplot(y ~ x, data, ...)

See the full xyplot() tour here:http://www.stat.ubc.ca/~jenny/STAT545A/block09_xyplotLattice.html

Page 3: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

xyplot(y ~ x, data, subset = ?, ...)

Page 4: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

easy to add a regression line, a loess smooth, connect the dots, etc. via type

, type = ....,

Page 5: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

• The type argument is incredibly useful. Use it!

• It is a character vector consisting of one or more of <read the help file for the values>. If type has more than one element, an attempt is made to combine the effect of each of the components.

From Ch. 5 in Sarkar (2008).

deprecated

Page 7: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

, group = ....,

, auto.key = TRUE,

Page 8: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

xyplot(y ~ x | z, ...)

Page 9: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

combatting overplotting when you are blessed with lots of data

Page 10: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

gdpPercap

lifeExp

30

40

50

60

70

80

10^2.5 10^3.0 10^3.5 10^4.0 10^4.5 10^5.0

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●● ●●

●● ●

●●

●●

●● ●

● ●

● ●

●●

●●

●●

●●

●● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●

● ●

● ●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●●

●●

●●●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

gdpPercap

lifeExp

30

40

50

60

70

80

10^2.5 10^3.0 10^3.5 10^4.0 10^4.5 10^5.0

Counts

1

2

3

4

5

6

7

8

8

9

10

11

12

13

14

15

16

xyplot(lifeExp ~ gdpPercap, gDatOrdered, scales = list(x = list(log = 10)))

library(hexbin)

hexbinplot(lifeExp ~ gdpPercap, gDatOrdered, scales = list(x = list(log = 10)), xbins = 40)

For high volume scatterplots, consider hexagonal binning.

Page 11: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

“Hexagonal binning”Superior to scatterplot, when number of points creates overplotting problem.

Idea: tile the plane with hexagons, shade according to relative frequency of points.

Page 12: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

hexbinplot(lifeExp ~ gdpPercap | continent, gDatOrdered, subset = continent != "Oceania", scales = list(x = list(log = 10)), xbins = 40)

gdpPercap

lifeExp

30

40

50

60

70

80

10^2.510^3.010^3.510^4.010^4.510^5.0

Africa Americas

Asia

10^2.510^3.010^3.510^4.010^4.510^5.0

30

40

50

60

70

80

Europe Counts

1

1

2

2

3

3

4

4

4

5

5

6

6

7

7

8

8

Page 13: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

L in condition conc0.0

Test

sta

t of a

n in

tera

ctio

n te

rm fo

r del

etio

n an

d ol

igom

ycin

−15

−10

−5

0

5

10

15

40 50 60 70 80

testStat.Gene.olMy05.cat

40 50 60 70 80

testStat.Gene.olMy10.cat

40 50 60 70 80

testStat.Gene.olMy15.cat

40 50 60 70 80

testStat.Gene.olMy20.cat

40 50 60 70 80

testStat.Gene.olMy25.cat

40 50 60 70 80

testStat.Gene.olMy.quant Counts

134679

1012141517182021232426

β18h

β72h

1:2 1:1 2:1

1:2

1:1

2:1

Page 14: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Relevant R functions:

hexbinplot() and hexplom() from hexbin package

smoothScatter() -- also works well with pairs()

try running this:

example(smoothScatter)

library(hexbin)example(hexbin)

Page 15: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

● ●

●●●●

●●●

● ●

●●

●● ●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●●

●●

●●

●● ●

●●

●●

●●

●●●

● ●

● ●

●●●

●●

● ●●

●●

●●

●●●

●●

● ●

●●

● ●

●●

●●

●●●

●●

● ●

●●●

●●

●●

● ●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●●

●●

●●

●●●

●●

●●

●●●

●●

●●

● ●

●●●

●●

●●

●●

●●

●●

●●

●● ●●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●

● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●●●

●● ●

● ●

●●

●●

● ●

●●

●●

●●

●● ●

●●

● ●

●●

●●

●●

● ●●

● ●● ●

●●

●●●

●●

●●

● ●

●● ●

●●

●●

● ●●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●●

●● ●

●●

● ●

●●

● ●

● ●●

● ●●

● ●●

●●● ●

●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●● ●●

●●

●●

●●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

● ●

●●●

●●

●●

●●●

● ●●●

●● ●

● ●

●●

● ●

●●

●●

● ●●

●●●

●●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●● ●

●● ●

●●●

●●

● ●

● ● ●

●● ●●●

●●

●●

● ●

●●

●● ●

●●

● ●●●

●●

●●

●●

●●

●●●

●●

●●

● ●

● ●●

●●●

●●

●●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

● ●

●●

●●

●●●

●●●

●●

●●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●● ●

●●

●●

● ●

● ●

● ●●

●●●●

●●

●●●

●●

● ●

●●

●●

●●●

●●●●●

● ● ●

●● ●

● ●

● ●●

●●

●●

● ●

●●

●●●● ●

● ●

● ●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●●

●●

●● ●

●●

●●

●●●

●●

●●

●●

● ●●

● ●

●●● ●

●●

●●●

●●

●●

●●

●●●

●●

● ●

●●

●●●

●●●

●●

●●

● ●

●●●

●●●●

●●

●●

●●

●●●

● ●●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

● ●●

●●

●●

● ●●

●●

●●

●●

●●

●● ●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●● ●

●●●

● ●

●● ● ●●●

●●

● ●●

●●

●●

●●

● ●●

●●

●●

●●

●●●●

●●

●●●

●●

●●

●●

●●●

●●

●●●

●●

●● ●●

●●●

●● ●

●●

● ●●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

● ●●●

●●

● ●●

●●

●●

●●●●●

●●

●●

● ●●

●●

●● ●

●●

●●

● ●

●●

●●

●●

● ●●

● ●

●●

●●

●●●●

●●

● ●●

●●●

●●●

● ●● ●

●●

●●●

●●

●●

●●● ●

●●

●●

●●

●●●

●●

● ●

●●

●●

●●●●

●● ●

● ●

● ●

●● ●

●●●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●● ●

●●●

●●●

●●

●●

●● ●

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●●●

●●●

●●●

●●

●●●

●●

●● ●

●●

●●

●●●

●●

●●

● ●●

●●

●●

●●

●● ●

●●●

●●

●●●

● ●●

●●

● ●●●

●●

● ●●

●●

● ●●●

●●

●●●

●●

●●

●●

●●

● ●

● ●●

●●

●● ●

●●

●●

● ●●

●●

●●

●●●●

●●

● ●

●●●

●● ●

●●

●●●●

●●

●●

●●

●●

●● ●

●●

●●

●●● ●

● ●●

●●

●●●

●●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●

●●

●●

●● ●●

●●

● ●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●●

●●

●●●

●●

●●

●●

●●●

● ●●

●●

●●

●●●

●●

●●

●●

● ●

● ●●

●●

●●

●● ●

●●

●●

●●

●●

●●●

●●

● ●●

●●

● ●

●●

●●

●●●

●●

●●

● ●

●●

● ●

●●

●●

●●

● ●

●●

●●

●● ●

●●

●●

●●

●● ●●

● ●

●●

●●

●●

●●●● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●● ●●

●●

● ●●

●●

●●

●●

●●

●● ●

●●●

●● ●

●●

● ●

●●

●●

●●● ●●

●●

●●

● ●

●●●

●●

●●●●●

● ●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

● ●●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

●●

●●

●● ●

● ●●

●●

● ●●

●●

●●●

●●●

●●

●●●

●● ●

●●● ●

●●

● ●

●●

● ●●

●●●

●●

● ●●

● ●●●

● ●●

●●

● ●

●●

●●●●●

●●●

●●

● ●

●●

●●

●●●

●●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

● ●●

●●

●●

● ●●

●●

●●●

●●

●●●

●●

●●

●● ●

●●

●●

●● ●

●●

●●

●●

●●

●●●

●●●●

●●

● ●

●●

●● ●

● ●

●●

● ●

●●

●●

● ●

●●

●●

●●

●●

●● ●

● ●

●●

●●

●● ●

●●

●●

●●● ●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●●

● ●

●●● ● ●

●●●

●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●●

● ● ●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●●

● ●

●●

● ●

●● ● ●

●●

● ●●

● ●

● ●

●●●

● ●●

● ●

●●

●●●

●● ●●●

●●

● ●

●●

●●

● ●●

●●

●●

● ●

● ●●

●●● ●●● ●●

● ●

●● ●

● ●●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●●

●●●

●● ●

●●

●●

●●

● ●●

●●

●●

●●

●●

●● ●

●●

●●

●● ●

●●

● ●●

●●●

●●

●●●●

●●

●●

●●

● ●●

●●●

●●

●●

● ●●

●●

●●

●●

● ●

●●

●● ●

●●

●● ●●

●●

●●

●●

●●●

●●

●●

● ●●

●●

● ●

● ●

●●

●●

●●

● ●

●●●

●●●

●●●

●●

●●

●●

● ●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●●●

●● ●

●●

●●

●●

● ● ●

●●●

●●

●●

●●●

● ●●

● ●

●●

●●● ●

●●●

●● ●

●●

● ●●●●

●●

●● ●

●● ●

● ●●

●● ●

●●

●●

●●

●●

● ●

●●

●● ●

●● ●

●●

●●

●●

●●● ●

●●

●●

●●●

●●

● ●

●● ●●●

●●

●●●

●●

● ●● ●

●●●

●● ●

● ●

●● ●●

●●

●●●

●●

●●

●●

●●●

●●●

●●●

●● ●

●●●● ●●●●

●●●

●●

●●

●●

●●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●● ●

● ●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

● ●

●●●●●

●●

●●

●●

●●

●●●

●●●

●●

●● ●

●●

●● ●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●● ●

● ●

●●

● ●

●●

●●

●●

● ●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●● ●

● ●

●●

● ●

●● ●

●●

● ●●

●●

● ●

●●

●●

●●

●● ●

●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

● ●

●●

●● ●

● ●

● ●

●●

●●

● ●

●●

●● ●

●●

●●

●● ●

● ●

●●

●●●

●●

●●

●●

● ●●

● ●●●

●●

●●

● ●

● ●●

●●

●●

● ●

●●

●●

●●●

●●

●●

● ●●

● ●●●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

● ●

●●

●●

●●

● ●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●● ●

●●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●

● ● ●

●●

●●

●●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

● ●

●●

●●●●

● ●

●●

●● ●

● ●

●●

●●●

●●

●●

● ● ●

●●

●●

● ●

● ●

●●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●●

●● ●

●●

● ● ●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●

● ●● ●

●●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●● ●

●●

●●

●●

●● ●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●● ●

●●● ●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●

● ●●

●●

●●

●● ●●

●●

●●

●● ●

● ●●

●●

●●

●●

●●

● ●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●●●

●●

● ●

●●

●●

●●

●● ●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●●

● ●

● ●●

●●

●●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

● ●●

●●

●●●

●●

●●

●●

● ●

● ●

● ●

●●

●●●

● ●

● ●●

●●

●●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●● ●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●● ●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●●

●●

●●

●●

●●●

●●

● ● ●

●●●●

●●

●●

●●

●●

●●

●●

●●●

●●

● ●

●●

●●

●● ●

●●●

●●

● ●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●

● ●●

●● ●

●●

●●●

●●●●

●●●

●●

● ●

●●

●●

●●

●●

●●●

●●

●● ●

● ●

●●

●●

●● ●●

● ●

●●

●●●

●●

●●

● ●

●●

●●

●● ●

● ●

●●

●●

●●

●●●

●●

●●

●● ●

●●

● ●

●●●

●●

●●

● ●

●●

● ●●

●●

●●●

● ●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

● ●●

●●

●●

● ●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●● ●

●●

●●

●●

●●●

●●

●●

●●

●● ●●

●●

●●

●●

●●

●●

●● ●●

● ●

●●●

●●

●●

● ●●

●●

●●

●●●

●●

●●●

●●

●●●●●

●●●

●●

●●

●● ●

●●

●●

●●

●●

● ●

●●

● ●●

●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

● ●●

● ●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●●

● ●

●●

●●

● ●

● ● ●●

●●

●●

●●

● ●●

●●

●●

●●

●● ●

●●

●●

● ●●

● ●

●●● ●

● ●●

●●

●●●

●●

● ●

●●

●●

● ● ●

● ●●●

●●

●●●

●●

●●

●●●

● ●

●●●

●●

●●

●●

●●

●●●

● ●

●●

●●

●●

● ●●

●●

●●

●●

● ●

●●

● ●●

●●●

●●

●●●

● ●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●● ●

●●

−2 0 2 4 6 8

−4−2

02

46

8

x[,1]

x[,2]

−2 0 2 4 6 8

−4−2

02

46

8

x[,1]

x[,2]

−2 0 2 4 6 8

−4−2

02

46

8

x[,1]

x[,2]

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

● ●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●●

●●

● ●

●●

● ●●

●●

●●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●● ●

● ●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

● ●

●●

●●

●●

●●●

●●

●●

●●●

●●●

●●

●●

●●

● ●

●●

●●

●●●

● ●

●●

●●

●●

● ●●

● ●● ●

●●●

●●

● ●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

● ●

● ●●

●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●

●●

● ●●●

● ●

●●

●●

● ●

●●

● ●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●● ●

●● ●

●●

●●

●● ●

● ●●

●●

●●

● ●

●●

● ●

●●

● ●●

●●

●●

●●

●●

●●●

●●

● ●

● ●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

● ●

● ●

●●●

●●

● ●

●●

● ●

●●

●●

●●

●●●●

● ● ●

●● ●

● ●

●●

●●

●●

●●

●● ●

●●

●●

●●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

● ●

●●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●● ●

●●

●●

●●

●●

● ●●

●●

●●

● ●

●●

●●

●●

● ●●

●●

●●

● ●

●●

●●

●● ●

●●

●●●

●●

●●

● ●

●●

●●●

●●

●●

● ● ●

●●

● ●

●● ● ●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●● ●

●●●

●● ●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●● ●

●●

● ●●●

● ●

●●

●●●

●●

●●

●●

● ●

●●

●● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●●

●●

●●

●●●

●●

● ●

●●

●●●

●● ●

● ●

● ●

●● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●● ●

●●

●●

●●

● ●

●●

●●●

●●

● ●●

● ●●

●●

●●

●●

●●

● ●

● ●●

●●

●● ●

●●

●●

●●

●●

●●●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

● ●

● ●

●●

●●●

●●●

●●

●●

● ●

●●

●●

●●

●●

● ●●

●●●

●● ●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●● ●

●●

●●

●● ●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●●

●●

●●

●●

●●

●●● ●●

●●

●●

● ●

●●

●●

●●●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●● ●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●● ●

●●

● ●●

●●

●●

●●

●●

●●

● ●

●●●

●●

●●

●●

● ●●

●●

●●

● ●

● ●

●●

● ●

●●

● ●

●●

●●●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●●

●●

●●

●●

● ●

●●

● ●

● ●

●●

●●

● ●

●●

●●●

●●

●● ●

● ●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●

●●

● ●●

● ●

●●

●●

●●

●● ●

● ●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●● ●

● ●●

● ●

●●

●●

● ●

● ●

●●

●●●

● ●● ●

●●

● ●

●●

● ●●

●●

●●

●●

● ●●

●● ●

●●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

● ●●

● ●

●●

●●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●●

●●

● ●●

●●

●●

●●

● ●

●●

●●●

●●

●● ●●

●●

●●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●●

● ●

●●

●●

●●

● ● ●

●●

●●●

● ●●

●●

●●

●● ●

●●●

● ●●

●●

●●●

●●

● ●

●●

● ●●

●●

●●

●●

●● ●

●●

● ●

●●

●●

●●

●● ●

●●

●●

●●

●●

●● ●●

●●

●●●

●●

● ●● ●

●● ●

● ●

●●●

●●

● ●●

●●

●● ●

●●

●●●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●● ●●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●● ●●

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●● ●

● ●

●●

●●

● ●

●●

● ●

●●

●●

●● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●●

●●

●●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

● ●

●●●

●●

● ● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●● ●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●● ●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●●●

●●

●●

●●

● ●

●●

●●

●●

●● ●

●●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

● ●

●●

●●

●● ●●

● ●

●●

● ●

●●

●●

● ●

●●

● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

●●

● ●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

● ●

● ●

● ●

●●

●●

●●

● ●

● ●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●●

●●

● ●

●●●

●●

●●

●●

●●

●●

●●

● ●●

● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

● ●

●●

●●

●● ●●

● ●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●

●●

●●

●● ●

● ●

●●●

●●

●●

● ●

● ●●

●●

●●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

● ●●

●●●

●●

●●

●●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●● ●●

●●

●●●

●●

● ●●

●●

●●

●●●

●●

●●●

●●

●●●

●●

●●●

●●

● ●

●●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ● ●●

●●

● ●●

●●

●●

●●

●●

●●

● ●

●●

●● ●

● ●●

●●●

● ●

●●

● ● ●

●●

●●

●●

●●●

●●

●●

●●●

● ●

●●

●●

●●

●●

●●

●●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●●

●●

−2 0 2 4 6 8

−4−2

02

46

8

x[,1]

x[,2]

plot(x) smoothScatter(x)

Lab.palette <- colorRampPalette(c("blue", "orange", "red"), space = "Lab")smoothScatter(x, colramp = Lab.palette)

plot(x, col = densCols(x), pch=20)

FAIL!

Page 16: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Exp 18, L: comparing effect estimates

e18.l.ee.olslp0

100

0 100

−200

−100

−200 −100

e18.l.ee.ol25−20

0

20−20 0 20

−60

−40

−20

−60 −40 −20

e18.l.ee.ol200

20

40 0 20 40

−40

−20

0

−40 −20 0

Hexagonal binning, again, but in context of a scatterplot matrix. Read about pairs(), splom() and hexplom().

Page 17: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

var 1

−15 −5 0 5 10 −15 −5 0 5 10

−10

−50

510

15

−15

−50

510

var 2

var 3

−10

−50

510

15

−10 −5 0 5 10 15

−15

−50

510

−10 −5 0 5 10 15

var 4

pairs(y, panel = function(...) smoothScatter(..., nrpoints=0, add=TRUE))

Page 18: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

three different ways of making graphics in R these days

* Base or traditional R graphics * `lattice` add-on package * `ggplot2` add-on package

The CRAN Task View: Graphic Displays & Dynamic Graphics & Graphic Devices & Visualization provides a survey of the whole R graphics landscape.

Page 19: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

base graphics <<< latticebase graphics <<< ggplot2

more on this topic can be found here

Page 20: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Assignment 1: Best Set of Graphs

2000 6000 10000 14000

4055

70

Year of 1950

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 15000

5065

Year of 1955

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)0 5000 10000 15000

3050

70

Year of 1960

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 15000 2000055

65

Year of 1965

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 20000

6470

Year of 1970

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 20000

6470

Year of 1975

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 15000 25000

6672

Year of 1980

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

10000 15000 20000 25000 30000

7076

Year of 1985

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

An excellent demonstration of how traditional graphics fails at such tasks.

hello whitespace!

non-common axes

repeated elements, e.g. axis labels

misallocation of space, too much spent on annotation, not enough on data

Page 21: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Assignment 1: Best Set of Graphs

2000 6000 10000 14000

4055

70

Year of 1950

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 15000

5065

Year of 1955

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 15000

3050

70

Year of 1960

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 15000 20000

5565

Year of 1965

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 20000

6470

Year of 1970

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 10000 20000

6470

Year of 1975

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

0 5000 15000 25000

6672

Year of 1980

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

10000 15000 20000 25000 30000

7076

Year of 1985

Income per PersonLife

Exp

ecta

ncy

at B

irth

(yrs

)

lattice

base

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

304050607080

10^2.5 10^3.5 10^4.5

●●

●●

●●●

● ●●●

●●

●●

●●

●●

●●

1962

Afric

a

●●

●●

● ●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

1977

Afric

a

10^2.5 10^3.5 10^4.5

●●

●●●

●●●

●●●

● ●

●● ●

●●

● ●

●●

●●

1992

Afric

a

●● ●

●●●

● ●

●●●●

●●

●●

●●

2007

Afric

a

●●●

●●

●●

● ●

●●

●●

●●

●●

1962

Amer

icas ●●

●●

●●

●●

●●● ●

1977

Amer

icas ●●● ●

●●

●●

●●●

●●

●●●●

1992

Amer

icas

304050607080

●●● ●●

● ●●

●●

●●

●●●●

● ●●●

●●

2007

Amer

icas

304050607080

●●●

●●●

●●

●●

●●

●●

●● ●

1962

Asia

●●●

●●●

●●

●●

●●

●●

● ●

1977

Asia ●

●●● ● ●●

●●

● ●

●●

●●

1992

Asia ●●

●●● ●●

●●

●●

●●

●●

●●

2007

Asia

●●●●●●

●●

●●

●●

●●

●●

●●

1962Eu

rope

10^2.5 10^3.5 10^4.5

●●●●

●●●

●●●

●●

●● ●

●●

●●

●●

●●

●●

1977

Euro

pe

●●

●●●●●●

●●

●●●

●●

●●●

●●

1992

Euro

pe

10^2.5 10^3.5 10^4.5

304050607080●

●●●●●●●

●●● ●●

● ●

●●●

● ●

● ●●

●●

2007

Euro

pe

Page 22: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

time invested

quality of output

* figure is totally fabricated but, I claim, still true

base

lattice (and ggplot2)

week one ....

Page 23: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

time invested

quality of output

* figure is totally fabricated but, I claim, still true

base

lattice (and ggplot2)

week two and thereafter

Page 24: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

2007200 400 1000 2000 4000 10000 20000 40000

20

25

30

35

40

45

50

55

60

65

70

75

80

85

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

● ●

lattice

traditional

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

30

40

50

60

70

80

500 1000 5000 10000 50000

2007●

● ●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

● ●

AfricaAmericasAsiaEuropeOceania

you need to focus on the other 99%

get better results with far less time by using lattice or ggplot2

you should be making TONS of figures

about 1% of them are very fancy and highly specific

these will take lots of timeperiod.

Page 25: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

high level lattice function return objects of class “trellis”

Get more technical info on lattice here:http://www.stat.ubc.ca/~jenny/STAT545A/block10_latticeNittyGritty.html

Page 26: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

you can make an assignment to store the return value from, e.g. xyplot()

in that case, the trellis `print()` method does not get called and no figure appears on your screen

call print(myFig) explicitly to see it

you will need to this, e.g., if making a figure inside a function, inside a loop, etc.

plot() is almost equivalent to print()

Page 27: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

storage + explicit print() call allows you to access optional arguments

main application is putting 2 or more lattice plots on one “page”

there is also an update() method which I rarely use

Page 28: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

reshaping

it’s really important

I assume you’re beginning to understand that from personal experience

tall or long data generally best for plotting and modelling

short or wide data often best for generating human-facing tables

Page 29: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

in lattice, some reshaping -- specifically from wide to tall -- can be avoided via the “extended formula interface”

Page 30: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

high level lattice functionspanel functions

Page 31: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

1.4 Looking ahead 9

Function Default Displayhistogram() Histogramdensityplot() Kernel Density Plotqqmath() Theoretical Quantile Plotqq() Two-sample Quantile Plotstripplot() Stripchart (Comparative 1-D Scatter Plots)bwplot() Comparative Box-and-Whisker Plotsdotplot() Cleveland Dot Plotbarchart() Bar Plotxyplot() Scatter Plotsplom() Scatter-Plot Matrixcontourplot() Contour Plot of Surfaceslevelplot() False Color Level Plot of Surfaceswireframe() Three-dimensional Perspective Plot of Surfacescloud() Three-dimensional Scatter Plotparallel() Parallel Coordinates Plot

Table 1.1. High-level functions in the lattice package and their default displays.

intended to produce a particular type of graphic by default. The full list ofhigh-level functions in lattice is given in Table 1.1. Chapters 3 through 6 focuson the capabilities of these high-level functions, describing each one in turn.The functions have much in common: they each have a formula interface thatsupports multipanel conditioning in a consistent manner, and respond to anumber of common arguments. These common features, including the basicsof multipanel conditioning, are described briefly in Chapter 2, and in furtherdetail in Chapters 7 through 12. lattice is designed to be easily extensible usingpanel functions; some nontrivial examples are given in Chapter 13. Extensionscan also be implemented as new high-level functions; Chapter 14 gives someexamples and provides pointers for those who wish to create their own.

Page 32: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

how to customize a lattice figure:

- use those arguments! READ THE HELP FILE. I know they’re long and boring . Sorry. - tip: read help on the default panel function (I’ll elaborate)

- change graphical parameters, such as the vector of colors used by a grouping factor (I’ll elaborate)

- customize the panel function

Page 34: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Things to know about panel functions:

Panel functions are functions (!)

They are responsible for graphical content inside panels

They get executed once for every panel

Every high level function has a default panel function

e.g., xyplot() has default panel function panel.xyplot()

Page 35: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Workflow for rewriting (e.g. extending) a panel function:

Start with a graphing command (e.g. a call to xyplot) that “works”, i.e. it has nothing wrong with it -- it’s just missing some cool features.

Rewrite the panel function but without adding any functionality!

Gradually add the elements you need.

Page 36: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

30

40

50

60

70

80

500 1000 5000 10000 50000

●●●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

AfricaAmericasAsiaEuropeOceania

yDat <- subset(gDatOrdered, year == jYear)xyplot(lifeExp ~ gdpPercap, yDat, xlab = jXlab, ylab = jYlab, scales = list(x = list(log = 10)), xlim = jXlim, ylim = jYlim, xscale.components = xscale.components.log10, key = continentKey, panel = panel.xyplot)

The points are in the right place. Everything is “right” except size and fill color........

Page 37: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

see step-by-step development towards a non-default panel function in the tutorial:http://www.stat.ubc.ca/~jenny/STAT545A/block10_latticeNittyGritty.html

Page 38: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

xyplot(lifeExp ~ gdpPercap | factor(year), ...)

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

30

40

50

60

70

80

10^2.5 10^3.0 10^3.5 10^4.0 10^4.5

●●

●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

● ●

●●

●●

●● ●●

1962

●●●

●●● ●●

● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

1977

●●

● ● ●●●

●●●

● ●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

● ●

●●

●●●

●●

●●

1992

10^2.5 10^3.0 10^3.5 10^4.0 10^4.5

30

40

50

60

70

80●●●

●●

●●●

●●●●

●●●

●●

● ●

●●

●●

●●

●●

● ●● ●

●● ●

●●

●●

●●

●●

● ●

●●

● ●

● ●

2007

AfricaAmericasAsiaEuropeOceania

Page 39: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

30

40

50

60

70

80

10^2.5 10^3.0 10^3.5 10^4.0 10^4.5

●● ●

●●●

● ●

●●

●●

●●

●●

●●

Africa

●● ●●

●●●

●●●

● ●

●●

Americas

●● ●●●

●●

●●

●●

●●

Asia

10^2.5 10^3.0 10^3.5 10^4.0 10^4.5

30

40

50

60

70

80●●

●●●●●

●●● ●●

● ●

●●●

● ●

● ●

●●

Europe

AfricaAmericasAsiaEuropeOceania

xyplot(lifeExp ~ gdpPercap | continent, ...)

Page 40: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Income per person (GDP/capita, inflation−adjusted $)

Life

exp

ecta

ncy

at b

irth

(yea

rs)

304050607080

10^2.5 10^3.5 10^4.5

●●

●●

●●●

● ●●●

●●

●●

●●

●●

●●

1962

Afric

a

●●

●●

● ●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

1977

Afric

a

10^2.5 10^3.5 10^4.5

●●

●●●

●●●

●●●

● ●

●● ●

●●

● ●

●●

●●

1992

Afric

a●

●● ●

●●●

● ●

●●●●

●●

●●

●●

2007

Afric

a

●●●

●●

●●

● ●

●●

●●

●●

●●

1962

Amer

icas ●●

●●

●●

●●

●●● ●

1977

Amer

icas ●●● ●

●●

●●

●●●

●●

●●●●

1992

Amer

icas

304050607080

●●● ●●

● ●●

●●

●●

●●●●

● ●●●

●●

2007

Amer

icas

304050607080

●●●

●●●

●●

●●

●●

●●

●● ●

1962As

ia

●●●

●●●

●●

●●

●●

●●

● ●

1977

Asia ●

●●● ● ●●

●●

● ●

●●

●●

1992

Asia ●●

●●● ●●

●●

●●

●●

●●

●●

2007

Asia

●●●●●●

●●

●●

●●

●●

●●

●●

1962

Euro

pe

10^2.5 10^3.5 10^4.5

●●●●

●●●

●●●

●●

●● ●

●●

●●

●●

●●

●●

1977

Euro

pe

●●

●●●●●●

●●

●●●

●●

●●●

●●

1992

Euro

pe

10^2.5 10^3.5 10^4.5

304050607080●

●●●●●●●

●●● ●●

● ●

●●●

● ●

● ●●

●●

2007

Euro

pe

library(latticeExtra) # useOuterStrips()jPlot <- xyplot(lifeExp ~ gdpPercap | factor(year) * continent, ...)useOuterStrips(jPlot)

Page 41: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

saving figures to file

Page 42: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

do not save figures mouse-y stylenot self-documentingnot reproducible

http://cache.desktopnexus.com/thumbnails/180681-bigthumbnail.jpg

Page 43: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

(Getting ready to) make a figure• First draft (and the next ~100 attempts!) should be displayed

in a window; for example, the Rstudio Plots pane

• When ready to preserve for posterity, I advise you default to PDF. Most generally useful format to have around.

- Educate yourself about vector versus raster graphics here (or Google it yourself). Short version: default to vector, which PDF is an example of.

• Save the figure w/ a line of R code in your script, not with the mouse.

• If preparing a figure for the web and/or a figure that presents a very large number of data points -- png() is a good option to consider. The web winds are blowing towards SVG these days.

Page 44: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

(Getting ready to) make a figure• If you rigorously produce your figures from code you save --

versus via typing at the command line or (shudder) with a mouse -- it will always be easy to remake a figure natively on a different device. This will come up!

• Additionally, it will be easy to find the R code that generated any figure by searching for the figure filename on your computer, filtering for *.R files if possible.

• The following methods for writing to PDF should work for other devices.

Page 45: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

Two good blog posts on saving R graphics

10 tips for making your R graphics look their bestRevolutions blog posthttp://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html <-- highly recommended

High-quality R graphics on the Web with SVGRevolutions blog posthttp://blog.revolutionanalytics.com/2011/07/r-svg-graphics.html

Page 46: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

pdf(paste0(whereAmI,"figs/rawPlotsByORF.pdf"), width = 9.5, height = 7)for(i in seq(along = levels(kDat$ORF))) print(xyplot(pheno ~ tm | day, kDat,<blah, blah, ...>)dev.off()

Save a figure -- Method 1

• Open a PDF device by calling pdf(), execute all your graphics commands, close the PDF by calling dev.off()

- Pros: Most ‘correct’ method. Possible to create multi-page files.

- Cons: pdf() and dev.off() commands clutter up your R file and require repeated (de-)commenting out. It’s annoying to re-submit all those commands, when you’re staring at a beautiful figure on the screen.

Page 47: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

xyplot(lifeExp ~ gdpPercap | year, gDat)dev.print(pdf, file = paste0(whereAmI,"figs/bryan-a01-latticeMinimal.pdf"), width = 9, height = 6)

Save a figure -- Method 2• Make the figure in a screen device, such as X11(). Call

dev.print to copy that to a PDF file.

- Pros: Immediate gratification. PDF-generating code is limited to one command, so easier to (de-)comment out.

- Cons: Slightly ‘incorrect’. Certain aspects of the figure depend on the graphics device (which would be X11, not PDF), therefore, in some cases, you can get different PDF files with Methods 1 and 2. It’s a risk I’m often willing to take. Also, won’t work for multi-page figures.

Page 48: STAT 545A Class meeting #7 Monday, September 30, 2013jenny/STAT545A/2013-lectures… ·  · 2013-10-04scales = list(x = list(log = 10)), xbins = 40) gdpPercap lifeExp 30 40 50 60

BTW ggplot2 has some special ways to write figures to file