Graphics in R STAT 133 Gaston Sanchez Department of Statistics, - - PowerPoint PPT Presentation

graphics in r
SMART_READER_LITE
LIVE PREVIEW

Graphics in R STAT 133 Gaston Sanchez Department of Statistics, - - PowerPoint PPT Presentation

Graphics in R STAT 133 Gaston Sanchez Department of Statistics, UCBerkeley gastonsanchez.com github.com/gastonstat/stat133 Course web: gastonsanchez.com/stat133 Base Graphics 2 Graphics in R Traditional Graphics R


slide-1
SLIDE 1

Graphics in R

STAT 133 Gaston Sanchez

Department of Statistics, UC–Berkeley gastonsanchez.com github.com/gastonstat/stat133 Course web: gastonsanchez.com/stat133

slide-2
SLIDE 2

Base Graphics

2

slide-3
SLIDE 3

Graphics in R

Traditional Graphics

◮ R "graphics" follows a static, ”painting on canvas”

model.

◮ Graphics elements are drawn, and remain visible until

painted over.

◮ For dynamic and/or interactive graphics, R is limited. 3

slide-4
SLIDE 4

Traditional Graphics

Traditional graphics model

In the traditional model, we create a plot by first calling a high-level function that creates a complete plot, and then we call low-level functions to add more output if necessary

4

slide-5
SLIDE 5

Dataset mtcars

head(mtcars) ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 1 4 4 ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 1 4 4 ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 3 1 ## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 3 2 ## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 3 1 5

slide-6
SLIDE 6

Scatter plot

# simple scatter-plot plot(mtcars$mpg, mtcars$hp)

  • 10

15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

6

slide-7
SLIDE 7

Axis Labels

# xlab and ylab plot(mtcars$mpg, mtcars$hp, xlab = "miles per gallon", ylab = "horsepower")

  • 10

15 20 25 30 50 150 250 miles per gallon horsepower

7

slide-8
SLIDE 8

Title and subtitle

# title and subtitle plot(mtcars$mpg, mtcars$hp, xlab = "miles per gallon", ylab = "horsepower", main = "Simple Scatterplot", sub = 'data matcars')

  • 10

15 20 25 30 50 150 250

Simple Scatterplot

data matcars miles per gallon horsepower

8

slide-9
SLIDE 9

x and y coordinate ranges

# 'xlim' and 'ylim' plot(mtcars$mpg, mtcars$hp, xlim = c(10, 35), ylim = c(50, 400))

  • 10

15 20 25 30 35 50 150 250 350 mtcars$mpg mtcars$hp

9

slide-10
SLIDE 10

Type

# using 'type' (e.g. lines) plot(mtcars$mpg, mtcars$hp, type = "l")

10 15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

10

slide-11
SLIDE 11

Points

# character expansion 'cex' # and 'point character' plot(mtcars$mpg, mtcars$hp, cex = 1.5, pch = 1)

  • 10

15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

11

slide-12
SLIDE 12

Point symbols (pch) available in R

  • 1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 12

slide-13
SLIDE 13

Point Character

# 'pch' can be any character plot(mtcars$mpg, mtcars$hp, pch = "@")

@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 10 15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

13

slide-14
SLIDE 14

Point Character

# 'pch' symbols will be recycled plot(mtcars$mpg, mtcars$hp, pch = 1:25)

  • 10

15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

14

slide-15
SLIDE 15

Point Colors

# color argument 'col' plot(mtcars$mpg, mtcars$hp, pch = 19, col = "blue", cex = 1.2)

  • 10

15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

15

slide-16
SLIDE 16

Coloring Point Symbols

◮ the col argument can be used to color symbols ◮ symbols 21 through 25 can additionally have their interiors

filled by using the bg (background) argument

16

slide-17
SLIDE 17

Coloring Point symbols

  • 1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 17

slide-18
SLIDE 18

So far ...

