Search This Blog

Wednesday, January 04, 2012

SPLUS/R Library: Notes From Roger's Seminars on R Graphing in R








UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             

SPLUS/R Library: Notes From Roger's Seminars on R
Graphing in R


This page contains the lecture notes from Graphing in R which was a part of a series of seminars given by Roger Peng at the Statistical Department at UCLA during the summer of 2002. We thank Roger Peng for his permission to adapt and distribute this page via our web site.

1. Plotting Devices
2. Graph Functions
3. Parameter Settings
4. Saving Graphs

Plotting Devices:

If you are working on a windows platform then when you use a function which creates a graph a graphics window will automatically be launched. If you would like to launch the graphics window by yourself without invoking a plotting function then you would use the windows function.
>windows()
To see the arguments for the windows function.
>args(windows)
function (width = 7, height = 7, pointsize = 12, record = getOption("graphics.record"), 
    rescale = c("R", "fit", "fixed"), xpinch, ypinch, canvas = "white", 
    gamma = getOption("gamma")) 
NULL
The first two arguments of windows are the dimensions of the width and height of the graphics window. To resize the window.
>windows(5,5)
Other common device functions include: dev.off, dev.set, graphics.off. These functions are especially useful if there is more than one graphics window open. Only one device can be active and it is in the active device that all the graphics operations occur. The function dev.set is used to specify which device is going to be active. The output from the dev.set function is to list the number of the active device. The dev.off function is used to close a specific device. The default of dev.off is to close the current device. The function graphics.off will close all the devices at once.

Caution: In the windows it is tempting to close the device by clicking on a button in the upper right corner. This can lead to potential problems where the window will be closed but R might not perceive the device as having been shut off.

In the following example we open a graphics device and use the dev.set function to see that device number 2 is active. Then we open another device but use dev.set to active device 2 again. Then we open yet another device and use dev.off to shut down device 2. Finally, we shut down all the remaining devices by using the graphics.off function.
>windows(5,5)
>dev.set()
windows
      2
      
>windows(5,5)
>dev.set(2)
windows
      2
      
>windows(5,5)
>dev.off(2)
>graphics.off()

Graph functions

Common functions to create plots: plot, boxplot, barplot, stars, pairs, matplot, hist, image, contour.
Each of these plot functions are a part of the base package and to see the specifics for each function please consult the help pages (by using the help function, i.e. >help(function_name) ). The plot function is the most basic plotting function. Using the plot function with only one variable will result in an index plot. Using two variables in the plot function will result in a graph with the first argument as the variable plotted on the x-axis and the second argument as the variable plotted on the y-axis. In the following example we create two variables, var1 and var2, and graph them using the plot and hist functions.
>var1 <- rnorm(100)
>plot(var1)
>var2 <- var1 + 2*rnorm(100)
>plot(var1, var2)
>hist(var1)
Common functions that will add features to an existing plot: points, lines, text, mtext, image, contour, title, legend. This is not the exhaustive list of all the functions that will add to a  plot since this list would be too long to include here.  Furthermore, to understand the specific features of each function please consult the help pages.  Note that image and contour can be used to add to an existing plot by using the option add and setting it to TRUE. In the following example we graph two variables using the points function and we identify the two variables using the legend function. The pch option in the points function changes the symbols used in the plot. In the legend function the first argument is the x-coordinate of the upper right hand corner of the legend box, the second argument is the y-coordinate, the third argument is the list of the text to be displayed and the pch option specifies which symbols will be displayed.
>plot(var1, var2)
>var3 <- var2 + 2*rnorm(100)
>points(var1, var3, pch=20)
>legend(-3, 6, c("var2", "var3"), pch= c(1, 20) ) 

