• Tidak ada hasil yang ditemukan

Scatter Plots

Dalam dokumen BOOK College mathematics and statistics (Halaman 56-59)

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.

> points(Ice$Years.after.1970,

Ice$September.Extent.in.MSK,type="p", cex=1.25,pch=7,col="red")

> title(main="Arctic Ice Extent for\nMarch and September",ylab="Million Square Kilometers", col.lab="blue",cex.lab=0.75)

> grid (NULL,NULL,lty=6,col="gray")

> lvn=c("March","September")

> legend("topright",lvn,pch=c(10,7),pt.cex=c(1.5, 1.25), col=c("black","red"),y.intersp=1.25)

> text(42,3.63,"Record Low\n3.63msk",pos=1,cex=0.75)

10 20 30 40

05101520

Years After 1970

Arctic Ice Extent for March and September

Million Square Kilometers

March September

Record Low 3.63msk

Theparfunction is optional and sets the background color of the graph. If it is left out the default is white. The first two variables of theplotfunction are thexandydata using the names of the columns of the imported data. These must be the first two elements whereas the order of the remaining options can be changed. The type is set to p for points, and we note that two other

Scatter Plots 41 common options are l for lines, and o for points and lines. The cex option is a scale factor so that the points are plotted at 1.5 times the default size. The type of points is set by pch and there are numbers 0 through 25 as well as a few select symbols such as + and %. The axes ranges are set by xlim and ylim, and are captioned by xlab and ylab. Note that the text after the axes labeling needs to be in quotes. If they are left out the default is to use the column names as labels. The ylab is left blank here to illustrate the use of label options. The plot function finishes up with col.lab for the color of the axes caption, and a scale factor set by cex.lab for the size of the font. Since ylab was blank these commands were applied to thexaxis caption only.

The pointsfunction adds another set of points to the main plot. In this case the September data was added to the scatter plot. The options for point are mostly the same as for plot although you do not need to set xlim and ylim since they were set inplot. The point type was changed with pch=7 and the col command was used to set the color of the points. In plot, the default is black since col was not set.

A title was added to the graph with the title function. Note the use of

\n to force a line break in the title. If spaces are put around \n then the centering will be off. They axis caption was set here and given a color with col.lab and scaled to be 75% of the default size with cex.lab=0.75. A grid was then added to the graph with thegridfunction. The first two elements are set to NULL which tells R to set them at the default tick marks. The line type is set with lty=6. There are six options from 1 to 6, with 1 being a solid line and 2 through 6 a version of dotted or dashed lines.

The next two commands build the legend. The first, lvn (legend vector names) defines the names of the data for the legend. You can do this directly in thelegendfunction but it begins to get lengthy. The first element oflegend places the legend in the graph. The next is the vector of names. The following three commands define the points we used for each data set so that it is repeated in the legend. The last command is a scale factor for the line spacing in the legend.

Finally, text was added to the graph to point out the record low amount of ice extent. The record low was identified by using min(September.Extent.in.MSK). The first two elements set the coordinates of the text which were chosen by trial and error. The third is the text to be added defined in quotes. The position is set to below with pos=1 and has options 1=below, 2=left, 3=above, and 4=right. There is an offset option as a scale factor, but it wasn’t used here. Again, cex is used to scale the font size to be 75% of the default size.

The graph that is created opens in its own window. As you do this you should be working in the editor and so note that if you change a command and rerun the line R will simply add or overwrite to the open window. This can be useful when trying to position text since you can see the changes. To get a clean window to produce your final graph you can simply close out of the window or runwindows. If you want to work with multiple graphics windows

usedev.newto initiate a new window. Note that your graph can be saved in multiple formats and will be saved based on the size of the open window.

Dalam dokumen BOOK College mathematics and statistics (Halaman 56-59)