R project

profileJasperZ511626
7_Graphics.pdf

Graphics)

Why)is)graphics)in)this)course?)

•  Good)graphics)today)requires)the)computer) •  Visualiza9on)enters)every)step)of)the)data) analysis)cycle))

– Data)cleaning)–)are)there)anomalies?) – Explora9on))) – Model)checking) – Repor9ng)results)

•  Plots)can)uncover)structure)in)data)that)can’t)be) detected)with)numerical)summaries)

•  Important)communica9on)skill)

R’s)graphics)model)

•  There)are)two)models)in)R)–)painter)and) objectJoriented)

•  We)will)use)the)painter’s)model) •  The)other)is)easy)to)get)started)but)hard)to) tweak)

•  Painter’s)model)–)start)with)a)blank)canvas,) add/paint)on)it)in)mul9ple)passes)

)

)

Know)your)data)types)

The)appropriate)graphical)techniques)depend)on) the)kind)of)data)that)you)are)working)with)

•  Quan9ta9ve)) – con9nuous)–)e.g.)height,)weight) – discrete)–)numeric)data)with)few)values,)e.g.)number) of)children)in)family)

•  Qualita9ve)) – ordered)–)categories)with)an)order)but)no)meaningful) distance)between,)e.g.)number)of)stars)for)a)movie)

ra9ng)

– nominal)J)categories)have)no)meaningful)order,)e.g.) gender)

Case:)Infant)Health)

load(url("hUp://www.stanford.edu/~vcs/

StatData/KaiserBabies.rda")))

)

Smoking:))

Some)doctors)used)to)recommend)it))

Today:)

Kaiser)Study)

•  Oakland)Kaiser)mothers)) •  1960s) •  Measure)the)babies)weight)(in)ounces))at) birth)

•  All)babies:)) – Male) – Single)births)(no)twins,)etc.)) – Survived)28)days)

> names(infants)! )[1])"gesta9on")"bwt")))))))"parity"))))"age")))))))

)[5])"ed"))))))))"ht"))))))))"wt"))))))))"dage"))))))

)[9])"ded")))))))"dht")))))))"dwt")))))))"marital")))

[13])"inc")))))))"smoke")))))"number"))))

> dim(infants)! [1])1236)))15)

Informa9on)collected)on)mother’s)and)

their)babies)

•  Birth)weight)(ounces)) •  Gesta9on)(weeks)) •  Parity)J)total)number)of)previous)pregnancies) •  Mother’s)height)and)weight) •  Mother’s)smoking)status) •  Mother’s)age,)race,)educa9on)level,)income) •  And)more…)

Here)are)the)data)for)birth)weight)

What)do)you)see?) )[1])120)113)128)123)108)136)138)132)120)143)140)144)141)110)114)115))92)115)144)119)105)115)137)122)131)103)146)114)125)114)122))93)130)119)113)134)

))[37])107)134)122)128)129)110)138)111))87)143)155)110)122)145)115)108)102)143))146)124)124)145)106))75)107)124)122)101)128)104))97)137)103)142)130)156)

))[73])133)120))91)127)153)121)120))99)149)129)139)114)138)129)138)131)125)114)])128)134)114))92))85)135))87)125)128)105)120)119)116)107)119)133)155)126)

)[109])129)137)103)125))91)134))95)118)141)131)121)100)131)118)152)121)117)115))112))94)109)132)117)101)112)128)128)117)134)127))93)122)100)147)120)144)

)[145])105)136)102)160)113)126)126)115)127)119)129)123)118)133)105)134)144)111))125)135)134)116)129)113)131)126)121)121)138)136)120)122)134)101)112)132)

)[181])136)113))96)124)113)131)137)133)107))96)142)136))75)125)104)130))90)118))123)137)101)142))98)124)151)109)150)119)131)101)113)127))97)117)150))85)

)[217])128)105))90)115)107)121)119)117)134)117)115)110)130)140)111))93)154)125))93)122)129)126))85)173)144)114)111)154)150)111)126)122)141)142))99)113)

)[253])149)117)130)106)128)125)114)130)116))81)124)125)110)125)138)142)115)102)140)133)127)104)119)152)123)143)131)141)129)113)119)109)104)131)110)148)

)[289])137)117)115))98)136)121)132))91)119))85)106)132))80)109)111)143)136)110)))98)108)101))71)124))93)106)101)100)104)117)117)149)135)110)121)142)104)

)[325])138)112)117)109)131)120)116)140)103)120)139)123)104)131)111)122)116)129))133)110)105))93)122)133)130)104)106)120)121)118)140)114)116)129)120)127)

)[361])107))71))88)107)122)106)135)107)129)126)116)124)123)145)102)129))98)110))135)101))96)104)100)154)127)126)126)127))98)127)129)131)132)127))99)115)

)[397])145)102)136)121)121)120)118)127)132)102)143)118)102)163)132)116)138)139))132))87)131)130)123)115)116)119)125)144)123)120)140)120)116)120)146)112)

)[433])115)132)146)122)128)119)135)116)129)116)100)118)138)123)113)129)122)132))120)114)130)117)142)144)127)115))85))99)123)112))68)102)109)102))99))78)

)[469])128)107)136)101)100)109)117))88))95)119)123)127)107)124)126))98))96)104))133))93)101)118)130)125)140)115)130)114)105)101)132)112))69)114)123)129)

)[505])114)115))98)128)119)119)154)127)131)129)114)110)103)117)138)126)124)111))132)103)158)146)101)132)114))71)116)108)123)129)134)113)123)147)121)125)

)[541])115)101))93)109)115)130)123)111))97)122)124)129)124)107)142)129)174)105))103)124)105)133)161)105)108)153)133)115)127)128)117)123)119)141))91)116)

)[577])116)121)111)102)118)126))98)131)115)103)147)123)125)117))99)115)116)118))170)104)108)144))99))97)142))85)130)117)109)147)105)135)115)123)105)154)

)[613])110)119)103)117)120)145)104)123)124)129))91)109)108))79)133)114)128)129))97)103)176)143)127)107)113)106)152)150)136)151)124)123)119)122)112))93)

)[649])109)136)121)150))94)120)146)129)125)124)141))96)138)127)114)103)127)141))113))99))97)116)126)158)119)123)129)117)100)131)146))84)115)115)118))91)

)[685])112)115)110)117)109))99)131)136)130)134)128)150))86)115)141))78)100)116))110)109)113)136)114)121)117)166))87)120))95)132))90)131)103)144)137)124)

)[721])136)117)121)116)139)110))86)133))81)133)132)132)137))84)136))92)114)129)167))71)124)105)155)125)125)125)115)174)127)113)115)139)127)111)112)143)

)[757])116)155)121)110))87)132)105)129)123))91)147)144)128)137)104)120)112)138)))96)134)126)112)138)110))83)112)148)119))86)110)126)125)136)127))84)131)

)[793])123))96)110)123)152)127)117)125)139)114))96)124)107)113))98)119)107)117))117)144)136)121)165)120)125)137)100)134))88)108)123)141)130)139)130)113)

)[829]))77))62))93)109)145))92)120)135)113)126)143)128))98)110)162)116)128)111)137)134)100)160)112)134)145)116)126)111)126)109)136)119)103)124)155)122)

)[865])113)122)126)116)102)110)133)125)164)133)135)124)122)121)100)129))90)128))116))86)123))87)128)120)125)118)116)131)151))88)137)127))96)129)128))85)

)[901])111)124)112)115))72)122)116)127))90))99)144)138))58)109)110)129)150)128)142)115)108)108)139)115)136)163)131))77)124)104)102))94)158)112)119))97)

)[937]))99)115)139)144))99)105))89)129)119)114)106)122)136)121)112)112)123)139)125)105)130)146)133)147)109)122)135)107)117)138)120)119)118)105)113)136)

)[973])148)140)134)120)123)102))55)103)123)105)138)128)139)104)159)118))99)144))121)117)119)105)125)119)101)105)110)100))98)127)117)122)122)118)137)120)

