next up previous
Next: Data Structures Up: Introduction to S Previous: Functions

Basic Graphics

S has many powerful graphics functions. In order to take advantage of them on a unix machine, S requires that you first specify the type of graphics device you are using. R users on the Mac can skip down to the list of graphics functions at the bottom of this page.

> motif()         # open a graphics window 
> postscript()  # send graphics output to a postscript printer
> printer()     # produce line printer plots: necessary if you are at a 
                # `dumb' terminal, but very low resolution
Usually you will want to use motif(); it is easy to print directly from the graphics window to a laserprinter. To print graphics directly from the graphics window, you need to do two things:

Basic S graphics functions include:

Function Name          Description 

hist(x)             draw a histogram of the data in x 
stem(x)	            draw a stem and leaf diagram of the data in x
boxplot(x)          draw a box plot of x 
plot(x,y)           draw a scatterplot of y on x
contour(x,y,z)      draw a contour plot; z[i,j] is the height
                          of the surface at (x[i],y[j])

lines(x,y)          add a line to an existing plot, connectin successive
			pairs of points (x[i],y[i])
text(x,y,z)         plot symbol z[i] at coordinates (x[i],y[i])
title("My Title")   add a title to the current plot \\
legend(...)         add a legend to a plot

S has very sophisticated graphics capabilities which allow the user to add lines, text, and so forth to existing plots, highlight selected points, plot subsets of the data using different plot characters, and so on. The following example shows how to make a scatterplot of two variables, x and y, using a third variable, grp, to determine the plot character.

Example

> motif()                  # open a graphics window
> grp <- rep(c(1,2),25)      # make up group numbers
> x <- rnorm(50) + grp       # generate x data
> y <- rnorm(50) + grp       # generate y data
> plot(x,y,type="n")       # set up plot region
> text(x,y,grp)              # plot group numbers
> legend(locator(1), legend=c("Group 1","Group 2"),pch=c("12"))
                           # add legend to the plot: click the left
			   # mouse button in a blank area

>     #alternatively you could specify plot symbols by groups:
> plot(x,y,type="n")
> points(x[grp==1],y[grp==1],pch="o")
>            # plot the x,y points from group 1 with an "o"
> points(x[grp==2],y[grp==2],pch="x")
>            # plot group 2 with "x"'s


next up previous
Next: Data Structures Up: Introduction to S Previous: Functions

Albyn Jones
jones@reed.edu
Tue Jun 25 11:03:47 PDT 1996