# using plot() plot(mtcars$mpg, mtcars$hp, xlim = c(10, 35), ylim = c(50, 400), xlab = "miles per gallon", ylab = "horsepower", main = "Simple Scatterplot", sub = 'data matcars', pch = 1:25, cex = 1.2, col = "blue")

18

slide-19
SLIDE 19
  • 10

15 20 25 30 35 50 100 150 200 250 300 350 400

Simple Scatterplot

data matcars miles per gallon horsepower 19

slide-20
SLIDE 20

Low-Level Functions

20

slide-21
SLIDE 21

High and Low level functions

◮ Usually we call a high-level function ◮ Most times we change the default arguments ◮ Then we call low-level functions 21

slide-22
SLIDE 22

Scatter plot

# simple scatter-plot plot(mtcars$mpg, mtcars$hp) # adding text text(mtcars$mpg, mtcars$hp, labels = rownames(mtcars)) # dummy legend legend("topright", legend = "a legend") # graphic title title("Miles Per Galon -vs- Horsepower")

22

slide-23
SLIDE 23
  • 10

15 20 25 30 50 100 150 200 250 300 mtcars$mpg mtcars$hp Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout Valiant Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla Toyota Corona Dodge Challenger AMC Javelin Camaro Z28 Pontiac Firebird Fiat X1−9 Porsche 914−2 Lotus Europa Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E a legend

Miles Per Galon −vs− Horsepower

23

slide-24
SLIDE 24

Scatter plot

# simple scatter-plot plot(mtcars$mpg, mtcars$hp, type = "n", xlab = "miles per gallon", ylab = "horsepower") # grid lines abline(v = seq(from = 10, to = 30, by = 5), col = 'gray') abline(h = seq(from = 50, to = 300, by = 50), col = ' gray') # plot points points(mtcars$mpg, mtcars$hp, pch = 19, col = "blue") # plot text text(mtcars$mpg, mtcars$hp, labels = rownames(mtcars), pos = 4, col = "gray50") # graphic title title("Miles Per Galon -vs- Horsepower")

24

slide-25
SLIDE 25

10 15 20 25 30 50 100 150 200 250 300 miles per gallon horsepower

  • Mazda RX4

Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout Valiant Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental Chrysler Imperial Fiat 128 Honda Civic To Toyota Corona Dodge Challenger AMC Javelin Camaro Z28 Pontiac Firebird Fiat X1−9 Porsche 914−2 Lotus Europa Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E

Miles Per Galon −vs− Horsepower

25

slide-26
SLIDE 26

Test your knowledge

The function plot()

A) accepts any type of vector B) is a generic function C) works only for 1-D and 2-D objects D) is designed to display scatterplots and boxplots

26

slide-27
SLIDE 27

Low-level functions

Function Description points() points lines() connected line segments abline() straight lines across a plot segments() disconnected line segments arrows() arrows rect() rectangles polygon() polygons text() text symbols() various symbols legend() legends

27

slide-28
SLIDE 28

Drawing Points with points()

points(x, y, pch = int, col = str)

◮ pch integer or string indicating type of point character ◮ col color of points 28

slide-29
SLIDE 29

# drawing points plot(mtcars$mpg, mtcars$hp, type = "n") points(mtcars$mpg, mtcars$hp) 10 15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

  • 29
slide-30
SLIDE 30

Connected Line Segments

lines(x, y, lty = str, lwd = num, col = str)

◮ lty specifies the line texture. It should be one of "blank"

(0), "solid" (1), "dashed"(2), "dotted" (3), "dotdash" (4), "longdash" (5) or "twodash" (6).

◮ lwd and col specify the line width and colour 30

slide-31
SLIDE 31

# connected lines plot(mtcars$mpg, mtcars$hp, type = "n") lines(mtcars$mpg, mtcars$hp, type = "s", lwd = 2) 10 15 20 25 30 50 150 250 mtcars$mpg mtcars$hp 31

slide-32
SLIDE 32

Line Graph Options

The type argument can be used to produced other types of lines