[1009])143)108)131)110)105)133)125))78)114)111)103)114))75)169))94)150)144)144))143)145)121)105)134)129)114))97)160))65)145))95)139)123)109)110)122)115)

[1045])117)108)120)131)136)125))96)102)102)112)135))91)129)155)109))80)125))94))148))73)123))65)118)102)120)108)122)103)105)126)145)139)124)121)126)119)

[1081])114)118)127)117)137)133)100)107)115))91)112)125)157)108)130)135)123)100))124)174)129)119)126)128)116)100))96)131)110)108)129)141)110)118)111)160)

[1117])120)121)113)117)158)128)158)133)163)128)126)127)134)140)102)100)120))98))130)104)122)137)114))63))98))99))89)117)143)106))99)156))72))75))97)106)

[1153]))91)117)117)112)112)141)131)130)132)114)160)106))84)112)139)104)130))71))82)119)123)115)124)138))88)146)128))82)100)114))97)126)122)152)116)132)

[1189]))84)119)104)106)124)139)103)112))96)102)120)102))97)113)130))97)116)114)127))87)141)144)116))75)138))99)118)152))97)146))81)110)135)114)124)115)

[1225])143)113)109)103)118)127)132)113)128)130)125)117)

Rug)plot)

Birth Weight

60 80 100 120 140 160 180

Each)baby’s)weight)is)represented)as)a)9ckmark.)The)

thicker)lines)are)from)mul9ple)babies)with)similar)

weights.)I)added)a)liUle)random)noise)to)the)weights)

to)keep)them)from)falling)on)top)of)each)other.))

What%can%you%see%now?))

How%are%birth%weights%distributed?%

Distribu9on)of)Birth)Weight)

•  The)distribu5on)is)the)paUern)of)varia9on)in) the)birth)weights.))

•  It)provides)the)numerical)values)for)birth) weight)and)how)oien)each)value)occurs.))

•  A)histogram/density%plot)shows)the)shape)of) the)distribu9on)

Histogram: ))hist(infants$bwt))

hist(infants$bwt),)freq)=)FALSE,))

))))))))xlab)=)"Birth)Weight)(oz)",))

))))))))main)=)"Male)Babies,)Oakland)Kaiser)1960s")

Histograms%

•  Are)a)special)case)of)density)plots) •  AREA)=)Propor9on)(or)percent)) •  The)area)of)a)bar:)

Height)*)Width)=)Area)

(Propor9on/oz))*)oz)=)Propor9on)

•  Histograms)are)not)the)same)as)bar)charts) •  With)bar)charts,)it)is)only)the)height)that) maUers.)Bar%charts%are%for%qualita5ve%data%%

Density)plot)–)smoothed)histogram)

