Line Plots
The primary command for creating line plots has the form
plot(x, y, options);
where x and y are vectors of data and options represents many of the possible options for
the plot command. If only one vector of data is passed, then the vector values will be plotted against their index.
Options may be globally set using the par(...)
command. The plot
and par documentation lists the options that may be specified.
Use the lines(...)
to plot additional data on an existing plot.
One can include parameters listed under the par command explicitly
in a plot(..)
call. (These parameters will only apply to current plot call).
One can create mathematics in plot labels and titles using plotmath.
Useful commands related to creating plots :
lines(...) |
Plots additional lines on a given plot. See lines for details |
par(...) |
Sets the default parameters for plotting. See par for details. |
windows(...) |
Opens up a new graphics window for plotting. See windows for details. Use the GUI windows selection to manage multiple plot windows. |
grid(...) |
Adds a grid lines to the plot. See grid for details. |
screen(...) |
Commands for creating multiple plots on a single page. See screen for details (and samples below). |
x <- seq(0.0,2.0*pi,.1); y <- sin(x); plot(x,y,type='l');
# Generating x,y data for sin and cos functions, then plotting in the same plot
x <- seq(0.0,2.0*pi,.1); y <- sin(x); z <- cos(x); plot(x,y,type='l'); # plot sin lines(x,z,type='l',col="blue"); # plot cos as blue line
# Generating x,y data for a sin function and then plotting in a window
# with x and y ranges [0.0,2.0*pi] x [-1.5,1.5].
x <- seq(0.0,2.0*pi,.1); y <- sin(x); plot(x,y,type='l',xaxs='i',yaxs='i',xlim=range(0,2*pi),ylim=range(-1.5,1.5));
# Creating a two plots on a single page.
x <- seq(0.0,2.0*pi,.1); y <- sin(x); z <- cos(x); split.screen(c(2,1)); screen(1); # select screen 1 to plot on plot(x,y,type='l'); # plot sin screen(2); # select screen 2 plot(x,z,type='l'); # plot cos close.screen(); # close split screen mode
# Creating plots on two separate pages
x <- seq(0.0,2.0*pi,.1); y <- sin(x); z <- cos(x); plot(x,y,type='l'); # plot sin windows(); # create a new graphics window plot(x,z,type='l'); # plot cos in new window
#
# A plot with a title that contains mathematics (the paste command concatenates strings)
#
aTitle <- expression(paste("f(x) = ", over(1,1 + x^2))); x <- seq(-5.0,5.0,.1); y <- 1.0/(1.0 + x^2); plot(x,y,type='l',col='blue',main=aTitle);
#
# Using the par command to set plot options, and then restore them
#
oldPar <- par(xaxs='i',yaxs='i',lwd=2,font.lab=2); # set new options x <- seq(-5.0,5.0,.1); y <- 1.0/(1.0 + x^2); plot(x,y,type='l',col='blue'); par(oldPar); # restore original
UCLA Mathematics Department | ©2000 |