01 intro

35
Hadley Wickham Stat405 Statistical computing & graphics Monday, 23 August 2010

Upload: hadley-wickham

Post on 13-May-2015

1.310 views

Category:

Documents


1 download

TRANSCRIPT

Hadley Wickham

Stat405Statistical computing & graphics

Monday, 23 August 2010

1. Introductions

2. Syllabus

3. Introduction to linux

4. Introduction to R

5. Basic graphics

Monday, 23 August 2010

HadleyHELLO

my name is

Monday, 23 August 2010

had.co.nz/stat405(if you can’t remember just google stat405)

[email protected]

Monday, 23 August 2010

About me

From New Zealand

Divisional advisor for McMurtry

Major advisor for statistics

Monday, 23 August 2010

Syllabus

Monday, 23 August 2010

Computing environment

Lab computers: linuxYour computers: mac and windows

Essential tools: R, text editor, latex

Use of command line strongly encouraged.

Will show basic set up for mac, windows and linux.

Monday, 23 August 2010

Lab access

Once registrations are finalised, I’ll get everyone lab access. (But can use R on any computer on campus - including your own)

Monday, 23 August 2010

Setup

Find the instructions related to your operating system on the class website. Follow them to get R and running

I’ll circulate and make sure everyone gets set up right.

Monday, 23 August 2010

Introduction to R

Monday, 23 August 2010

Learning a newlanguage is hard!

Monday, 23 August 2010

Scatterplot basicsinstall.packages("ggplot2")library(ggplot2)

?mpghead(mpg)str(mpg)summary(mpg)

qplot(displ, hwy, data = mpg)

Monday, 23 August 2010

Scatterplot basicsinstall.packages("ggplot2")library(ggplot2)

?mpghead(mpg)str(mpg)summary(mpg)

qplot(displ, hwy, data = mpg)

Always explicitly specify the data

Monday, 23 August 2010

displ

hwy

15

20

25

30

35

40

●●

●●

●● ●●

● ●

● ●

●●

●●

● ●

●●

●●

● ●

●●

● ●

● ●

●●●

●●

●●

●●

●●

●●●

● ●

● ●

●●

● ●

●●

● ●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●● ●●

●●●

●● ●

2 3 4 5 6 7

qplot(displ, hwy, data = mpg)Monday, 23 August 2010

Additional variables

Can display additional variables with aesthetics (like shape, colour, size) or facetting (small multiples displaying different subsets)

Monday, 23 August 2010

displ

hwy

15

20

25

30

35

40

●●

●●

●● ●●

●●

●●

●●

●●

●● ●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●● ●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●● ●●

● ●

●● ●

● ●

● ●

● ●

● ●

●●●

●●

2 3 4 5 6 7

class● 2seater● compact● midsize● minivan● pickup● subcompact● suv

qplot(displ, hwy, colour = class, data = mpg)Monday, 23 August 2010

displ

hwy

15

20

25

30

35

40

●●

●●

●● ●●

●●

●●

●●

●●

●● ●●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●● ●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●● ●●

● ●

●● ●

● ●

● ●

● ●

● ●

●●●

●●

2 3 4 5 6 7

class● 2seater● compact● midsize● minivan● pickup● subcompact● suv

Legend chosen and displayed automatically.

qplot(displ, hwy, colour = class, data = mpg)Monday, 23 August 2010

Your turn

Experiment with colour, size, and shape aesthetics.

What’s the difference between discrete or continuous variables?

What happens when you combine multiple aesthetics?

Monday, 23 August 2010

Discrete Continuous

Colour

Size

Shape

Rainbow of colours

Gradient from red to blue

Discrete size steps

Linear mapping between radius

and value

Different shape for each

Doesn’t work

Monday, 23 August 2010

Faceting

Small multiples displaying different subsets of the data.

Useful for exploring conditional relationships. Useful for large data.

Monday, 23 August 2010

Your turnqplot(displ, hwy, data = mpg) + facet_grid(. ~ cyl)

qplot(displ, hwy, data = mpg) + facet_grid(drv ~ .)

qplot(displ, hwy, data = mpg) + facet_grid(drv ~ cyl)