plot(density(infants$bwt)))

50 100 150

0 .0 0 0

0 .0 0 5

0 .0 1 0

0 .0 1 5

0 .0 2 0

Male babies born at Oakland Kaiser in the 1960s

Birth Weight (oz)

D e n s it y (

p ro

p o rt

io n p

e r

o z )

plot(density(infants$bwt), ! xlab = "Birth Weight (oz)", ! main = "Male Babies, Oakland Kaiser…"))

Babies)birth)weight) plot(density(infants$bwt, bw = 1)))

60 80 100 120 140 160 180

0 .0 0 0

0 .0 0 5

0 .0 1 0

0 .0 1 5

0 .0 2 0

0 .0 2 5

Male babies born at Oakland Kaiser in the 1960s

Birth Weight (oz)

D e n s it y (

p ro

p o rt

io n p

e r

o z )

Selec9ng)a)bandwidth%

•  R)chooses)a)bandwith)for)you,)but)you)can) specify)one)if)you)like.)

•  The)goal)is)to)see)the)overall)shape)of)the) distribu9on,)not)the)individual)points.)

•  In)a)way,)the)density)is)a)smooth)abstrac9on) of)the)distribu9on.)

Boxplot:)))))boxplot(infants$bwt))

boxplot(infants$bwt, ! xlab="Birth Weight (oz)")!

Looking)for)Structure:)

Quan9ta9ve)Distribu9on%

•  Distribu5on:)paUern)of)values)for)a)variable% •  Mode:)high)density)region) •  Long%Tail:)many)observa9ons)far)from)center)) •  Symmetry/Skewness:)distribu9on)of)values) the)lei)and)right)of)the)center.))

•  Gaps:)places)where)there)are)no)observa9ons.%) •  Outliers:)unusually)large)or)small)values)that) falls)well)beyond)the)overall)paUern)of)data)

What)Structure)Do)You)See?%

)

What)Structure)Do)You)See?%

)

Parity:)Number)of)siblings)

•  This)quan9ta9ve)variable)is)different)from) birth)weight)–)there)are)only)a)few)possible)

values,)i.e.)it’s)not)possible)to)have)2.3)

siblings,)and)it’s)highly)unlikely)to)have)17)

>)table(infants$parity)! ))0))))))1)))))2))))))3))))4)))))5)))))6))))7)))8))))9))10)11))13))

315)310)238)168))83))52))32))16)))8)))7))))4)))2))))1))

Number)of)Siblings)

0 1 2 3 4 5 6 7 8 9 11

Number of siblings

0 50

15 0

25 0

barplot(table(infants$parity))))

Alterna9ve)–))bar)width)has)no)meaning) 0.

00 0.

10 0.

20

Number of siblings

P ro

po rt

io n

0 1 2 3 4 5 6 7 8 9 11 13

plot(table(infants$parity), ! type ="h", lwd = 4, ! ylab ="Proportion", col="darkgrey")!

Case:)College)Students)

load(url("hUp://www.stanford.edu/~vcs/

StatData/videogame.rda")))

)

> objects()! )[1])"infants"))"video”)

)

> names(video)! )[1])"9me"))"like"))"where")"freq"))"busy"))"educ"))

)[7])"sex")))"age")))"home"))"math"))"work"))"own")))

[13])"cdrom")"email")"grade”)

)

>)dim(video))

[1])91)15)

STAT)2)Survey)

•  Random)Sample)of)91)of)314)Berkeley) students)enrolled)in)Stat)2)

•  Survey)collected)the)following)info:) – sex)–)Male/Female) – grade)–)grade)expected)in)the)course)(“A”,)“B”,) “C”,)“D”,)“F”)))

•  What)type)of)data)are)these?) – sex)is)qualita9ve)(nominal)) – grade)is)qualita9ve)with)an)ordering) )

Make)tables)of)qualita9ve)data)

> table(video$grade)! )

)F))D))C))B))A))

)0))0))8)52)31))

>)table(video$grade,)video$sex))

) Female Male! F 0 0! D 0 0! C 8 0! B 21 31! A 9 22!

Anything)unusual)

about)the)expected)

grade?)) )

Does)expected)

grade)depend)on)

gender?)

Expected)Grade)

Bar)chart)

)

Pie)chart))

pie(table(video$grade)) )

F D C B A

0 1 0

2 0

3 0

4 0

5 0

C

B

A

AREAS)can)be)

hard)to)compare) WIDTH)of)bars)have)no)

meaning)

Expected)Grade)

Dot)chart)

dotchart(table(video$grade), pch = 19)!

F D C B A

0 10 20 30 40 50

Focus)on)

comparison)of)

the)values))

Method)of)Comparison)

•  Oien,)we)not)only)want)to)beUer)understand) a)distribu9on,)but)we)want)to)compare)the)

distribu9on)for)subgroups)or)to)compare)

against)another)popula9on)or)standard)

)

•  How)do)you)think)the)expected)grade) distribu9on)might)vary)with)gender?))

Two)Qualita9ve)variables) Stat 2 Survey

sex

g ra d e

Female Male

F D

C B

A

mosaicplot(table(video$sex,)video$grade),))

)))))))))))))))))))))main)=)"Stat)2)Survey"))

How)to)read)a)Mosaic)plot))

There)are)91)

students)in)the)

survey.)

Think)of)them)as)

spread)out)evenly)

