Statistics Chihara 1
Plotting Concepts in R
There are three basic plotting functions in R: high-level plots, low-level plots, and the layout command
par. Basically, a high-level plot function creates a complete plot and a low-level plot function adds to an
existing plot, that is, one created by a high-level plot command.
High-Level Plot Functions
Some of the basic plot functions include:
Function Description
plot scatter/line plot
hist histogram
barplot barplot
boxplot boxplot
qqnorm normal-quantile
qqplot Q-Q plot
pairs scatterplot matrix
between 2 or more variables
High-level functions may also take optional arguments that enhance the plot.
> x <- seq(-3,3,length=25)
> y <- x^2
> plot(x,y)
> plot(x,y,type="l") # that’s an ’el’
> plot(x,y,type="b")
Option Plot type
type="p" points (default)
type="l" line
type="b" both line/pts
type="n" no points plotted
Other options to plot will change the color, type of line, the range of the x- and y-axis, labels for the x-
and y-axis, etc.
> plot(x,y,type="b",pch="$")
> plot(x,y,type="b",pch=5,col="red")
> plot(x,y,ylim=c(-1,3),ylab="x-squared")
> plot(x,dnorm(x),type="l",main="normal, mu=0,sigma=1")
Statistics Chihara 2
Option Description
pch plot character (pch=1, 2, ...)
lty line type (lty=1, 2, ...)
lwd line thickness (lwd= 1, 2,...)
col color (col=“red”, “blue”,...)
xlim x-axis limits: xlim=c(min,max)
ylim y-axis limits
xlab x-axis label: xlab=“my label”
ylab y-axis label
main main title
sub sub title
The barplot, hist, boxplot and qqnorm will take most of the above options also (the ones that make
sense for the type of plot).
Low-level Plot Functions
Low-level plot functions can be executed only after a high-level plot has been created. For example,
> plot(x,x^3,type="l")
> lines(x, x^2,lty=2, col="blue")
> points(c(-1.5, 1.5, 2.5), c(-10, 5,15))
> text(c(-2,0,2),c(5,-5,5),c("fish","frog","fly"))
> title("My plot")
Another example, using the built-in data set ChickWeight
> help(ChickWeight)
> hist(ChickWeight$weight,main="Distribution of weights", xlab="weight in grams")
> abline(v=mean(ChickWeight$weight),col="red")
> text(mean(ChickWeight$weight)+50, 150, "mean weight"))
Function Description
lines add a line plot
points add points
text add text
mtext margin text
abline add a straight line
qqline add line to qqnorm
title add a title
Statistics Chihara 3
The abline command can create vertical or horizontal lines,
> plot(x,x^2,type="l")
> abline(h=seq(0,8,by=2),lty=2)
or a line of the form y = 2 + (1/2)x,
> abline(2,1/2, lty=3)
For this case, you provide abline with the y-intercept and slope.
The par Command
The par command controls the layout of the graphics device. The option you will use most often will
probably be mfrow (multi-figure, by row), or mfcol. For example, to have a 3x2 layout where the plots
are added by row, set
> par(mfrow=c(3,2))
This setting will exist throughout the life of the graphics device unless you change it back to the default
mfrow=c(1,1).
You can also change the default color, plot character, etc. for the graphs created on the graphics device.
> par(col=4,lwd=2,pch=6)
> plot(x,x^2,type="b")
> plot(x,sin(x),type="l")
But specifying certain options in the plot function will override these settings.
> plot(x,sin(x),type="b",col="blue")
Incidentally, if you create a new graphics device,
> windows() #PC running Windows
> X11() #Unix
this new device will have the default layout settings.
Misc.
• Type colors() at the command line to see the list of colors available to the plotting commands.
• You can export to some common file formats (jpg, pdf, ps). To export the graph to another format
for later printing, choose File > Save As... .
Modified: March 2009, L. Chihara