qplot(displ, hwy, data = mpg) + facet_wrap(~ class)

Monday, 23 August 2010

Summary

facet_grid(): 2d grid, rows ~ cols, . for no split

facet_wrap(): 1d ribbon wrapped into 2d

Monday, 23 August 2010

Aside: workflow

Keep a copy of the slides open so that you can copy and paste the code.

For complicated commands, write them in gedit and then copy and paste.

Monday, 23 August 2010

cty

hwy

15

20

25

30

35

40

● ●

● ●

● ●●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●

●● ●

●●

●●

●●

●●

● ●

●●

● ●

●●●●

● ●

●●

●●

● ●

●●

● ●

● ●

●●●●

● ●●

● ●●

10 15 20 25 30 35qplot(cty, hwy, data = mpg)

What’s the problem with this plot?

Monday, 23 August 2010

cty

hwy

15

20

25

30

35

40

●●

● ●

● ●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

● ●

●●

●●

● ●

●●

●●

● ●

●●●

● ●●

● ●●

10 15 20 25 30 35qplot(cty, hwy, data = mpg, geom = "jitter")Monday, 23 August 2010

cty

hwy

15

20

25

30

35

40

●●

● ●

● ●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

● ●

●●

●●

● ●

●●

●●

● ●

●●●

● ●●

● ●●

10 15 20 25 30 35qplot(cty, hwy, data = mpg, geom = "jitter")

geom controls “type” of plot

Monday, 23 August 2010

class

hwy

15

20

25

30

35

40

●●

●●

●●●●

●●

●●

●●

●●

●●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●●

●●

●●●

●●

●●

●●

●●

●●●

●●

2seater compact midsize minivan pickup subcompact suv

qplot(class, hwy, data = mpg)Monday, 23 August 2010

class

hwy

15

20

25

30

35

40

●●

●●

●●●●

●●

●●

●●

●●

●●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●●

●●

●●●

●●

●●

●●

●●

●●●

●●

2seater compact midsize minivan pickup subcompact suv

qplot(class, hwy, data = mpg)

How could we improve this plot?

Brainstorm for 1 minute.

Monday, 23 August 2010

reorder(class, hwy)

hwy

15

20

25

30

35

40

●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●● ●

●●

●●

●●●

●●●●

● ●●

●●

●●●●

●●

●●

●●

●●

●●●●

pickup suv minivan 2seater midsize subcompact compact

Monday, 23 August 2010

reorder(class, hwy)

hwy

15

20

25

30

35

40

●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●● ●

●●

●●

●●●

●●●●

● ●●

●●

●●●●

●●

●●

●●

●●

●●●●

pickup suv minivan 2seater midsize subcompact compact

qplot(reorder(class, hwy), hwy, data = mpg)

Incredibly useful technique!

Monday, 23 August 2010

reorder(class, hwy)

hwy

15

20

25

30

35

40

●●

●●

● ●

●●

●●

● ●

● ●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●●●

●●● ●

●●

●●

●●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

pickup suv minivan 2seater midsize subcompact compactqplot(reorder(class, hwy), hwy, data = mpg, geom = "jitter")Monday, 23 August 2010

reorder(class, hwy)

hwy

15

20

25

30

35

40

●●●

●●

pickup suv minivan 2seater midsize subcompact compactqplot(reorder(class, hwy), hwy, data = mpg, geom = "boxplot")Monday, 23 August 2010

reorder(class, hwy)

hwy

15

20

25

30

35

40

●●

●●

●●

●●

●●

● ●

● ●

●●

●●●

● ●●

●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

● ●

● ●

●●●

● ●

●●

● ●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

pickup suv minivan 2seater midsize subcompact compactqplot(reorder(class, hwy), hwy, data = mpg, geom = c("jitter", "boxplot"))Monday, 23 August 2010

Your turn

Read the help for reorder. Redraw the previously plots with class ordered by median hwy.

How would you put the jittered points on top of the boxplots?

Monday, 23 August 2010

Aside: coding strategy

At the end of each interactive session, you want a summary of everything you did. Two options:

1. Save everything you did with savehistory() then remove the unimportant bits.

2. Build up the important bits as you go. (this is how I work)

Monday, 23 August 2010