in)the)box)

New)Plot:)Mosaic))

Put)all)the)

females)on)

one)side)of)

the)box.)

There)are)38.)

New)Plot:)Mosaic))

Rearrange)the)females)

so)that)those)who)

expect)the)same)grade)

are)together)in)the)

box.)

8)of)the)38)expect)a)C)

Mosaic)plot))

Stat 2 Survey

sex

g ra d e

Female Male

F D

C B

A

Smaller)

frac9on)of)

females)expect)

an)A)in)

comparison)to)

Males))

None)of)the)

males)expect)

a)C)

Case:)East)Bay)Housing)Market)

load(url("hUp://www.stanford.edu/~vcs/

StatData/SFHousing.rda")))

Warning:)It’s)BIG)

)

San)Francisco)Chronicle)lis9ngs)

Data)

Variables:)

•  City) •  County) •  Price) •  #)bedrooms) •  Lot)square)footage) •  and)10)more)

•  Record:)house)sold)in)a) par9cular)9me)period)

)

•  Over)200,000)houses) )

•  Subset)to)a)dozen)ci9es) in)the)East)Bay)–)about)

25,000)houses)

Rela9onship)between)city)and)

sale)price)

Data)types:)

City)J)factor)

Sale)price)J)numeric)

Examine)a)subset)of)the)ci9es)

someCities = c("Albany", "Berkeley”, "El Cerrito", "Emeryville", "Piedmont", "Richmond", "Lafayette", "Walnut Creek", "Kensington","Alameda","Orinda”,"Moraga")! shousing = ! housing[housing$city %in% someCities & housing$price < 2000000,]! dim(shousing)! [1])20415))))15)

boxplot(shousing$price ~ shousing$city, las = 2)!

Boxplots)

Ci9es)ordered)by)median)price)

Rela9onship)between)price)per)

square)foot)and)total)square)foot)

Both)are)quan9ta9ve)

ppsf = shousing$price/shousing$bsqft 
 plot(ppsf ~ shousing$bsqft)!

WHAT’s%Wrong%

with%this%plot?%

ScaUer)plot)

plot(ppsf ~ shousing$bsqft, plot)y)against)x) pch=19, change)plovng)character)to)solid)circle) cex = 0.2, shrink)plovng)character)to)20%) subset = shousing$city =="Berkeley",!

Plot)a)subset)of)records))

main="Berkeley", 9tle)of)plot) xlab="Area (ft^2)", label)for)x)axis) ylab = "Price/ft^2") label)for)y)axis)

Rela9onships)between))

more)than)2)variables)

•  Qualita9ve)informa9on)can)be)conveyed)in) plots)through)color,)plovng)symbol,)

juxtaposed)panels)

•  The)following)plot)uses)informa9on)from)4) variables:)city,)number)of)bedrooms,)lot)size)

(sq)i),)and)price)per)square)i)

● ●

●●

● ●

●●

● ●

● ●

●●

● ●

● ●

● ●

● ●

● ●

●●

●●

● ●

●●

● ●

●●

●● ●

● ●

● ●

● ●

● ●

●●

● ●

● ●

● ●

● ●

● ● ●

● ●

● ●

●● ●

● ●

● ●

● ●●

● ●

● ●

● ●

● ●

● ●

● ● ●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

●●

● ●

● ●

● ●

●●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

●●

●●

● ●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

●●

● ●

● ●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ● ●

● ●

● ●

● ●

●●

● ●

● ●

●●

● ●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

1000 2000 3000 4000 5000

2 0 0

4 0 0

6 0 0

8 0 0

Berkeley

Square Feet

P ri

c e p

e r

s q u a re

f e e t

1 bedrooms

2 bedrooms

3 bedrooms

4 bedrooms

5 bedrooms

6 bedrooms

7 bedrooms

8 bedrooms ●

● ●●

● ●

● ●

● ●

● ●

●●

●● ●

● ●

● ●

● ●

● ●

●●

● ●

●●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

● ●

●●

● ●

● ●

● ●

0 1000 2000 3000 4000 5000

2 0 0

4 0 0

6 0 0

8 0 0

Piedmont

Square Feet P

ri c e p

e r

s q u a re

f e e t

1 bedrooms

2 bedrooms

3 bedrooms

4 bedrooms

5 bedrooms

6 bedrooms

7 bedrooms

8 bedrooms

What)do)you)see?)

Summary)of)graph)rela9onships))

between)two)variables)

•  Two)Qualita9ve)variables) – mosaicplot,)sideJbyJside)barplots)

•  One)Quan9ta9ve)and)one)Qualita9ve) – Boxplots,)dotcharts,)mul9ple)density)plots,) violin)plots)

•  Two)Quan9ta9ve)variables) – ScaUer)plot,)line)plot)

What)do)you)think)of)this)plot?)

FIND)5)things)that)you)would)change))

Let’s)fix)it!) )

Making)good)plots)is)an)itera9ve)process)

Goal)is)to)convey)a)message)as)clearly)as)

possible)

Visit)the)website)

hUp://www.stanford.edu/~vcs/StatData/chip04.txt)

)

Read)it)into)R)

> chips = read.table("http:// www.stanford.edu/~vcs/StatData/ chip04.txt", header = TRUE)! ! > class(chips)! [1] "data.frame"! > names(chips)! [1])"Name"))))))))"Date"))))))))"Transistors")

