graphics in r · basics of graphics in r graphics systems i "graphics" and...

66
Graphics in R STAT 133 Gaston Sanchez Department of Statistics, UC–Berkeley gastonsanchez.com github.com/gastonstat/stat133 Course web: gastonsanchez.com/stat133

Upload: others

Post on 31-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Graphics in RSTAT 133

Gaston Sanchez

Department of Statistics, UC–Berkeley

gastonsanchez.com

github.com/gastonstat/stat133

Course web: gastonsanchez.com/stat133

Page 2: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

R Graphics

2

Page 3: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Understanding Graphics in R

2 main graphics systems

"graphics" & "grid"

3

Page 4: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Basics of Graphics in R

Graphics Systems

I "graphics" and "grid" are the two main graphicssystems in R

I "graphics" is the traditional system, also referred to asbase graphics

I "grid" prodives low-level functions for programmingplotting functions

4

Page 5: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Basics of Graphics in R

Graphics Engine

I Underneath "graphics" and "grid" there is the package"grDevices"

I "grDevices" is the graphics engine in R

I It provides the graphics devices and support for colors andfonts

5

Page 6: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

grid

graphics

grDevices

maps diagram plotrix

ggplot2

lattice

tikzDevice

JavaGD

Cairo

6

Page 7: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Basics of Graphics in R

Package "graphics"

The package "graphics" is the traditional system; it providesfunctions for complete plots, as well as low-level facilities.

Many other graphics packages are built on top of graphics like"maps", "diagram", "pixmap", and many more.

7

Page 8: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Understanding Graphics in R

Package "grid"

The "grid" package does not provide functions for drawingcomplete plots.

"grid" is not used directly to produce statistical plots. Instead,it is used to build other graphics packages like "lattice" or"ggplot2".

8

Page 9: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

In this course

I In this course we’ll focus on the packages "graphics" and"ggplot2"

I "graphics" is the traditional plotting system in R, andmany functions and packages are built on top of it.

I "ggplot2" excels at providing graphics for visualizingmultivariate data sets —in data.frame format—, whiletaking care of many issues for superior visual displays.

9

Page 10: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

R Graphics by Paul Murrell

10

Page 11: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Some Resources

I R Graphics by Paul Murrellbook and webpage

I R Graphics Cookbook by Winston Changhttp://www.cookbook-r.com/Graphs/

I ggplot2: Elegant Graphics for Data Analysis byHadley Wickham

I R Graphs Cookbook by Hrishi Mittal

I Graphics for Statistics and Data Analysis with R byKevin Keen

11

Page 12: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Traditional (Base) Graphics

12

Page 13: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Base Graphics in R

Types of graphics functionsGraphics functions can be divided into two main types:

I high-level functions produce complete plots, e.g.barplot(), boxplot(), dotchart()

I low-level functions add further output to an existing plot,e.g. text(), points(), legend()

13

Page 14: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

The plot() function

I plot() is the most important high-level function intraditional graphics

I The first argument to plot() provides the data to plot

I The provided data can take different forms: e.g. vectors,factors, matrices, data frames.

I To be more precise, plot() is a generic function

I You can create your own plot() method function

14

Page 15: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Basic Plots with plot()

In its basic form, we can use plot() to make graphics of:

I one single variable

I two variables

I multiple variables

15

Page 16: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of One Variable

16

Page 17: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

High-level graphics of a single variable

Function Data Descriptionplot() numeric scatterplotplot() factor barplotplot() 1-D table barplot

numeric can be either a vector or a 1-D array (e.g. row orcolumn from a matrix)

17

Page 18: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

One variable objects

Vector / Factor

row (data.frame)

row (matrix)

1-D table

column (data.frame)

column (matrix)

18

Page 19: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

plot() of one variable

# plot numeric vector

num_vec <- (c(1:10))^2

plot(num_vec)

# plot factor

set.seed(4)

abc <- factor(sample(c('A', 'B', 'C'), 20, replace = TRUE))

plot(abc)

# plot 1D-table

abc_table <- table(abc)

plot(abc_table)

19

Page 20: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

plot() of one variable

●●

2 4 6 8 10

02

04

06

08

01

00

Index

nu

m_

ve

c

A B C

02

46

8

02

46

8

abc

ab