◮ type = "l" line graph ◮ type = "s" step function (horizontal first) ◮ type = "S" step function (vertical first) ◮ type = "h" high density (needle) plot ◮ type = "p" draw points ◮ type = "b" draw points and lines ◮ type = "o" over-plotting points and lines 32

slide-33
SLIDE 33

Line Graph Options

type = 'l' type = 's' type = 'S' type = 'h'

33

slide-34
SLIDE 34

Connected Line Segments

x <- 2005:2015 y <- c(81, 83, 84.3, 85, 85.4, 86.5, 88.3, 88.6, 90.8, 91.1, 91.3) plot(x, y, type = 'n', xlab = "Time", ylab = "Values") lines(x, y, lwd = 2) title(main = "Line Graph Example")

34

slide-35
SLIDE 35

Connected Line Segments

2006 2008 2010 2012 2014 82 84 86 88 90 Time Values

Line Graph Example

35

slide-36
SLIDE 36

Drawing Straight Lines

abline(a = intercept, b = slope) abline(h = numbers) abline(v = numbers)

◮ The a / b form specifies a line in intercept / slope form ◮ h specifies horizontal lines at given y-values ◮ v specifies vartical lines at given x-values 36

slide-37
SLIDE 37

# drawing straight lines plot(mtcars$mpg, mtcars$hp, type = "n") abline(v = seq(10, 30, by = 5), h = seq(50, 300, by = 50)) points(mtcars$mpg, mtcars$hp, pch = 19, col = "red") 10 15 20 25 30 50 150 250 mtcars$mpg mtcars$hp

  • 37
slide-38
SLIDE 38

Drawing Disconnected Lines

Disconnected lines can be drawn with the function: segments(x0, y0, x1, y1)

◮ The x0, y0, x1, y1 arguments give the start and end

coordinates of the segments.

◮ Line texture, colour and width arguments can also be given. 38

slide-39
SLIDE 39

Drawing Line Segments

n <- 11 theta <- seq(0, 2 * pi, length = n + 1)[1:n] x <- sin(theta) y <- cos(theta) v1 <- rep(1:n, n) v2 <- rep(1:n, rep(n, n)) plot(x, y, type = 'n') segments(x[v1], y[v1], x[v2], y[v2])

39

slide-40
SLIDE 40

−1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0 x y 40

slide-41
SLIDE 41

Drawing Polygons

Polygons can be drawn with the function: polygon(x, y, col = str, border = str)

◮ x, y give the coordinates of the polygon vertexes. NA

values separate polygons.

◮ col specifies the color of the interior. ◮ border specifies the color of the border. ◮ line texture and width specifications can also be given 41

slide-42
SLIDE 42

Drawing Polygons

10 20 30 40 0.00 0.02 0.04 0.06

Kernel Density Curve

N = 32 Bandwidth = 2.477 Density

42

slide-43
SLIDE 43

Adding Text

We can add text using the function: text(x, y, labels, ...)

◮ x, y give the coordinates of the text. ◮ labels gives the actual text strings. ◮ font optional font for the text. ◮ col optional color for the text. ◮ srt rotation of the text. ◮ adj justification of the text. 43

slide-44
SLIDE 44

Drawing Text

plot(0.5, 0.5, xlim = c(0, 1), ylim = c(0, 1), type = 'n') abline(h = c(.2, .5, .8), v = c(.5, .2, .8), col = "lightgrey") text(0.5, 0.5, "srt = 45, adj = c(.5, .5)", srt = 45, adj = c(.5, .5)) text(0.5, 0.8, "adj = c(0, .5)", adj = c(0, .5)) text(0.5, 0.2, "adj = c(1, .5)", adj = c(1, .5)) text(0.2, 0.5, "adj = c(1, 1)", adj = c(1, 1)) text(0.8, 0.5, "adj = c(0, 0)", adj = c(0, 0))

44

slide-45
SLIDE 45

Drawing Text

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.5 0.5 srt = 45, adj = c(.5, .5) adj = c(0, .5) adj = c(1, .5) adj = c(1, 1) adj = c(0, 0)