[4])"Microns")))))"ClockSpeed"))"Data"))))))))

[7])"MIPS"))))))))

> dim(chips)! [1])10))7)

)

This)is)preUy)easy)to)get)

plot(chips$Date, chips$Transistors,! type ="l", ! lwd = 3, ! col ="green",! log ="y")! ! How)can)we)improve)it)

even)more?)

%

•  Add)more)data) •  Add)legend)for)different) informa9on)

•  Add)reference)lines)for) important)date)

)

)

Review)Plovng)Func9ons)

•  hist() histogram) •  boxplot() boxplot) •  dotchart() dotchart) •  plot())for)scaUer) plots,)line)plots,)density)

plots)

•  barchart()! •  pie()! •  mosaicplot()!

•  abline() add)line)to) canvas)

•  points() add)points) to)canvas)

•  lines() add)line) segments)to)canvas)

•  text() add)text)to) canvas)

Review)Plot)Arguments))

?plot.default! •  type = "l" "p")for)

points,)"l")for)lines,)"n”)

for)nothing)

•  ylim = c(0, 1) the) range)for)the)scale)of)the)

axis)

•  xlab = "x axis label"!

•  main = "plot title"! •  col =)vector)of)colors) •  log = "y" use)log)

scale)on)y)axis,)can)be)"x")

or)"xy")

•  lwd = 2 thickness)of) line)

•  pch = 19 plovng) character)–)check)other)

numbers)

•  cex = 0.5 character) magnifica9on)))

•  lty = 2 type)of)line)–) check)other)numbers))

•  las = 1 0,1,2,)or)3) style)of)9ck)mark)labels)

)

Graph)Construc9on)

Outline)

•  Vocabulary) •  3)Proper9es)of)good)graph)construc9on)

– Data)stand)out) – Facilitate)comparison) – Informa9on)rich)

•  Percep9on) •  Case)studies)

Vocabulary)

● ●

● ●

● ●

● ●

● ●

Axis Label: Day

● ●

● ●

● ●

● ●

● ●

10 15 20 25 30 tick

mark tick

mark

label

Reference line: 75

V e

rt ic

a l A

x is

plotting

symbol

Title: Temperature in August

data label

Min: Aug 17 over 70

below 70

Legend

Data)Stand)Out)

Avoid)having)other)graph)elements)

interfere)with)data)

● ●

● ●

● ●

● ●

● ●

Day

1 3 5 7 9 12 15 18 21 24 27 30

Use)visually)prominent)symbols)

● ●

● ●

● ●

● ●

● ●

0 5 10 15 20 25 30

6 5

7 0

7 5

8 0

Day

D e g re e s

Avoid)overJplovng)

● ●

●●

●●● ●

●●

● ●

● ●

● ●

●●

● ●●

● ●

●●

●●

● ●

●● ●

●●

● ●

●●

● ●

●●

● ●

●●

●●

● ●

●●

●● ●

●● ●

●●

●●

●● ●● ●

● ●

● ●

● ●

●●

● ●●

●●

● ●

●●

● ●

●●

● ●

●● ●

● ●

●● ●

●●

●●●

●●

● ●●

● ●

● ● ●

●● ●

●●

●●

●●●

●●

● ●

55 60 65 70

6 0

6 5

7 0

7 5

1200 Families

Mom Height

D a

d H

e ig

h t

Why)are)there)so)

few)data)points?)

One)way)to)avoid)over)plovng:)

JiUer)the)values)

55 60 65 70

6 0

6 5

7 0

7 5

jitter(ht, 2)

ji tt

e r(

d h

t, 2

)

Add)a)liUle)bit)of)random)

noise)so)all)of)the)values)

aren’t)ploUed)on)top)of)

each)other)

Shrink)the)

plovng)symbol)so)

they)don’t)plot)on)

top)of)each)other)

See)a)point)cloud)J))

Different)values)of)data)may)obscure)

each)other)

0 50 100 150 200

0 .0 0

0 .1 0

0 .2 0

D e n s it y

0 2 4 6 8 10

0 .0 0

0 .1 0

0 .2 0

D e n s it y

Most)of)the)data)are)in)the)0)to)10)range.)))

The)few)large)values)obscure)the)bulk)of)the)data.)

Consider)men9oning)these)large)values)in)a)

cap9on,)instead)of)showing)them)in)the)plot.)

Choosing)the)Scale)of)the)Axis)

•  Include)all)or)nearly)all)of)the)data) •  Fill)data)region) •  Origin)need)not)be)on)the)scale) •  Choose)a)scale)that)improves)resolu9on)(to)be) con9nued))

Eliminate)superfluous)material)

•  Chart)junk)–)stuff)that)adds)no)meaning,)e.g.) buUerflies)on)top)of)barplots,)background)

images)

•  Extra)9ck)marks)and)grid)lines) •  Unnecessary)text)and)arrows)) •  Decimal)places)beyond)the)measurement) error)or)the)level)of)difference)

Facilitate)Comparisons)

Put)Juxtaposed)plots)on)same)scale)

15 20 25

0 .0 0

0 .0 5

0 .1 0

0 .1 5

0 .2 0

Group 1

D e n s it y

20 30 40 50 60

0 .0 0

0 .0 2

0 .0 4

0 .0 6

0 .0 8

Group 2

D e n s it y

Make)it)easy)to)dis9nguish)elements)

of))superposed)plots)(e.g.)color))

