Skip Navigation Links
The previous issue in this series can be found in the November, 2002 issue of Benchmarks Online: Statistical Resources on the Internet
Interactive Graphics in R
By Dr. Rich Herrington, Research and Statistical Support Services Manager
This month we discuss the creation of elementary graphs in R. The GNU S language, "R" is used to implement this procedure. R is a statistical programming environment that utilizes the S and S-Plus language developed at Lucent Technologies. In the following document we illustrate the use of a GNU Web interface to the R engine on the "rss" server ( http://rss.acs.unt.edu/cgi-bin/R/Rprog). This GNU Web interface is a derivative of the "Rcgi" Perl scripts available for download from the CRAN Website (http://www.cran.r-project.org), the main "R" Website. Scripts can be submitted interactively, edited, and then be re-submitted with changed parameters by selecting the hypertext link buttons that appear below the figures. For example, clicking the "Run Program" button below creates a vector of 100 random normal deviates; creates a histogram of the random numbers, and then overlays a nonparametric density estimate over the histogram. To view any text output, scroll to the bottom of the browser window. To view any graphical output, select the "Display Graphic" link. The script can be edited and resubmitted by changing the script in the form window and then selecting "Run the R Program". Selecting the browser "back page" button will return the reader to this document.
Basics of Elementary Graphics in R
The S language allows great flexibility in creating graphs. From very elementary components, the user can build a graph to almost any specification. The functions plot, points, lines, text, mtext, axis, etc, form a suite of functions that plot points, lines and text. A short description of a few commands follows and then some examples:
Theplot function is a workhorse of the S graphics system. Once a plot has been created, additional functions exist for adding to the plotted graphic: points, lines, segments, etc. Options for the plot function give the user greater flexibility in specifying the parts of the plot:
The resulting graph is produced:
Plotting Multiple Graphs on the Same Page: The par Function
The par function allows one to change graphics settings globally: Some layout parameters for the par function:
In the following example we use the par function to add several figures to the same graph. It can either be filled rowwise or columwise.
The resulting matrix of graphs are produced:
Graphics Functions in the Hmisc Library
Frank Harrell's Hmisc library provides a number of interesting high level graphical functions. One of particular interest is drawPlot. drawPlot is a simple mouse-driven function for drawing series of lines, step functions, polynomials, Bezier curves, and points. For example, to draw a general smooth Bezier curve, the user uses the mouse to click on a few points, and must overshoot the final curve coordinates to define the curve. The originally entered points are not erased once the curve is drawn. If the plot function is used on the object returned by drawPlot, only final curves will be shown. The drawPlot function gives the user a way to interactively draw complex figures on new or existing plots. An example is given below of the points that were returned from drawing a graph of holiday cheer. Since the rss server works as a batch submission and not interactively, interactive mouse activity is not possible. However, we can plot out the coordinates that were returned during the R session in Windows 2000. After pressing the "Run Program" button below, a new window opens displaying the program window again, and below that, any text that was generated. To display graphics, press the "Display Graphic (GIF)" button. Rendering of the graphic will take a moment to complete.