c_

table

A B C

20

Page 21: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

More high-level graphics of a single variable

Function Data Descriptionbarplot() numeric barplotpie() numeric pie chartdotchart() numeric dotplot

boxplot() numeric boxplothist() numeric histogramstripchart() numeric 1-D scatterplotstem() numeric stem-and-leaf plot

21

Page 22: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of one variable

# barplot numeric vector

barplot(num_vec)

# pie chart

pie(1:3)

# dot plot

dotchart(num_vec)

22

Page 23: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of one variable

02

04

06

08

01

00

1

2

3●

0 20 40 60 80 100

23

Page 24: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of one variable

# barplot numeric vector

boxplot(num_vec)

# pie chart

hist(num_vec)

# dot plot

stripchart(num_vec)

# stem-and-leaf

stem(num_vec)

24

Page 25: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

boxplot()

# boxplot

boxplot(iris$Sepal.Length)

4.5

5.0

5.5

6.0

6.5

7.0

7.5

8.0

25

Page 26: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

hist()

# histogram

hist(iris$Sepal.Length)

Histogram of iris$Sepal.Length

iris$Sepal.Length

Fre

quen

cy

4 5 6 7 8

05

1015

2025

30

26

Page 27: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Test your knowledge

What option does not apply to histograms:

A) adjacent bars (no gaps)

B) area of bars indicate proportions

C) bins of equal length

D) bars can be reordered

27

Page 28: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

stripchart()

# strip-chart (1-D scatter plot)

# (for small sample sizes)

stripchart(num_vec)

0 20 40 60 80 100

28

Page 29: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

stem()

# stem-and-leaf plot

# (for small sample sizes)

stem(num_vec)

##

## The decimal point is 1 digit(s) to the right of the |

##

## 0 | 1496

## 2 | 56

## 4 | 9

## 6 | 4

## 8 | 1

## 10 | 0

29

Page 30: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Kernel Density Curve

I Surprisingly, R does not have a specific function to plotdensity curves

I R does have the density() function which computes akernel density estimate

I We can pass a "density" object to plot() in order toget a density curve.

30

Page 31: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Kernel Density Curve

# kernel density curve

dens <- density(num_vec)

plot(dens)

−50 0 50 100 150

0.00

00.

004

0.00

8

density.default(x = num_vec)

N = 10 Bandwidth = 19.41

Den

sity

31

Page 32: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Test your knowledge

What type of plot is based on the five-numbersummary

A) bar chart

B) box plot

C) histogram

D) scatterplot

32

Page 33: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of Two Variables

33

Page 34: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

High-level graphics of two variables

Function Data Descriptionplot() numeric, numeric scatterplotplot() numeric, factor stripchartsplot() factor, numeric boxplotsplot() factor, factor spineplotplot() 2-column numeric matrix scatterplotplot() 2-column numeric data.frame scatterplotplot() 2-D table mosaicplot

34

Page 35: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Two variable objects

2-D table(frequency orcrosstable)

2-column (numeric data.frame)

2-column (numeric matrix)

2 numeric vectorsnum vector, factorfactor, num vector2 factors

35

Page 36: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# plot numeric, numeric

plot(iris$Petal.Length, iris$Sepal.Length)

# plot numeric, factor

plot(iris$Petal.Length, iris$Species)

# plot factor, numeric

plot(iris$Species, iris$Petal.Length)

# plot factor, factor

plot(iris$Species, iris$Species)

36

Page 37: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# plot numeric, numeric

plot(iris$Petal.Length, iris$Sepal.Length)

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●● ●

●●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

1 2 3 4 5 6 7

4.5

5.5

6.5

7.5

iris$Petal.Length

iris

$S

ep

al.L

en

gth

37

Page 38: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# plot numeric, factor

plot(iris$Petal.Length, iris$Species)

●●● ●● ●●●●●●●●●● ●●● ●● ●●● ● ●●●●● ●●●●●●●●●● ●●●● ● ●● ●●●●

●● ●● ●● ●● ●●● ●● ●● ●●● ●● ●● ●●●● ● ●●● ●● ● ●●● ●●●● ● ●●● ●●●●● ●

●● ●● ● ●● ●● ●● ● ●●● ● ● ● ●● ●● ●● ● ●●● ● ● ● ●●● ● ●●●● ● ●●● ●●●● ● ●●