20 30 40 50 60

0 .0 0

0 .0 5

0 .1 0

0 .1 5

0 .2 0 Groups

D e n s it y

Choosing)the)Scale)

•  Keep)scales)on)x)and)y)axes)the)same)for)both) plots)to)facilitate)the)comparison)

•  Zoom)in)to)focus)on)the)region)that)contains) the)bulk)of)the)data)

•  These)two)principles)may)go)counter)to)one) another)

•  Keep)the)scale)the)same)throughout)the)plot) (i.e.)don’t)change)it)midJaxis))

)

Emphasizes)the)important)difference)

Which)of)these)sideJbyJside)bar)plots)

emphasizes)the)important)difference?)

Avoid)Jiggling)the)baseline)

It)is)difficult)to)see)how)a)

country)has)changed)over)

9me)because)the)boUom/

base)line)moves)

Comparison:)volume,)area,)height)

We)naturally)

compare)the)

volume)of)the)

barrels,)but)the)

change)is)really)the)

height)of)the)

barrels)

Informa9on)Rich)

How)to)make)a)plot)informa9on)rich)

•  Describe)what)you)see)in)the)Cap5on% •  Add)context)with)Reference%Markers)(lines)and) points))including)text)

•  Add)Legends)and)Labels% •  Use)color)and)plovng)symbols)to)add)more) informa9on)

•  Plot)the)same)thing)more)than)once)in)different) ways/scales)

•  Reduce)cluUer)

Cap9ons)

•  Cap9ons)should)be)comprehensive) •  SelfJcontained) •  Cap9ons)should:)

– Describe)what)has)been)graphed) – Draw)aUen9on)to)important)features) – Describe)conclusions)drawn)from)graph)

Good)Plot)Making)Prac9ce))

•  Put)major)conclusions)in)graphical)form) •  Provide)reference)informa9on) •  Proof)read)for)clarity)and)consistency) •  Graphing)is)an)itera9ve)process) •  Mul9plicity)is)OK,)i.e.)two)plots)of)the)same) variable)may)provide)different)messages)

•  Make)plots)data)rich))

)

Percep9on))

Color,)shape)(including)banking))can)

affect)your)ability)to)make)good)

comparisons)

Banking:)Aspect)Ra9o)

•  The)height/width)of)the)data)region)was) selected)to)be)about)1)so)that)the)trend)line)is)

at)about)45)degrees.))

•  The)Aspect)ra9o)affects)our)visual)decoding)of) the)rate)of)change)

•  )The)banking)to)45)degrees)helps)us)see)rate) of)change)

•  The)ability)to)effec9vely)judge)rate)of)change) allows)us)to)see)important)paUerns)in)data)

Banking)at)45)degrees)

•  Roughly:)Examine)the)absolute)value)of)the) orienta9on)of)segments,)they)should)be)

centered)at)45)degrees.)

•  Transforma9ons)to)improve)the)aspect)ra9o) uncovers)the)structure)of)the)rela9onship)

between)variables)

•  Easier)to)see)important)features))

Bank)to)45)degrees)

2 4 6 8 10

0 5 0

1 0 0

1 5 0

2 0 0

2 4 6 8 10

2 5

1 0

2 0

5 0

1 0 0

2 0 0

log-transformation

1 2 5 10

2 5

1 0

2 0

5 0

1 0 0

2 0 0

log-log transformtation

Shapes)

POP%

QUIZ!!!%

Number)your)paper)1J6)

1.  ____________) 2.  ____________) 3.  ____________) 4.  ____________) 5.  ____________) 6.  ____________)

Warm)up:)

What)%)of)the)white)is)the)blue?)

100%)

Warm)up:)

What)%)of)the)white)is)the)blue?)

50%)

Warm)up:)

What)percent)of)the)white)is)the)blue?)

100%)

1.)What)%)of)the)white)is)the)blue?)

2)What)%)of)the)white)is)the)blue?)

3.)What)%)of)the)white)is)the)blue?)

4.)What)%)of)the)white)is)the)blue?)

5.)What)%)of)the)white)is)the)blue?)

6)What)%)of)the)white)is)the)blue?)

How)accurate)were)you?)

You)Guess) )Truth) )Absolute)Error )Type))

1.  __70__))) )65) ) ))) ))5)) ) ) ) )Bar)) 2.  __33__) )35) ) ) ))2 ) ) ) ) )Pie )) 3.  __75__))))))60) ) ) )15) ) ) ) )Pie) 4.  __75__)) )75) ) ) ))0 ) ) ) ) )Bar) 5.  __35__) )30) ) ) ))5 ) ) ) ) )Bar) 6.  __85__) )75) ) ) )10) ) ) ) )Pie)

Bar)plot)vs)Pie)chart)

•  Cleveland’s)experiment)had)a)group)of)subjects) judge)40)pairs)of)values)on)bar)chars)and)the)

same)40)pairs)on)pie)charts:)What%percent%the% smaller%was%of%the%larger?%

•  Pie)chart)judgments)are)less)accurate)than)bar) chart)judgments)

•  Bar)chart)errors)are)about)the)same)size)for)all) percents.)

•  Pie)chart)errors)tend)to)be)larger)for)percents) greater)than)35%))

Color)

Color)Guidelines)

•  Choosing)a)set)of)colors)which)work)well) together)is)a)challenging)task)for)anyone)who)

