S-Plus creates three kinds of graphics, depending on how you count. Traditional or
command-line graphics come from the command-line. New style or Axum graphics
come from the GUI, and Trellis (tm) graphics can be generated either way. The reason that S-Plus
has the first two is originally historical but each provides some advantages. The third represents a
different approach to graphics that's worth learning about.
S-Plus has a number of different types of graphics devices, but you generally won't
care. The usual graphics device is the graphsheet. That gets started automatically
by the plotting command and carries a name like "GS1" (if you create the graph from the
GUI) or "GSD1" (from the command line). If you issue another plot command, it will
generally clear your device. To start a second device simultaneously with the first,
use the graphsheet() command explicitly. You can give your picture a meaningful
title and set a host of other options there. The dev.set(), dev.cur()
and related commands let you manage your windows from within a function. If you end up
with a million windows, graphics.off() will close all of them.
S-Plus has a few other devices that might be useful: postscript()
Postscript graphics, wmf.graph() and pdf.graph() for producing
Postscript files, Windows Metafiles and PDF documents. The java.graph()
device produces "graphlets," which are interactive graphics for Web pages (see the
Finally you shouldn't use one of these devices Trellis graphics (below); use trellis.device()
instead. This performs some special set-up routines for Trellis graphs: you can
specify whether the ultimate output will be to graphsheet, wmf, or pdf devices.
Command-line Graphics Are Editable If...
Until recent versions of S-Plus, traditional graphics (produced at the command line) weren't editable. Therefore a good approach was to create them with a function, editing the function until you got everything exactly as you wanted it. This is still a good approach if you need to produce a specific sort of graph many times. Non-editable graphics are drawn a bit faster that the editable kind, so they're useful when you have very big data sets.
In S-Plus 6, command-line graphics can be edited if the "editable graphics" button on the command-line toolbar is depressed. Editing graphics in S-Plus is done as in Excel: click on an axis to change it, click on the data itself to change the plotting character, and so on.
The plot() Command
The basic command-line command for producing a scatter plot or line graph is plot(). This function takes a number of options that are detailed in the help for par(). The ones you need to know right away are col= to set colors, lty= to set line types (dotted and dashed), lwd= to set line widths, pch= to set the character type, type= to pick points (type = "p"), lines ("l"), or both ("b"), and cex= to set the "character expansion" (font size).
Also important are xlab= and ylab= to set the labels, xlim= and ylim= to set the limits of the axes, and main= to put a title on the plot. See help (par) for details on these and lots of other options.
The mtext() function, to add a sub-title, is also useful here.
Other commands that are useful are boxplot(), barplot(), hist() and pie().
You can add stuff to a plot with the text() and arrows() commands. text() is vectorized,
so it supplements points() as a good way to add plotting symbols that are text. Often you'll want to find the co-ordinates of a particular spot on the graph so you can place an arrow or some textual annotation. The locator() function will do the trick.
Special Commands Worth Knowing
Here are a few more tips:
- The type="n" argument to plot() plots nothing. This is useful for setting up a plot's ranges, axes, title and so on before you call points() or lines() to add stuff.
- To put a density curve on a histogram, use the prob=T argument in the hist() function, as in hist(y, prob=T). Then set up a vector of values that span the width of the histogram, maybe using a command like x <- seq (min(y), max(y), len=100). Then add the line with a command like lines(x, dnorm(x)).
- The pairs() plot shows scatterplots of every pair of vectors in a data frame. spin() shows a three-dimensional plot (for numeric data) and brush() combines the two. Experiment with the brush()
function. It's cool. You can identify weird points and the function will return the indices of those points.
- A different version of "three-dimensional" plot comes out of persp(), contour(), and image().
plot() as a generic function
Some objects have particular plotting needs. For those objects there may be special plotting methods already
written. For example, if your object mylm has a class of lm, then when you issue the
plot(mylm) command, S-Plus knows that the particular plot function it should call is the
plot.lm() function. plot() is a generic function with methods for lots of different
classes. See classes and methods for more information.
There are lots of types of graph available on the 2D palette and under the Graph menu. GUI graphics are generally editable, either directly or through the Object Explorer. One of the great things about the GUI is that it's easy to go straight from a data window to a graph: just click your columns (X, then Y) and plot. Customizing is easy, too. The only real disadvantages of GUI graphics are that it can be hard to create hundreds of them, and that you can't create new types.
Saving The Commands Used To Create A GUI Graphic
Sometimes it takes while to get a graphic to exactly where you want it. Once you've done that you can save it by opening a new script window, copying the graphic window and pasting it into the script window. The code it produces is very complicated, but it works most of the time. (Plotting separate vectors, rather than columns in a data frame, seems to fail.) You can also drag and drop the graphic into a script window but this hardly ever works for me.
Trellis(tm) (Lattice) Graphics
Trellis is a trademark for a way of displaying multiple plots. As a general name let us use the word "lattice" to describe this approach. In a lattice we see a set of plots, each displaying a relationship between two or more variables. The different panels of the graph condition the pictures on the values of yet another variable. S-Plus takes care of a lot of details here: all the axes are the same, for example. Trellis graphs can be produced at the command line, but it's easier to produce them from the GUI. Do one of these:
- Press "set conditioning mode" on the main tool bar, and pick the number of conditioning
columns. Then go to a data window and pick columns to plot, in order (conditioning variables
last) and pick a graph from the plot palette.
- Draw a plot without conditioning. Then highlight the conditioning variable in the data
window. Drag from the first row of the relevant column (not from the column label) over to
the title part of the graph until you see a dotted rectangular window. When you release the
mouse, the graph is drawn.
Saving and Exporting Graphics to Word
When you "save" a graph it gets stored in S-Plus's own ".sgr" format. The next time you
start S-Plus you can open such a graph back up and continue editing it.
You don't need to save or export a graphic to insert it into Word: you can just copy and paste it
and that works fine. However I don't recommend editing inside Word. For one thing your
vertical axis label will be rotated.
Personally I'm in the habit of saving my pictures on disk. A good format for this is
WMF is you're using Office. (EPS with WMF header is a good choice if you're using LaTex.)
Then in Word use Import | Picture | From File to get the picture into your document and
change the picture's properties as necessary.
Making PowerPoint Presentations From Your S-Plus Graphics
S-Plus has a cool "Make PowerPoint Presentation" that turns your graphs into
PowerPoint instantly. Just press the button and off you go.
Return to S-Plus docs