1 2 3 4 5 6 7

1.0

1.5

2.0

2.5

3.0

iris$Petal.Length

iris

$S

pe

cie

s

38

Page 39: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# plot factor, numeric

plot(iris$Species, iris$Petal.Length)

setosa versicolor virginica

12

34

56

7

39

Page 40: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# plot factor, factor

plot(iris$Species, iris$Species)

x

y

setosa versicolor virginica

seto

save

rsic

olor

virg

inic

a

0.0

0.2

0.4

0.6

0.8

1.0

40

Page 41: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# some fake data

set.seed(1)

# hair color

hair <- factor(

sample(c('blond', 'black', 'brown'), 100, replace = TRUE))

# eye color

eye <- factor(

sample(c('blue', 'brown', 'green'), 100, replace = TRUE))

41

Page 42: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# plot factor, factor

plot(hair, eye)

x

y

black blond brown

blue

brow

ngr

een

0.0

0.2

0.4

0.6

0.8

1.0

42

Page 43: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

More high-level graphics of two variables

Function Data Descriptionsunflowerplot() numeric, numeric sunflower scatterplotsmoothScatter() numeric, numeric smooth scatterplot

boxplot() list of numeric boxplotsbarplot() matrix stacked / side-by-side barplotdotchart() matrix dotplot

stripchart() list of numeric stripchartsspineplot() numeric, factor spinogramcdplot() numeric, factor conditional density plot

fourfoldplot() 2x2 table fourfold displayassocplot() 2-D table association plotmosaicplot() 2-D table mosaic plot

43

Page 44: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# sunflower plot (numeric, numeric)

sunflowerplot(iris$Petal.Length, iris$Sepal.Length)

1 2 3 4 5 6 7

4.5

5.5

6.5

7.5

iris$Petal.Length

iris

$S

ep

al.L

en

gth

●●

●●

● ●

●● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●●

44

Page 45: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# smooth scatter plot (numeric, numeric)

smoothScatter(iris$Petal.Length, iris$Sepal.Length)

1 2 3 4 5 6 7

4.5

5.5

6.5

7.5

iris$Petal.Length

iris

$S

ep

al.L

en

gth

45

Page 46: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# boxplots (numeric, numeric)

boxplot(iris$Petal.Length, iris$Sepal.Length)

1 2

12

34

56

78

46

Page 47: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

m <- matrix(1:8, 4, 2)

# barplot (numeric matrix)

barplot(m)

05

10

15

20

25

47

Page 48: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

m <- matrix(1:8, 4, 2)

# barplot (numeric matrix)

barplot(m, beside = TRUE)

02

46

8

48

Page 49: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two variables

# conditional density plot (numeric, factor)

cdplot(iris$Petal.Length, iris$Species)

iris$Petal.Length

iris

$S

pe

cie

s

2 3 4 5 6

seto

savi

rgin

ica

0.0

0.2

0.4

0.6

0.8

1.0

49

Page 50: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Two categorical variables: frequency table

# 2-D table (HairEyeColor data)

x <- margin.table(HairEyeColor, c(1, 2))

x

## Eye

## Hair Brown Blue Hazel Green

## Black 68 20 15 5

## Brown 119 84 54 29

## Red 26 17 14 14

## Blond 7 94 10 16

50

Page 51: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two categorical variables

# mosaic plot (2-D table)

mosaicplot(x, main = "Relation between hair and eye color")

Relation between hair and eye color

Hair

Eye

Black Brown Red BlondB

row

nB

lue

Ha

zel

Gre

en

51

Page 52: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of two categorical variables

# association plot (2-D table)

assocplot(x, main = "Relation between hair and eye color")

Black Brown Red Blond

Gre

en

Blu

eB

row

nRelation between hair and eye color

Hair

Eye

52

Page 53: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of Multiple Variables

53

Page 54: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

High-level graphics of multiple variables

Function Data Descriptionplot() data frame scatterplot matrixpairs() matrix scatterplot matrixmatplot() matrix scatterplotstars() matrix star plots

image() numeric, numeric, numeric image plotcontour() numeric, numeric, numeric contour plotfilled.contour() numeric, numeric, numeric filled contour plotpersp() numeric, numeric, numeric 3-D surfacesymbols() numeric, numeric, numeric symbols scatterplot