45

slide-46
SLIDE 46

Adding a Legend

Legends can be added with: legend(xloc, yloc, legend = text lty = linetypes, lwd = linewidths, pch = glyphname, col = colours, xjust = justification, yjust = justification)

◮ xloc and yloc give the coordinates where the legend is to

be placed

◮ xjust and yjust give the justification of the legend box

with respect to the location.

46

slide-47
SLIDE 47

Adding Legends

# coords of exact line plot(mtcars$mpg, mtcars$hp) legend("topright", legend = "A legend")

  • 10

15 20 25 30 50 100 150 200 250 300 mtcars$mpg mtcars$hp A legend

47

slide-48
SLIDE 48

Plots from scratch

48

slide-49
SLIDE 49

Customizing Annotations

It is also possible to create a plot from scratch. Although this procedure is less documented, it is extremely flexible:

  • 1. call plot.new() to start a new plot frame
  • 2. call plot.window() to define coordinates
  • 3. then call low-level functions:
  • 4. typical options involve axis()
  • 5. then title() (title, subtitle)
  • 6. after that call other function: e.g. points(), lines(), etc

49

slide-50
SLIDE 50

Plot from Scratch

plot.new() plot.window(xlim = c(0, 10), ylim = c(-2, 4), xaxs = "i") axis(1, col.axis = "grey30") axis(2, col.axis = "grey30", las = 1) title(main = "Main Title", col.main = "tomato", sub = "Plot Subtitle", col.sub = "orange", xlab = "x-axis", ylab = "y-axis", col.lab = "blue", font.lab = 3) box("figure", col = "grey90")

50

slide-51
SLIDE 51

Plot from Scratch

2 4 6 8 10 −2 −1 1 2 3 4

Main Title

Plot Subtitle x−axis y−axis

51

slide-52
SLIDE 52

Another plot from scratch

set.seed(5) x <- rnorm(200) y <- x + rnorm(200) plot.new() plot.window(xlim = c(-4.5, 4.5), xaxs = "i", ylim = c(-4.5, 4.5), yaxs = "i") z <- lm(y ~ x) abline(h = -4:4, v = -4:4, col = "lightgrey") abline(a = coef(z)[1], b = coef(z)[2], lwd = 2, col = "red") points(x, y) axis(1) axis(2, las = 1) box() title(main = "A Fitted Regression Line")

52

slide-53
SLIDE 53
  • −4

−2 2 4 −4 −2 2 4

A Fitted Regression Line 53

slide-54
SLIDE 54

Creating a Plot from Scratch

◮ Start a new plot with plot.new() ◮ plot.new() opens a new (empty) plot frame ◮ plot.new() chooses a default plotting region 54

slide-55
SLIDE 55

Setting Up Coordinates

Then use plot.window() to set up the coordinate system for the plotting frame

# axis limits (0,1)x(0,1) plot.window(xlim = c(0, 1), ylim = c(0, 1))

By default plot.window() produces axis limits which are expanded by 6% over those actually specified.

55

slide-56
SLIDE 56

Setting Up Coordinates

The default limits expansion can be turned-off by specifying xaxs = "i" and/or yaxs = "i"

plot.window(xlim, ylim, xaxs = "i")

56

slide-57
SLIDE 57

Aspect Ratio Control

Another important argument is asp, which allows us to specify the aspect ratio

plot.window(xlim, ylim, xaxs = "i", asp = 1)

asp = 1 means that unit steps in the x and y directions produce equal distances in the x and y directions on the plot. (Important to avoid distortion of circles that look like ellipses)

57

slide-58
SLIDE 58

Drawing Axes

The axis() function can be used to draw axes at any of the four sides of a plot.

◮ side=1 below the graph ◮ side=2 to the left of the graph ◮ side=3 above the graph ◮ side=4 to the right of the graph 58

slide-59
SLIDE 59

Customizing Axes

Axes can be customized via several arguments (see ?axis)

