Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to plotting data Fish 552: Lecture 4.

Similar presentations


Presentation on theme: "Introduction to plotting data Fish 552: Lecture 4."— Presentation transcript:

1 Introduction to plotting data Fish 552: Lecture 4

2 Recommended Readings R Graphics –R Graphics: Chapters 1 and 2 (Paul Murrell, 2006) – http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.htmlhttp://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html Only ch. 1 is available free online, but ch. 2 is recommended as well if you have access to the book

3 Graphics in R Two main graphics packages –base graphics changes to layout fairly easy –lattice graphics more structured graphics products, but changes to the basic layout can be more difficult

4 Base Graphics: plot() plot() generic function for plotting R objects –points, lines, points and lines,... > ?plot Download the primates data and read it into R –Notice the row names

5 3 Ways to plot plot(x = primates$Bodywt, y = primates$Brainwt) plot(Brainwt ~ Bodywt, data = primates) attach(primates) plot(x = Brainwt, y = Bodywt) detach(primates)

6

7 xlab(), ylab() By default, R will use the name of the variables as axis labels Add labels to the x and y axes plot(Brainwt ~ Bodywt, data = primates, xlab = “Body Weight (kg)”, ylab = “Brain Weight (g)”)

8

9 xlim(), ylim() By default R chooses x and y limits just larger than the range of your data To change the default x and y limits plot(Brainwt ~ Bodywt, data = primates, xlim = c(0,300), ylim = c(0,1400))

10

11 Use of Colors The color palette when color is specified by a number > palette() [1] "black" "red" "green3" "blue" "cyan“ [6] "magenta" "yellow" "gray" plot(Brainwt ~ Bodywt, data = primates, xlim = c(0,300), ylim = c(0,1400), col = 2)

12

13 In-class exercise 1 Copy and paste these commands into your R script and run them. Try to figure out what’s going on here... plot(Brainwt ~ Bodywt, data = primates, xlim = c(0,300), ylim = c(0,1500), col = 4) plot(Brainwt ~ Bodywt, data = primates, xlim = c(0,300), ylim = c(0,1500), col = "blue") plot(Brainwt ~ Bodywt, data = primates, xlim = c(0,300), ylim = c(0,1500), col = 12) plot(Brainwt ~ Bodywt, data = primates, xlim = c(0,300), ylim = c(0,1500), col = c(2,4))

14 Use of Colors There are 657 colors to choose from colors() point.colors <- c("red", "orange", "green", "blue", "magenta") plot(Brainwt ~ Bodywt,..., col = point.colors)

15

16

17 Point character control pch : point character The default plotting character is an unfilled circle (pch = 1) plot(Brainwt ~ Bodywt,..., pch = 20) cex: character expansion The default expansion is none (cex = 1) plot(Brainwt ~ Bodywt,..., cex = 3)

18

19 More options bg : set background color for devices –Use this option in combination with pch 21-25 plot(Brainwt~Bodywt,..., bg = point.colors, pch = 21)

20

21 Plot generated with code from: ?points

22 Graphing parameters: par() These graphing parameters apply to all graphs called after they are set, until the graphing parameters are changed. This allows you to only have to write the command once to apply to multiple graphs. Show help for par (?par) List all par options > par() $xlog [1] FALSE..

23 Graphing parameters: par() Save default par values > old.par <- par() Example par(cex = 1.25) Restore defaults > par(oldPar) Warning messages: 1: In par(oldPar) : graphical parameter "cin" cannot be set 2: In par(oldPar) : graphical parameter "cra" cannot be set 3: In par(oldPar) : graphical parameter "csi" cannot be set 4: In par(oldPar) : graphical parameter "cxy" cannot be set 5: In par(oldPar) : graphical parameter "din" cannot be set R.O. indicates read-only arguments: These may only be used in queries and cannot be set

24 Multiple Point Characters Simple extension of one pch plot(Brainwt~Bodywt,..., pch = 21:25)

25

26 locator() Interactive function Identifies the locations of a point on a graph > locator(1) $x [1] 220.7812 $y [1] 1165.847 loc <- locator(1)

27 legend() Look up help on legend function ( ?legend ) –Many of the options are the same as described in par legend(loc$x, loc$y, legend = rownames(primates), pt.bg = point.colors, pch = 21:25) plot(Brainwt ~ Bodywt,.... legend("topright", legend = rownames(primates), pt.bg = point.colors, pch = 21:25, bty = 'n') no box around legend topright, bottomleft, middle,...

28

29 Axis Properties yaxp / xaxp : control tick marks –c(lower point, upper point, number of intervals between tick marks) plot(Brainwt ~ Bodywt,..., yaxp = c(0,1500,5))

30

31 Hands-on Exercise 2 Try to replicate this graph –You’ll need to figure out how to add a title –Think outside the box with pch –Note the axes

32 Axis properties For more control, try the axis() function –?axis plot(Brainwt ~ Bodywt,…, yaxt = ‘n’, xaxt = ‘n’ ) axis(2, at = c(0,300,600,900,1200,1500), labels = c(0,300,600,900,1200,"BIG")) axis(1, at=c(50,100,150,200,2.... Suppress the x and y axis ticks and labels 1=below, 2=left, 3=above and 4=right

33

34 Labeling: text() Look up help on ?text plot(Brainwt ~ Bodywt, data = primates, text(primates$Bodywt, primates$Brainwt, labels = rownames(primates),pos = 4) adj along with the coordinates allows for more precise placement of labels text(primates$Bodywt, primates$Brainwt, labels = rownames(primates),adj = -0.5) Values of 1, 2, 3 and 4, respectively indicate positions below, to the left, above and to the right of the coordinates Shift to the right coordinates by ½ inch

35

36 Labeling: identify() identify() can be used to label or identify points on a graph –?identify Click near two of the points plot(Brainwt ~ Bodywt,... identify(primates$Bodywt, primates$Brainwt, labels = rownames(primates), n = 2)

37

38 Adding points and lines points() and lines() take many of the same arguments as plot() –These can be added to the current plot ?lines –lty specifies the line type 0 = blank 1 = solid 2 = dashed 3 = dotted 4 = dot-dash 5 = long-dash 6 = two dash

39 Adding points and lines - type specifies the type of plot to be drawn "p" - points, "l" - lines, "b" - both, "c" - lines part alone of "b", "o" - ‘overplotted’, "h" - ‘histogram’ like (or ‘high-density’) vertical lines, "s" - stair steps, "n" for no plotting. lines(x = c(50,100,150,200), y = c(250,400,550,750), type = "b", lwd = 3, lty = 2)

40 Additional points or lines Add lines lines(x = c(50,100,150,200), y = c(250,400,550,750), type ="b", lwd = 3, lty = 2) Add points points(x = c(150,200,250,300), y = c(100,150,200,250), cex = 3, pch = 20)

41

42 Hands-on Exercise 3 The equation for the standard normal density: exp(-x^2)/sqrt(2*pi) Hint: I used type= three separate times


Download ppt "Introduction to plotting data Fish 552: Lecture 4."

Similar presentations


Ads by Google