Graphing Functions 35
−6 −4 −2 0 2 4 6
−1.0−0.50.00.51.0
Function Plot Example
x−axis
y−axis
We have a useful graph, but there are a number of improvements we might like to make. It is typical for the axis to be in the center of the graph like a crosshair and given that we are graphing sin(x) we would prefer to label the x−axis from−2πto 2π. The grid could be improved and it would be nice to label the functions in the graph. We do all this with our next example. We start by defining our functions as in the previous example.
The parfunction sets the margins around the graph to three lines on all sides plus a small buffer of 0.2 lines. Theplot function plots the function f with a line width twice the default set by lwd=2 and the line type is a solid line set by lty=1. The function is colored blue and thexandyranges are set with xlim and ylim. The defaultx and y axes are turned off with xaxt and yaxt set to “n”, since we will define them to be centered later in the code. We could have turned off both with the one option axes=FALSE. We also turn off the frame around the plot with frame.plot=FALSE. We do not want a caption
Graphing Functions 37 along thexoryaxis and so xlab and ylab are set to blank with empty quotes, but a title is added with main.
Finally, the panel.first command draws the grid first but we do not use the defaults for grid. Within grid the first two options are the number of boxes in the x and y direction, respectively. Because we will define special tick marks along the x-axis, grid has NA which creates no lines along the x-axis. Gridlines for thex-axis will be drawn later in the code. On the other hand, we use default tick marks on the y-axis and NULL places gridlines along the default tick marks. For both of these options, a number can be used instead of NA or NULL. We use lty=6 to set the lines to a form of dashed lines–there are options 2 though 6 for dashed lines–and color the lines with gray60. Note that there are gray options from gray0, black, to gray100, white, as well as just gray.
R Code
> f=function(x){sin(x)}
> g=function(x){x-x^3/factorial(3)+x^5/factorial(5)}
> par(mar=c(3,3,3,3)+0.2)
> plot(f,lwd=2,lty=1,col="blue",xlim=c(-2*pi,2*pi), ylim=c(-1,1),xaxt="n",yaxt="n",frame.plot=FALSE,xlab="", ylab="",main="Function Plot Example",
panel.first=grid(NA,NULL,lty=6,col="gray40"))
> plot(g,lwd=2,lty=2,col="red",xlim=c(-3.5,3.5), add=TRUE)
> v1=c(-2*pi,-3*pi/2,-pi,-pi/2, 0,pi/2,pi,3*pi/2,2*pi)
> v2=c(expression(-2*pi),expression(-3*pi/2),
expression(-pi),expression(-pi/2),"",expression(pi/2), expression(pi),expression(3*pi/2),expression(2*pi))
> axis(side=1,pos=0,lwd=2,labels=v2,at=v1,cex.axis=1)
> axis(side=2,pos=0,lwd=2,c(-1,-0.5,0.5,1), cex.axis=1.5,font=2,las=0)
> v3=c(-2*pi,-3*pi/2,-pi,-pi/2, pi/2,pi,3*pi/2,2*pi)
> abline(v=v3,lty=6,col="gray40")
> mtext("x-axis",side=4,line=1,adj=0.5,las=2)
> mtext("y-axis",side=1,line=0,adj=0.5,las=0)
> text(3,0.25,"f(x)=sin(x)",adj=0,col="blue")
> text(4.75,0.85,expression(g(x)==frac(x^5,120)- frac(x^3,6)+x),adj=0.5,col="red")
Function Plot Example
−2π −3π 2 − π − π 2 π 2 π 3π 2 2π
−1.0 −0.5 0.5 1.0
x−axis
y−axis
f(x)=sin(x) g(x)= x5
120−x3 6+x
The second use of plot graphs the function g. Again we use a lwd=2, double the default width, but use a dashed line with lty=2 which allows us to see both functions when they overlap. The function is colored red and its domain is set with xlim=c(-3.5,3.5), which is smaller than the domain of the graph. The last option, add=TRUE, adds the graph to the previously created graph withplot, as opposed to drawing a new graph. We now move to creating our axis.
The vectors v1 and v2, define the location of the axis labels and the labels.
Vector v1 is numeric whereas v2 is a character vector using the expression command to create mathematical symbols. The first axis command creates thex-axis with side=1 setting it along the bottom or really horizontal since pos=0 draws the axis at 0 along they-axis. The line width of the axis is set to double the default with lwd=2, the tick marks are labeled with the vector v2 by labels=v2, and their locations are defined by the vector v1 with at=v1.
The last option, cex.axis=1, sets the size of the labels to the default of 1. The
Scatter Plots 39 second use of axis creates the y-axis with side=2 for the left side or really vertical since it is located at 0 along the x-axis with pos=0. The axis has a width double the default with lwd=2. If only one vector is included with the axis command, in this case c(-1,-0.5,0.5,1), it is used for both the labels and the locations. The next two options, cex.axis=1.5 and font=2, scales the labels to 1.5 the default size and boldfaces them. Note, the mathematical expressions used on thex-axis cannot be made bold although they can be scaled. We set las=0 so that the labels are parallel to the axis and note that las=2 would orient them to perpendicular.
The next two commands, v3 andabline, will create the vertical gridlines.
The vector v3 defines the locations for the lines, and notice that we left out 0 since that is the y-axis. The abline command draws lines and with v=v3 draws vertical lines at the v3 locations. We use the same line types as we used with grid, lty=6, although it is not necessary, and also color them with gray40. We could have drawn the horizontal gridlines in the same fashion by usingablinewith h=c(-1, -0.5, 0.5, 1), but we wanted to illustrate the use of grid, which usually works fine. We now move to adding text to our graph.
The two mtextfunctions label the xand y axes, with x-axis and y-axis.
The firstmtextfunction places the text on the right with side=4, in the second line of the margin with line=1, centered with adj=0.5, and perpendicular to the side with las=2. The second use of mtextplaces the text on the bottom with side=1, in the first line of the margin with line=0, centered with adj=0.5, and parallel to the side with las=0. We label thesin(x) graph with thetext command by placing the text at (0,0.25), left justified with adj=0, and colored blue. We label the polynomial by placing the text at (4.75,0.85). Note the syntax in theexpressioncommand to create the mathematical symbols. The text is centered with adj=0.5 and colored red. Both of these locations were chosen by a bit of trial and error. Now that is one nice graph.