Skip to the content of the web site.

Statistics & Acturial Sciences

Graphics


Opening a graphics window

To open/close a graphics device :
   > X11()      # open a graphics device in R
   > dev.off()  # close a graphic device in R
Note that you do not need to open a graphics device to use a plotting function. An X11() graphics device is opened automatically when a plotting function is called.

Common types of graphs

There are several high level plot functions that can be used to plot graphs. For instance, a histogram can be made by typing:
   > x <- rnorm(1000)
   > hist(x)
To plot two R objects against one another (called a scatterplot), type:
   > x <- 1:10
   > y <- seq(from=1,to=20,by=2)
   > plot(x,y)

Plotting functions, like hist or plot, have many features that can be changed to accomodate the type of plot that you want. For instance, suppose you wanted a line plot instead of a scatter plot with the x-axis labelled as "X Values". Type:

   > plot(x,y,type="l",xlab="X Values")
See the individual help files for more information.

You may also want to add other plots to an existing plot (i.e. overlay a plot).

  • To add a straight line with y-intercept, 5, and slope, 1, to the above plot, type:
       > plot(x,y,type="l",xlab="X Values")
       > abline(5,1)
    
  • To add a line plot to the above plot, type:
       > plot(x,y,type="l",xlab="X Values")
       > x1<-1:10
       > y1<-rep(c(5,15),5)
       > lines(x1,y1)
    
  • To add points to the above plot, type:
       > plot(x,y,type="l",xlab="X Values")
       > x1<-1:10
       > y1<-rep(c(5,15),5)
       > points(x1,y1)
    

Note that when using the functions, lines or points, any graphical features on the original plot (i.e. title, axis labels, axis ranges) can not be changed without re-running the original plot. This forces the values specified for the x and y coordinates in the functions, lines or points, to lie within the range of the original plot.

Changing graphical features

Graphical plots, like 'hist' or 'plot', have many additional graphical features that aren't given specifically in the help files. For instance, type:
   > help(plot)
to give the following help file.
plot                  package:base                  R Documentation

Generic X-Y Plotting

Description:

     Generic function for plotting of R objects.  For more details
     about the graphical parameter arguments, see `par'.

Usage:

     plot(x, y, xlim=range(x), ylim=range(y), type="p",
          main, xlab, ylab, ...)

Arguments:

       x: the coordinates of points in the plot. Alternatively, a
          single plotting structure, function or any R object with a
          `plot' method can be provided.

       y: the y coordinates of points in the plot, optional if `x' is
          an appropriate structure.

xlim, ylim: the ranges to be encompassed by the x and y axes.

    type: what type of plot should be drawn.  Possible types are

             *  `"p"' for points,

             *  `"l"' for lines,

             *  `"b"' for both,

             *  `"c"' for the lines part alone of `"b"',

             *  `"o"' for both ``overplotted'',

             *  `"h"' for ``histogram'' like (or ``high-density'')
                vertical lines,

             *  `"s"' for stair steps,

             *  `"S"' for other steps, see Details below,

             *  `"n"' for no plotting.

          All other `type's give a warning or an error; using, e.g.,
          `type = "punkte"' being equivalent to `type = "p"' for S
          compatibility.

    main: an overall title for the plot.

    xlab: a title for the x axis.

    ylab: a title for the y axis.

     ...: graphical parameters can be given as arguments to `plot'. 

    .

    .
    
    .

    etc.

The blue highlighted area indicates that there are other graphical parameters that can be given as arguments. To see these, type:
   > help(par)

There are two ways to set the graphical parameters given by 'par'.

  • To set graphical parmaeters that apply to all plots, set the features using arguments to 'par'. The only arguments that can be set this way are:
            *  `"ask"'
    
            *  `"fig"', `"fin"'
    
            *  `"mai"', `"mar"', `"mex"'
    
            *  `"mfrow"', `"mfcol"', `"mfg"'
    
            *  `"new"'
    
            *  `"oma"', `"omd"', `"omi"'
    
            *  `"pin"', `"plt"', `"ps"', `"pty"'
    
            *  `"usr"'
    
            *  `"xlog"', `"ylog"'
    

    Here is an example. A common parameter that you may want to set is to graph several plots on one page. Suppose you wanted 6 plots on one page, with 2 plots per line (a total of 3 lines), and the plots are placed on the graph by filling each row in order. The you would type:

      > par(mfrow=c(3,2))
      > plot(1:10)
      > title("Plot 1")
      > plot(10:100)
      > title("Plot 2")
      > plot(5:7)
      > title("Plot 3")
      > plot(50:75)
      > title("Plot 4")
      > plot(22:33)
      > title("Plot 5")
      > plot(1000:10000)
      > title("Plot 6")
    
    If you wanted to have 6 plots on one page, like above, but instead place the graphs by filling each column in order, then you would use
       > par(mfcol=c(3,2))
    
    To place each of the 6 plots in locations that don't follow an ordering given by 'mfrow' or 'mfcol', first set the number of plots on each page using 'mfrow' or 'mfcol' and then use 'par(mfg=c(a,b))' preceding each individual plotting function to specify the position that you want the plot graphed.

  • To set graphical parameters that apply to specific high level plots, set the features within the plotting function itself. For instance, suppose you wanted to change the plotting symbol in a scatterplot to be the character 's'. Then you would type:
       > plot(1:10,pch="s")
    
    Note that the graphical parameters that are set using 'par' (described in the previous point) can not be set using this method.

Saving/printing graphs

Depending on the operating system that you are using, saving/printing graphics in R differs. Simple methods for saving/printing graphs in R under UNIX or Windows are given below. For more detailed information about saving/printing in R, refer to Section 5.2 in the R Tip Sheet.

  • Generate a plot using any of the plotting functions in R. For instance, type
     > plot(1:10) 

You can save/print a graph in two ways.

  • To copy/save the graph as a metafile or bitmap, just right click on the graph and a menu, like the one shown below, will appear. If you choose to copy as a metfile or bitmap, you must then paste the graph into an editor (say, MS Word) in order to print/save the graph. You may also choose to save the graph as a metafile or postscript file directly before printing.


  • To save the graph in a variety of formats (i.e. postscript,pdf,bmp), left click on
     Save as 
    under the File drop down menu. This is shown below.


    To copy the graph to the clipboard as a bitmap or metafile, left click on

     Copy to the clipboard
    under the File drop down menu. This is shown below. Note that this is equivalent to right clicking on the graph and selecting one of the
     Copy as metafile
    or
     Copy as bitmap
    options. Once the graph is copied to the clipboard, the graph is pasted into an editor such as MS Word.


    Note that you can print directly to a printer as well.



Last Modified:  Friday 29 June 2007