Skip Navigation Links

Page One

Campus Computing News

Winter Break Hours

Moving off the Academic Mainframe

Adaptive Lab Website Completed

Today's Cartoon

RSS Matters

SAS Corner

The Network Connection

Link of the Month


Short Courses

IRC News

Staff Activities

Subscribe to Benchmarks Online

Research and Statistical Support Logo

RSS Matters

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 (  This GNU Web interface is a derivative of the "Rcgi" Perl scripts available for download from the CRAN  Website (, 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:

Function Description
plot(x, y)     Produces a scatterplot of x against y
points(x, y)  Points at the coordinates given by x and y
lines(x, y)  Lines through the points given by x and y
segments(x1, y1, x2, y2) Disconnect line segments from (x1, y1)
arrows(x1, y1, x2, y2) Arrows from (x1, y1) to (x2, y2)
text(x1, y1, text)  Text at the specified position
title("title", "subtitle")  Title and/or subtitle
abline(a, b) Line with intercept a and slope b
mtext(text, side=3) Text is written in one of the four margins

The plot 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:

Parameters for plot function Description
type = "p"   Plot type.  "p", "l", "b", "h", "o", "s", "n"
axes=T / axes=F With / Without axes
main="Title"   Title String
sub="Subtitle"     Subtitle String
xlab="x axis label" x-axis label
ylab="y axis label" y-axis label
xlim=c(xmin, xmax) x-axis scale
ylim=c(ymin, ymax)  y-axis scale
pch="*"  Plot character
lwd=1   Line width. 1=default, 2=twice as thick, etc.
lty=1       Line type.  1=solid, 2=small breaks, etc.
col=1   Color.  0=background


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:

Parameters for the par function Description
fin=c(m,n)  Figure size in inches. m width, n height
pin=c(m,n) Picture in inches, as in fin
mar=c(5,4,4,2)+0.1 All margins in lines
mai=c(1.41, 1.13, 1.13, 0.58) All margins in inches
oma=c(0,0,0,0) Outer margin lines
omi=c(0,0,0,0) Outer margin in inches
plt=c(0.11, 0.94, 0.18, 0.86) Plot region coords. as fraction of figure region
usr x-Axis and y-axis min and max
mfrow=c(m,n) Mutiple figure layout, rowwise plotting
mfcol=c(m,n) Multiple figure layout, colwise plotting;
new=F / new=T If set to T, the next high-level plotting command should not clean the current graphics window before drawing

 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.   


The graphical capabilities of the S languages are one of its most powerful features.  The S language provides a flexible system for creating graphs and modifying the layout of these graphs on a page.  

Next Time

Next time we return to Part II of our series on multilevel modeling using the NLME (linear and nonlinear mixed effects) functions in R and S-Plus.  Happy Holidays!


Harrell, F.E. and Alzola C.E. (2002).  An Introduction to S and the Hmisc and Design Libraries

Krause, A. and Olson, M. (2000).  The Basics of S and S-Plus, 2nd Edition.  Springer Verlag: New York.