does)not)have)an)intui9ve)gii)for)color)

)

•  )7J10%)of)males)are)) redJgreen)color)blind.)

Colorfulness)

•  Saturated/colorful)colors)are)hard)to)look)at) for)a)long)9me.))

•  They)tend)to)produce)an)aierJimage)effect) which)can)be)distrac9ng.))

)

Luminance)

•  If)the)size)of)the)areas)presented)in)a)graph)is) important,)then)the)areas)should)be)rendered)

with)colors)of)similar)luminance)(brightness).)

•  )Lighter)colors)tend)to)make)areas)look)larger) than)darker)colors)

Data)Type)and)Color)

•  Qualita9ve)–)Choose)a)qualita5ve)scheme)that) makes)it)easy)to)dis9nguish)between)categories)

•  Quan9ta9ve)–)Choose)a)color)scheme)that) implies)magnitude.))

– Does)the)data)progress)from)low)to)high?)Use)a) sequen5al)scheme)where)light)colors)are)for)low) values)

– Do)both)low)and)high)value)deserve)equal)emphasis?) Use)a)diverging)scheme)where)light)colors)represent) middle)values))

Brewer’s)Qualita9ve)PaleUe)

Accent

Dark2

Paired

Pastel1

Pastel2

Set1

Set2

Set3

Brewer’s)Diverging)PaleUe)

BrBG

PiYG

PRGn

PuOr

RdBu

RdGy

RdYlBu

RdYlGn

Spectral

Brewer’s)Sequen9al)PaleUes)

Blues

BuGn

BuPu

GnBu

Greens

Greys

Oranges

OrRd

PuBu

PuBuGn

PuRd

Purples

RdPu

Reds

YlGn

YlGnBu

YlOrBr

YlOrRd

Cases)

The)Plovng)Process)

•  Determine)what’s)the)message) •  Help)the)data)speak) •  Plovng)is)an)itera9ve)process)–)) •  An)ar9st)makes)many)sketches)before) pain9ng)the)masterpiece)

)

Case:)Voter)Registra9on)Trends)

in)California)

How)would)you)improve)this)plot?)

Changes)

•  Loca9on)of)9ck)marks)under)bars) •  Color)of)bars)–)indicate)party) •  Title) •  YJaxis)label)confusing) •  XJaxis)label)missing) •  Check)data)for)understanding)of)how)plot)is) made)

Data)

Majority of Democrats, Majority of Republicans, Election Year! 21,37,"2004")

23,35,"2008")

29,29,"2000")

37,21,"1996")

43,15,"1992")

)

Sources:)California)Secretary)of)State)

hUp://www.sos.ca.gov/elec9ons/ror/60day)presprim/hist)reg)

stats.pdf)

What’s)the)message?)

•  How)party)registra9on)has)changed)over)the)past) presiden9al)elec9ons)

•  More)informa9ve)if)we)have)registra9on)figures) for)people)not)coun9es)

•  County)size)may)be)a)lurking)variable)J)small) coun9es)tend)to)be)rural)and)conserva9ve))

)

Can)we)make)it)more)informa9on)rich?)

Data)

year,)eligible,)registered,)dem,)rep,)other,)decline)

1992,)20612814,)13217022,.485,).389,).031,).095)

1996,)19298379,)14314658,).474,).368,).052,).106)

2000,)21190865,)14676174,).462,).349,).052,).137)

2004,)21843202,)14945031,).432,).357,).049,).162)

2008,)22987562,)15468551,).427,).336,).044,).193)

)

How)about)a)line)plot)rather)than)bar)chart?)

Since)Other)and)“Decline)to)State”)are)about)25%)of)the)

2008)registra9ons,))leaving)them)out)of)the)plot)distorts)

the)message.)

0 1 0

2 0

3 0

4 0

5 0

Party Affiliation of Registered Voters in California

Year

P e r c e n t

1992 1996 2000 2004 2008

Democrat Republican Other Decline to state

0 1 0

2 0

3 0

4 0

5 0

Party Affiliation of Registered Voters in California

Year

P e r c e n t

1992 1996 2000 2004 2008

Democrat

Republican

Other

Decline to state

Colors)from)

Brewer’s)Set1)

Qualita9ve)

paleUe))

R)default)

colors)red,)

blue,)green,)

black)

Brief)look)at)how)to)use)the)special)

colors)from)Brewer’s)paleUes)in)R)

>)library(RColorBrewer))

>)colors)=)brewer.pal(9,)"Set1"))

)

>)plot(x,)y,)type)=)“l”,)col)=)colors[1]))

>)colors[1])

[1])"#377EB8”))J)R)doesn’t)give)regular)English)

names)to)these)colors.)More)later)on)this.))

Case:)CO2)emissions)around)the)

world)

ManyEyes)and)CO 2 )

How)might)we)

improve)this)plot?)

Changes)

•  Superpose)rather)than)stack)the)curves)so)the) baseline)doesn’t)jiggle)

•  Use)color)on)the)lines)rather)than)filling) polygons)

Many)Eyes)CO 2 )txt)file)

Read)into)R)from)the)Web)

myData))=)read.table()

url("hUp://www.stanford.edu/~vcs/StatData/CO2Na9ons.txt"),)))))

))))))))))))))))))))))header)=)TRUE,)sep)=)"\t"))

>)head(myData))

)“Kilos….”)))))X1980))))))))X1981))))))))X1982))))))))))X1983))))))))X1984))))…)

