Introduction to plotting data Fish 552: Lecture 4
Recommended Readings R Graphics –R Graphics: Chapters 1 and 2 (Paul Murrell, 2006) – Only ch. 1 is available free online, but ch. 2 is recommended as well if you have access to the book
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
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
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)
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)”)
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))
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)
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))
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)
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)
More options bg : set background color for devices –Use this option in combination with pch plot(Brainwt~Bodywt,..., bg = point.colors, pch = 21)
Plot generated with code from: ?points
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..
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
Multiple Point Characters Simple extension of one pch plot(Brainwt~Bodywt,..., pch = 21:25)
locator() Interactive function Identifies the locations of a point on a graph > locator(1) $x [1] $y [1] loc <- locator(1)
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,...
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))
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
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
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
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)
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
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)
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)
Hands-on Exercise 3 The equation for the standard normal density: exp(-x^2)/sqrt(2*pi) Hint: I used type= three separate times