mosaicplot() N-D table mosaic plot

54

Page 55: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

# scatter plot matrix (data frame)

plot(iris[ , 1:4])

## Warning: closing unused connection 5

(http://gastonsanchez.com/education.csv)

Sepal.Length

2.0 3.0 4.0

●●

●●

●●

● ●●

●●

●●

● ●●●

●●

●●●

●●

●●

● ●

● ●●

●●

●●

●● ●

●●

●●

●●

●●●

● ●

●●

●●●●

●●●

●●

● ●●

●●

●●

● ●

●●

●●

● ●●

●●

●●●

●●

●●●

●●●

●●

●●

●●●●

●●

●●●●

●●

●●

●●●●

●●

●●●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●●

●●●

0.5 1.5 2.5

4.5

6.0

7.5

●●●●

●●

● ●●

●●

●●● ●●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●● ●

●●

●●

●●

●●●● ●

●●●●●

●●●

●●

●●●

●●

●●

● ●

●●

●●

●●●

●●

●●●

●●

●●

● ●●

●●

●●

2.0

3.0

4.0

●●●

● ●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●● ●

●●

●●

●●●●

●●●

●●●

●●●

●●

●●

●● ●

●●● ●

●●

● ●●

●●

●●

●●

●●●

●● ●●●

●●

●Sepal.Width

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●

●●●

●●●

● ●

●●

●●●

●●● ●

●●

● ●●

●●

●●

●●

●●●

●● ●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●●

●●

●●●

● ●

●●

●●●

●● ●●

●●

●●●

●●

●●

●●

●●●

●● ● ●●

●●

●●●● ●●

● ●● ● ●●●● ●

●●●●● ●●

●●●●●●●● ●● ●●● ●●● ●●●●●●

● ●● ●●

●●●

●●● ●

●●

●●

●●●

●●

●●●●

●●●

●●● ●

●● ● ●

●●●

● ●●

●●● ●

●●●

●●

●●●

●●●●

●●

●●

●●

● ●●

●●

●●

●●

●●●●●

●●●●●●

●● ●● ●●

●●● ● ●●●● ●

●●●●●● ●

●●● ●●●●● ● ●●●

● ●●● ●●● ●●

●● ●● ●●

●●●

●●● ●

●●

●●

●●●

●●

● ●●●

● ●●

●●● ●

●● ●●

●●●

● ●●

● ●●●

●●●

●●

●● ●● ● ●●

●●

●●

● ●

● ●●

●●

●●

●●

●●●●●

●●●● ● ●●

Petal.Length

13

57

●●●●●●

●●●●●●●●●

●●●●●● ●

●●● ●●●●● ●●●●●●●●●●●●

●●

●●●●●

●●●

●●● ●

●●

●●

●●●

●●

●●●●● ●●

●●● ●

●●●●

●●●

● ●●

●●●●

●● ●

●●

●● ●● ●●●

●●

●●

●●

●●●●

●●

●●●●

●● ●

●●

● ●●●● ●

4.5 6.0 7.5

0.5

1.5

2.5

●●●● ●●●

●● ● ●●●● ●●●● ●●

●●

●●●

●●●●●

● ●●● ●●● ●●●

●●●●● ●●

●● ●●

●●

●●

●● ●●

●●

● ●●●

●●

●●●●

●●

●●

●●●●●

●●

●●● ●●

●●

● ●

● ●●

●●●

● ●

●●

● ●●

●●●

● ●●

●●

●●

●●

●●

●●●

●●

●● ●● ●●●

●● ● ●●●● ●●●● ●●

●●

●●●●●●●

●●●● ●●● ●●●

●●●●● ●●

●●●●

●●

●●

●● ●●

●●

●●●●

●●

●●●●

●●

●●

● ●●●●

●●

● ●●●●

●●

●●

● ●●

●●●

● ●

●●

●●●

●● ●

● ●●

●●

● ●

●●

●●

●●

● ●

1 3 5 7

●●●●●●●

●●●●●●●●●●●●●●

●●

●●●●●●●●

●●●●●●●●●●●

●●●

●●●●

●●●●

●●

●●

●● ●●

●●

●●●●

●●

●●●●

●●●●

●●●●●

●●

●●●●●

●●

● ●

● ●●

●●●

●●

●●

● ●●

●●●

●●●

● ●

●●

●●

●●

●●

●●

Petal.Width

55

Page 56: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

# scatter plot matrix (data frame)

pairs(iris[ , 1:4])

Sepal.Length

2.0 3.0 4.0

●●

●●

●●

● ●●

●●

●●

● ●●●

●●

●●●

●●

●●

● ●

● ●●

●●

●●

●● ●

●●

●●

●●

●●●

● ●

●●

●●●●

●●●

●●

● ●●

●●

●●

● ●

●●

●●

● ●●

●●

●●●

●●

●●●

●●●

●●

●●

●●●●

●●

●●●●

●●

●●

●●●●

●●

●●●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●●

●●●

0.5 1.5 2.5

4.5

6.0

7.5

●●●●

●●

● ●●

●●

●●● ●●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●● ●

●●

●●

●●

●●●● ●

●●●●●

●●●

●●

●●●

●●

●●

● ●

●●

●●

●●●

●●

●●●

●●

●●

● ●●

●●

●●

2.0

3.0

4.0

●●●

● ●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●● ●

●●

●●

●●●●

●●●

●●●

●●●

●●

●●

●● ●

●●● ●

●●

● ●●

●●

●●

●●

●●●

●● ●●●

●●

●Sepal.Width

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●

●●●

●●●

● ●

●●

●●●

●●● ●

●●

● ●●

●●

●●

●●

●●●

●● ●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●●

●●

●●●

● ●

●●

●●●

●● ●●

●●

●●●

●●

●●

●●

●●●

●● ● ●●

●●

●●●● ●●

● ●● ● ●●●● ●

●●●●● ●●

●●●●●●●● ●● ●●● ●●● ●●●●●●

● ●● ●●

●●●

●●● ●

●●

●●

●●●

●●

●●●●

●●●

●●● ●

●● ● ●

●●●

● ●●

●●● ●

●●●

●●

●●●

●●●●

●●

●●

●●

● ●●

●●

●●

●●

●●●●●

●●●●●●

●● ●● ●●

●●● ● ●●●● ●

●●●●●● ●

●●● ●●●●● ● ●●●

● ●●● ●●● ●●

●● ●● ●●

●●●

●●● ●

●●

●●

●●●

●●

● ●●●

● ●●

●●● ●

●● ●●

●●●

● ●●

● ●●●

●●●

●●

●● ●● ● ●●

●●

●●

● ●

● ●●

●●

●●

●●

●●●●●

●●●● ● ●●

Petal.Length

13

57

●●●●●●

●●●●●●●●●

●●●●●● ●

●●● ●●●●● ●●●●●●●●●●●●

●●

●●●●●

●●●

●●● ●

●●

●●

●●●

●●

●●●●● ●●

●●● ●

●●●●

●●●

● ●●

●●●●

●● ●

●●

●● ●● ●●●

●●

●●

●●

●●●●

●●

●●●●

●● ●

●●

● ●●●● ●

4.5 6.0 7.5

0.5

1.5

2.5

●●●● ●●●

●● ● ●●●● ●●●● ●●

●●

●●●

●●●●●

● ●●● ●●● ●●●

●●●●● ●●

●● ●●

●●

●●

●● ●●

●●

● ●●●

●●

●●●●

●●

●●

●●●●●

●●

●●● ●●

●●

● ●

● ●●

●●●

● ●

●●

● ●●

●●●

● ●●

●●

●●

●●

●●

●●●

●●

●● ●● ●●●

●● ● ●●●● ●●●● ●●

●●

●●●●●●●

●●●● ●●● ●●●

●●●●● ●●

●●●●

●●

●●

●● ●●

●●

●●●●

●●

●●●●

●●

●●

● ●●●●

●●

● ●●●●

●●

●●

● ●●

●●●

● ●

●●

●●●

●● ●

● ●●

●●

● ●

●●

●●

●●

● ●

1 3 5 7

●●●●●●●

●●●●●●●●●●●●●●

●●

●●●●●●●●

●●●●●●●●●●●

●●●

●●●●

●●●●

●●

●●

●● ●●

●●

●●●●

●●

●●●●

●●●●

●●●●●

●●

●●●●●

●●

● ●

● ●●

●●●

●●

●●

● ●●

●●●

●●●

● ●

●●

●●

●●

●●

●●

Petal.Width

56

Page 57: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

# scatter plot matrix (data frame)

matplot(iris[ , 1:4])

111111

11111111

1111111111111

111111111111

1111111

11

11

111

1

1

11

1

1

11

1111

1

111111111111

1111

1111

11

11111

1111

1

1111

1

11

1

1

111

111

1111

11

1

1

1

1

111

111

111

111

1

111

111

1

111111

1

0 50 100 150

02

46

8

iris

[, 1

:4]

2222

2222222222

22222222222222222

2

22

2222222

2

222

2

2222222

2222

222

2

2

2

2222222

222222222222

22222

2

2222222222

22222222

222

22222222

2

22

2222

2222222

2

2222222222

2222

2222

3333333333333

33333

33333

33333333333333333333

3333333

333

3333

3

3

33

33

3

3

33333

3

3

3333333

3333

33333

33333

3

3333

3

3

3

3

333

3

3

333

3333333

33

3

3

3

3

3

33

33

3333

333333

33333

33333

33

44444444444444444444444

44444444444444444444

4444444

44444444444444444

444

4444444

444444

44444444444444444

4444

44444

44444

44444

4

4444

4444

44444

44

44444444444

4444

57

Page 58: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

# star plot (data frame)

stars(iris[ , 1:4])

58

Page 59: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

# color image (matrix)

image(t(volcano)[ncol(volcano):1, ])

0.0 0.2 0.4 0.6 0.8 1.0

0.0

0.2

0.4

0.6

0.8

1.0

59

Page 60: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

# display of Maunga Whau volcano

x <- 10*(1:nrow(volcano))

y <- 10*(1:ncol(volcano))

image(x, y, volcano, col = terrain.colors(100), axes = FALSE)

contour(x, y, volcano, levels = seq(90, 200, by = 5),

add = TRUE, col = "peru")

axis(1, at = seq(100, 800, by = 100))

axis(2, at = seq(100, 600, by = 100))

box()

title(main = "Maunga Whau Volcano", font.main = 4)

60

Page 61: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables

x

y

95

100

100

100

105

105 105

110

110

110

110

115

115 115

120 125

130

135

140 145

150

155

155

160

160

165

165

170

170 175

180

185

190

100 200 300 400 500 600 700 800

10

02

00

30

04

00

50

06

00

Maunga Whau Volcano

61

Page 62: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables# mosaic plot of N-D tables

mosaicplot(HairEyeColor)

HairEyeColor

Hair

Eye

Black Brown Red BlondB

row

nB

lue

Ha

zel

Gre

en

MaleFemale Male Female MaleFemale Male Female

62

Page 63: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Plots of multiple variables# symbols scatter plots

symbols(iris[, 1], iris[, 2], circles = iris[, 3]/100,

inches = FALSE)

4 5 6 7 8

2.0

3.0

4.0

iris[, 1]

iris

[, 2] ●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

●● ●

●●

● ●

●●

● ●●

●●

●●

●●

●●

●● ●

●●

63

Page 64: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Graphics Parameters

Graphics Functions and ArgumentsI Plot functions usually come with various arguments

I Typically, the first argument(s) is the data object(s) to beplotted

I Most of the other arguments have default options

I Graphic arguments have a consisting naming convention,but there will always be some exception

64

Page 65: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Graphical Parameters

Graphical ArgumentsI Some arguments are specific to a function (e.g. horiz orbeside in barplot())

I Other arguments are more general (e.g. col, xlab, ylab)

I General graphical parameters are listed in thedocumentation of the function par()

I See ?par for more information

65

Page 66: Graphics in R · Basics of Graphics in R Graphics Systems I "graphics" and "grid" are the two main graphics systems in R I "graphics" is the traditional system, also referred to as

Graphics in R

How to choose a graphics approach?I look first for an existing function that does what you want

—or something similar to what you want (don’t reivent thewheel!)

I Existing plotting functions can be combined andcustomized by using optional arguments or graphicalparameters

I For exploratory data analysis (quick and dirty) the plottingfunctions in "graphics" is a good option

66