Argen9na)0.3833319)0.3814282)0.4067595)0.3983605)0.3945897)

Brasil))))))))0.2029906)0.1949738)0.1942669)0.1946483)0.1873183)

China)))))))2.9040459)2.7329550)2.6809608)2.5489631)2.4055907)

India)))))))))0.5828453)0.5912511)0.6074240)0.6142710)0.6118927)

Mexico)))))0.4228629)0.4251151)0.4661091)0.4523890)0.4259077)

Sudafrica)0.9629758)1.0296546)1.1275181)1.1957160)1.2300436)

)

What)do)you)no9ce)about)the)data?)

What)do)you)no9ce)about)the)data?)

•  The)data)frame)has)a)row)for)every)country) and)a)column)for)every)year))

•  In)R,)the)variables)are)the)columns)of)the)data) frame)

•  The)variables)are)years,)e.g.,)X1980.)Note)that) R)put)an)X)in)front)so)that)the)name)starts)

with)a)leUer.)

)

What)can)you)see)now?)

Case:)CO2)levels)at)Mauna)Loa))

Time)and)the)horizontal)axis)

Mauna)Loa)Volcano)

Largest)Volcano)in)world)

4)km)above)sea)level)

Summit)17)km)above)base)

On)the)Island)of))Hawaii) )

Data)and)photos)available)from)Scripps)Ins9tute)and)NOAA)

Mauna)Loa)Observatory)

•  Far)from)any)con9nent,) the)air)sampled)is)a)

good)average)for)the)

central)pacific.))

•  Being)high,)it)is)above) the)inversion)layer)

where)local)effects)are)

present.)

•  Measurements)of) atmospheric)CO

2 )since)

1958)–)longest)

con9nuous)record)

Atmospheric)Carbon)Dioxide)

•  The)increasing)amount)of)CO 2 )in)the)

atmosphere)from)the)burning)of)fossil)fuels)

has)become)a)serious)environmental)concern.)

•  )Upper)safety)limit)for)atmospheric)CO 2 )is)350)

parts)per)million)

•  Does)a)rise)in)CO 2 )lead)to)a)rise)in)world)

temperatures?))

Time)Series)–)Pairs:)(9me,)CO 2 ))

1960 1970 1980 1990 2000 2010

3 2 0

3 4 0

3 6 0

3 8 0

date

c o 2

Points)are)

typically)not)

the)best)way)

to)plot)9me)

series)

Connect)the)measurements)with))

line)segments)

1960 1970 1980 1990 2000 2010

3 2 0

3 4 0

3 6 0

3 8 0

date

c o 2

Seasonality)vs)the)longJterm)Trend)

1960 1970 1980 1990 2000 2010

3 2 0

3 4 0

3 6 0

3 8 0

Monthly Average CO2

Date

C O

2 (

p p

m )

1988

Aspect)Ra9o)

•  The)height/width)of)the)data)region)was) selected)to)be)about)1)so)that)the)trend)line)is)

at)about)45)degrees.)

•  )The)banking)to)45)degrees)let’s)us)see)that) the)curve)is)convex)

•  This)means)that)the)rate)of)increase)of)CO 2 )is)

increasing)through)9me)

Global)Warming)

•  1981)US)Senate)convened)scien9st)for) tes9mony)on)global)warning)

•  Senator)Al)Gore)said)that)the)Mauna)Loa)data) clearly)demonstrated)increases)in)CO

2 )

•  PewiU)(witness)for)the)DOE))said)that)the) graph)was)misleading)because)it)doesn’t)

include)0))

Chartology)

PewiU)took)issue)with)the)graph,)saying)

“It)is)a)clever)piece)of)chartology”)because)it)can)

be)read)the)wrong)way.)

He)con9nued,)“It)is)intellectually)just)exactly)

correct.)It)displays)315)going)to)336,)but)it)

appears)to)be)going)from)0)to)very)large)

amounts.”)

Steven)Schneider)(Global&Warming))called) PewiU’s)objec9on)“double)talk”))

Including)0)&)The)Aspect)Ra9o)

1960 2000

0 1 0 0

2 0 0

3 0 0

4 0 0

date

c o 2

1960 1970 1980 1990 2000 2010

0 2 0 0

4 0 0

date

c o 2

When)we)include)0,)if)we)also)bank)at)45)

degrees,)the)plot)must)be)tall)and)narrow.)

With)this)plot)it’s)hard)to)see)any)other)features.)

There)is)also)a)lot)of)empty)space.)

)

To)fill)the)space)with)data,)we)need)to)stretch)the)

data)region)to)be)wide)and)short.)

Now,)it’s)hard)to)see)the)most)important)feature)

because)the)banking)is)nearly)0.))

Case:)Napoleon’s)March)

Minard’s)Napoleon’s)March)

Minard)Map)

•  Size)of)Army)–)thickness)of)the)band) •  See)the)effect)of)individual)baUles,)e.g.)the) crossing)of)Berezina))

•  Clear,)effec9ve)summary)) •  “seeming)to)defy)the)pen)of)the)historian)by) its)brutal)eloquence,”)E.J.)Marey)

Minard’s)Data)

•  Size)of)Army) •  Date)) •  Loca9on)

– La9tude) – Longitude)

•  Temperature) •  Direc9on)(advance/retreat))

Minard’s)Napoleon’s)March)