The next example uses the plot, text and title functions. The text function allows you to label points in the graph. The default option is to display the observation number for each point.  The cex option specifies the character expansion factor which is the desired size of text character relative to the default text size.  Numbers less than 1 will shrink the text to be smaller than the default and numbers greater than 1 will amplify it to be greater than the default.
>plot(var1, var2)
>text(var1, var2, cex=.7)
>title( "var2 and var3")
The next graph will illustrate the use of the function lines to add lines to an existing graph. The type option in the plot function is used to specify which type of graph to be plotted including points ("p"), lines ("l"), vertical height bars ("h"), and step-functions ("s"). The xlab and ylab options in the plot function change the labels on the x and y-axes respectively. The lines types used in the graph can be changed by using the lty option. The title function will place the first argument as a title above the graph and the second argument as a title below the graph.
>x <- seq( -10, 10, length = 1000)
>plot(x, sin(x), xlab="x-values", ylab="f(x)", type="l")
>lines(x, cos(x), lty=3)
>title( "Trigonometric functions", "sin(x) and cos(x)")
The following example illustrates how you can build a graph in R. We start by using the plot function but this time we use the axes option to indicate that we do not want to utilize the default axes. We use the pch option to change the symbols used in the graph. We want to show both the symbols at each point plotted and draw a line connecting the points, so we use the type option to plot both lines and symbols.
>x <- seq( 0, 2*pi, length = 15)
>y <- sin(x)
>plot(x, y, axes=F, type="b", pch="*", xlab=" ", ylab=" ")
Now we would like to add axes to the graph and we do this using the axis function.
>axis ( 1, at = c(0, 1, 2, pi, 4, 5, 2*pi), labels = c(0, 1, 2, "pi", 4, 5, "2 pi"), pos=0)
>axis (2, at = c( -1, -0.5, 0, 0.25, 0.5, 0.75, 1) )
Finally, we want to add the reference lines and the text at the coordinates (pi, 0). The abline function adds lines to the graph and when using the h options we indicate that we want the lines to be horizontal reference lines and thus we only need to specify the y-intercept for each line. The adj option in the text function determines how the text is adjusted (0=left justified, .5=centered, and 1=right justified).
>abline( h= c(-1, -0.5, 0.5, 1), lty=2) 
>text( pi, 0.1, "sin(pi) = 0", adj=0)

Parameter Settings:

It is possible to set the parameters of the graphs globally by using the par function.  However, if the graphs being produced vary from graph to graph then it is probably more useful to set the parameters within each plotting function.  The most commonly used parameter settings include mfrow (multiple figures by row) and mfcol (multiple figures by column) which determine the layout of multiple graphs in one graphing device.  By specifying mfrow = c(n,m) the results will be an n*m matrix of graphs that are filled by rows, using the mfcol option would generate the same graphics layout but it would be filled by columns rather than by rows. The placement of the graph within the graphics device is determine by fig=c(x1,x2,y1,y2) where x1-y2 are the coordinates of the current figure region expressed as a fraction of the device surface. This is dependent on mfrow and mfcol. Margins size in inches are determined by mai=c(xbot,xlef,xtop,xrig).
>par( mfcol= c(1, 2))
>hist(var1)
>title( "Histogram of var1")
>boxplot(var1)
>title("Boxplot of var1")

Saving graphs:

It is possible to output graphs created in R in many different formats including: Portable Network Graphics (PNG), Windows Bitmap (BMP), postscript (ps) and JPEG. The functions that output the file are named after the format, thus the jpeg function produces a JPEG file, the png function produces a PNG file, the bmp function produces a bmp file, the pdf function creates a pdf file and the postscript function produces a postscript file. The procedure for saving the graphs to a different format is to first create a file using the one of the functions for outputting plots; then perform all the plot functions to achieve the final graph; and then close the graphics device by using the dev.off function. The options h and w control the size (in pixels) of the height and width of the graph being saved.  The following example creates portable network graphics file of a histogram of var1 in a file called hist.png.
>png("hist.png")
>hist(var1)
>dev.off()
Here is an example creating a pdf file of a boxplot of var1 in a file called boxplot.pdf using the pdf function.
>pdf("boxplot.pdf")
>boxplot(var1)
>dev.off()

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.

No comments:

Post a Comment

Thank you