3
Graphing
The primary and most general function for graphing is plot and there are special functions for statistics related graphing commands such as barplot, boxplot, dotchart, hist, and pie. The options that control the elements of plotgenerally work for the other graphing functions and so it makes sense to begin with plot. We note that curve is another functions for graphing that may be a more natural choice for mathematicians as it is used to graph real valued functions, which was used extensively in Chapter 2. Our first set of examples will provide an orientation to theplot command and the graphing frame. The goal is to orientate us to the graphing frame, the margin, and the outer margin. One could get away with skipping these three examples and referring back to them as needed. On the other hand, we illustrate a multitude of options while demonstrating the structure R has in place for creating exceptional graphs.
The example begins by defining the two vectors x0 and y0, which are the coordinates of the four corners of the blue dashed square. The beginning of theplotfunction has the vectors x0 and y0 to providexandycoordinates for plotting. The point (1,1) is listed twice in the vectors because plot will draw lines from one point to the next and without the repeated (1,1) the bottom line of the square would be missing. The plot type is o, which plots the points and also connects them. The next two options, pch and cex, define the type of points to plot and the size with cex=3 increasing the point size by 300%.
Similarly, lty and lwd define the type of line and the thickness. The options xlim=c(0,5), ylim=c(0,5) set the axis ranges, while xlab and ylab set axis captions and main provides a title for the graph. Lastly, we set col=“blue” so that the graph is in blue. Note how text items are in quotations. The axis tick marks and numbering are created by default.
R Code
> x0=c(1,1,3,3,1)
> y0=c(1,3,3,1,1)
> plot(x0,y0,type="o",pch=12,cex=3,lwd=5,lty=2, xlim=c(0,5),ylim=c(0,5),xlab="x-axis",ylab=
"y-axis",col="blue",main="An Example")
> x1=c(2,2,4,4)
> y1=c(2,4,4,2)
29
> x2=c(2,4,4,2)
> y2=c(4,4,2,2)
> segments(x1,y1,x2,y2,lwd=10,col="darkorchid")
> curve(2+sqrt(4-(x-2)^2),2,4,col="red",lwd=10, add=TRUE)
> p=par("usr")
> text(p[1], p[3],"BL",adj=c(0,0),col="darkorchid")
> text(p[2], p[4],"RT",adj=c(1,1),col="darkorchid", font=2)
> text(mean(p[1:2]),mean(p[3:4]),expression(infinity), cex=3,adj=0.5)
0 1 2 3 4 5
012345
An Example
x−axis
y−axis
∞
BL
RT
The next object created is the purple, or darkorchid, solid square. This will be done with thesegmentsfunction, which will draw a line segment from one point to another. The four vectors x1,y1,x2, and y2 define the line segments to be drawn for the square. For example, segments draws a line from (2,2)
31 to (2,4), which are the first values in the four vectors. The next two options insegments, lwd and col, set the width and color of the lines. Notice that lty was omitted and has a default value of 1, which is a solid line.
We now add the arc or quarter of a circle with the curve function. The first element is the function to graph, in this case a circle of radius 2 centered at (2,2). The next two values define the domain ofxfor plotting, in this case fromx= 2 tox= 4. Thecurve function is a stand alone command; in other words it doesn’t need a previous plot to be created to be used. In this case the curve is added to an original plot and the plot domain has been defined.
Generally,curve is a good choice for mathematics since it graphs functions.
Finally, add=TRUE adds the curve or function to the existing graph.
We now add three text objects, BL, RT, and ∞, to the graph. In order to place the objects in their location we use thepar(“usr”)command. A four element vector (v1, v2, v3, v4) is returned by par(“usr”) and set to p, where (v1, v3) is the bottom left corner of the graph and (v2, v4) is the top right corner of the graph. The first text command placed BL at (p[1],p[3]) which is the left bottom corner of the plot region. The adj=c(0,0) command justifies the letters BL to the left and bottom of the location where it is written. The letters BL, for bottom left, are colored purple, or officially darkorchid. Similarly we place RT at the right top of the plot region with coordinates (p[2],p[4]), which is the location of the right top of the graph. The text is justified with the adj=c(1,1) command at right/top of the point (p[2],p[4]) and colored darkorchid. The option font=2 boldfaces the text. One last symbol was placed on the graph.
The new option here isexpression, which allows for typesetting mathematical objects, in this case the infinity symbol. Note howmeanwas used twice to get the center of the graph; the infinity symbol was scaled by 300% with cex=3 and center justified with adj=0.5.
Our next example focuses on the margin. Three of the first four lines define the vectors x0 and y0, and plot the box as in the first example. The second line is apar function. We set the four sides of the margin in the order of bottom, left, top, and right with mar=c(4,4,3,3) which provides 4,4,3, and 3, lines for the four sides respectively, around the margin. We already have text in the margin due to the plot command, which added x-axis, y-axis, and An Example.
After plot the fourmtext, margin text, functions place line 0 through line 3 in red in the bottom margin. The desired text is in quotations and side=1 places the text in the bottom margin, whereas 2, 3, and 4 would place the text on the left, top, and right margins respectively. The third option, line=, places the text in the desired line. Recall thatpar(mar=c(4,4,3,3))provided for 4 lines of margin along the bottom. Notice how line 0 aligns with the tick marks, line 1 with the axis numbers, line 2 with a space below the axis numbers, and line 3 with the axis caption. The fourth option, adj, justifies text to its location with 0 left/bottom, and 1 right/top, which places “Line 0” and “Line 2” on the right and the other two on the left. The last option colors the text red.
R Code
> x0=c(1,1,3,3,1)
> y0=c(1,3,3,1,1)
> par(mar=c(4,4,3,3))
> plot(x0,y0,type="o",pch=12,cex=3,lwd=5,lty=2, xlim=c(0,5),ylim=c(0,5),xlab="x-axis",ylab="y-axis", col="blue",main="An Example")
> mtext("Line 0",side=1,line=0,adj=1,col="red")
> mtext("Line 1",side=1,line=1,adj=0,col="red")
> mtext("Line 2",side=1,line=2,adj=1,col="red")
> mtext("Line 3",side=1,line=3,adj=0,col="red")
> box("figure", col="red")
> p=par("usr")
> text(p[2]+.15,mean(p[3:4]),"The Right Side",srt=-90, xpd=NA,adj=0.5)
> mtext("TRS",side=4,line=2,adj=0.5,las=2)
0 1 2 3 4 5
012345
An Example
x−axis
y−axis
Line 0 Line 1
Line 2 Line 3
The Right Side
TRS
33 We place a box around the margin with box(“figure”,col=“red”). The last three lines place the text on the right margin. Again we define p to be par(“usr”) so that we know the coordinates of the corners to help us place the text using the text function. The first two options of text define the coordinates for the text. Anxcoordinate of p[2]+0.15 places the text outside the graph since p[2] is the right-most value of the graph. The y coordinate is the average of p[3] and p[4]. We use themean command where p[3:4] are the values of the vector p from 3 through 4, which in this case is just the third (bottom) and fourth (top) values. The command srt=-90 rotates the text 90 degrees counterclockwise. The command xpd can assume three values, FALSE, TRUE, and NA, which clips the text to the plot region, figure region, and device region respectively. This needs to be set to NA so the text appears past the margin instead of being clipped. The last option adj=0.5 centers the text and c(0.5,0.5) could also have been used. Another way to place similar text on the graph is to usemtext, which is the next command. Here we place the TRS on the fourth side, the right, on the second line with line=2, and centered with adj=0.5. The last option, las=2, places the text perpendicular to the side. Setting las=0 would place the text parallel to the side. Note that the mtext command limits where text can be placed with adj, whereas the textcommand can place text anywhere. Also, with mtextthe orientation of the text is limited to parallel or perpendicular to the margin; we cannot rotate the text as we did withtext.
Our final example focuses on the outer margin . Four of the first five lines are the same as the previous example. We define our vectors x0 and y0 for plotting with plot, and define our margin with par(mar=c(4,4,3,3)). The fourth line,par(oma=c(3,3,3,3)), defines the outer margin with three lines on all sides. The four values represent the four sides of the outer margin in the order of bottom, left, top, and right, respectively. By default the outer margin is set to 0 for all sides. As before we box the margin in red withbox(“figure”, col=“red”), but now we also box the outer margin in blue withbox(“outer”, lwd=5, col=“blue”).
R Code
> x0=c(1,1,3,3,1)
> y0=c(1,3,3,1,1)
> par(mar=c(4,4,3,3))
> par(oma=c(3,3,3,3))
> plot(x0,y0,type="o",pch=12,cex=3,lwd=5,lty=2, xlim=c(0,5),ylim=c(0,5),xlab="x-axis",ylab="y-axis", col="blue",main="An Example")
> box("figure",col="red")
> box("outer",lwd=5,col="blue")
> mtext("Line A",side=3,line=0,adj=0,col="blue", outer=TRUE)
> mtext("Line B",side=3,line=1,adj=0.5,col="blue", outer=TRUE)
> mtext("Line C",side=3,line=2,adj=1,col="blue", outer=TRUE)
0 1 2 3 4 5
012345
An Example
x−axis
y−axis
Line A Line B Line C
The next three uses of mtext are almost exactly the same as those used in the previous example, except outer=TRUE places the text in the outer margin. Recall that par(oma=c(3,3,3,3)) provided for three outer margin lines around the whole graph. With side=3 the text is placed on the top and we set adj to 0, 0.5, and 1 to place the text on the left, center, and right, respectively. The text is colored blue. At this point, it would be a good idea to open R, if you haven’t already done so, and use the code here while changing values to help understand the options. Graphs can be saved by going to File ->Save while the graph window is selected.
Graphing Functions 35