◮ location of tick-marks ◮ labels of axis ◮ colors ◮ sizes ◮ text fonts ◮ text orientation 59

slide-60
SLIDE 60

Plot Annotation

The function title() allows us to include labels in the margins

◮ main main title above the graph ◮ sub subtitle below the graph ◮ xlab label for the x-axis ◮ ylab label for the y-axis 60

slide-61
SLIDE 61

Customizing Annotations

The annotations can be customized with additional arguments for the fonts, colors, and size (expansion)

◮ font.main, col.main, cex.main ◮ font.sub, col.sub, cex.sub ◮ font.lab, col.lab, cex.lab 61

slide-62
SLIDE 62

Drawing Arrows

Arrows can be drawn with the function: arrows(x0, y0, x1, y1, code = int, length = num, angle = num)

◮ The x0, y0, x1, y1 arguments give the start and end

coordinates.

◮ code=1 head at the start, code=2 head at the end,

code=3 head at both ends

◮ length of the arrow head and angle to the shaft 62

slide-63
SLIDE 63

Drawing Arrows

plot.new() plot.window(xlim = c(0, 1), ylim = c(0, 1)) arrows(.05, .075, .45, .9, code = 1) arrows(.55, .9, .95, .075, code = 2) arrows(.1, 0, .9, 0, code = 3) text(.5, 1, "A", cex = 1.5) text(0, 0, "B", cex = 1.5) text(1, 0, "C", cex = 1.5)

63

slide-64
SLIDE 64

Drawing Arrows

A B C

64

slide-65
SLIDE 65

Drawing Rectangles

Rectangles can be drawn with the function: rect(x0, y0, x1, y1, col = str, border = str)

◮ x0, y0, x1, y1 give the coordinates of diagonally

  • pposite corners of the rectangles.

◮ col specifies the color of the interior. ◮ border specifies the color of the border. 65

slide-66
SLIDE 66

Drawing Rectangles

# barplot manually constructed plot.new() plot.window(xlim = c(0, 5), ylim = c(0, 10)) rect(0:4, 0, 1:5, c(7, 8, 4, 3), col = "turquoise", border = "white") axis(1) axis(2, las = 1)

66

slide-67
SLIDE 67

Drawing Rectangles

1 2 3 4 5 2 4 6 8 10 67

slide-68
SLIDE 68

Plot Regions

68

slide-69
SLIDE 69

Anatomy of Plot Frame and Region

margin 3 margin 1 margin 2 margin 4

Plot Region

69

slide-70
SLIDE 70

Adjusting the Margins

Margins can be adjusted with the par() function in various ways:

◮ In inches: par(mai = c(2, 2, 1, 1)) ◮ In lines of text: par(mar = c(4, 4, 2, 2)) ◮ Width and Height in inches: par(pin = c(5, 4)) 70

slide-71
SLIDE 71

One more scatter plot

# simple scatter-plot

  • p <- par(mar = c(5, 4, 3, 1))

plot(mtcars$mpg, mtcars$hp, type = "n", xlab = "miles per gallon", ylab = "horsepower") # grid lines abline(v = seq(from = 10, to = 30, by = 5), col = 'gray') abline(h = seq(from = 50, to = 300, by = 50), col = ' gray') # points points(mtcars$mpg, mtcars$hp, pch = 19, col = "blue") # text (point labels) text(mtcars$mpg, mtcars$hp, labels = rownames(mtcars), pos = 4, col = "gray50") # title title("Miles Per Galon -vs- Horsepower") # reset graphical margins par(op)

71

slide-72
SLIDE 72

10 15 20 25 30 50 100 150 200 250 300 miles per gallon horsepower

  • Mazda RX4

Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout Valiant Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental Chrysler Imperial Fiat 128 Honda Civic To Toyota Corona Dodge Challenger AMC Javelin Camaro Z28 Pontiac Firebird Fiat X1−9 Porsche 914−2 Lotus Europa Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E

Miles Per Galon −vs− Horsepower

72