> X11() # open a graphics device in R > dev.off() # close a graphic device in RNote 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.
> 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).
> plot(x,y,type="l",xlab="X Values") > abline(5,1)
> plot(x,y,type="l",xlab="X Values") > x1<-1:10 > y1<-rep(c(5,15),5) > lines(x1,y1)
> 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.
> 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'.
* `"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.
> 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.
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.
> plot(1:10)
You can save/print a graph in two ways.

Save asunder 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 clipboardunder 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 metafileor
Copy as bitmapoptions. 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.