Basic Graphical Routines in R

A list of useful graphics functions (edited version of help(package = graphics))

Standard Graphics Facilities in R

Plotting commands can be divided into three basic groups:

In addition, R maintains a list of graphical parameters which can be manipulated to customize your plots.

High-level functions

The Generic plot function can deal with the task of plotting several types of R objects. The most common use is to plot a single or paired numeric vectors.

x <- runif(100, min = 1, max = 5)
y <- x^2 + runif(100)

plot(x)    # plot x against index 1, 2, ... length(x)
plot(x, y) # bivariate 'scatter plot'

## Create a list with components x and y. This can be plotted directly

z1 <- list(x = x, y = y)
plot(z1)

Many variations on the basic plot are possible.

## Different types

plot(z1, type = "l")

## Since the points are not ordered properly, this is all jumbled
## up. To get sorted values of x:

ord <- order(x)
z2 <- list(x = x[ord], y = y[ord])

plot(z2, type = "l") # lines
plot(z2, type = "o") # points and lines overlayed
plot(z2, type = "s") # step
plot(z2, type = "h") # histogram-like


plot(z2, type = "n") # no plotting! (useful for adding things later)
points(z2)
lines(z2$x, z2$x^2)


## It's possible to use log scales instead

plot(z2, type = "o", log = "x")
plot(z2, type = "o", log = "y")
plot(z2, type = "o", log = "xy")

There are several standard graphical elements, which can be controlled by extra arguments to plot functions:

plot(z2, type = "o", col = 'red', pch = 16, cex = 2)
plot(z2, col = c('red', 'blue'), pch = "+", cex = 2)

## colors can be specified in several ways

colors()  ## named colors

palette() ## these can be specified by numbers, 1 - 8
plot(1:8, col = 1:8, pch = 16, cex = 2)

## gray levels using gray()
plot(1:20, pch = 16, cex = 2,
     col = gray(seq(0, 1, length = 20)))

## RGB values using rgb()
plot(z2, col = rgb(1, 0, 1), pch = "+", cex = 2)


## plotting characters
plot(1:25, pch = 1:25, cex = 2)


## line type, width
plot(z2, type = "l", lty = 2)
plot(z2, type = "l", lty = 4, lwd = 2)

Other useful arguments that almost all plot functions have are main, sub, xlab, ylab. These are all used to annotate the plot by adding titles, sub-titles and labels.

So far, we have seen the default plot method. There are several other plot types which can be produced by the plot function, depending on what object is being plotted.

## create a grouping variable of length 100

a <- factor(sample(1:5, 100, replace = TRUE), levels = 1:5)
a
levels(a) <- LETTERS[1:5]
a

plot(a) ## the same as: 
barplot(table(a))


plot(a, x)
plot(y ~ a) # formula - used often for statistical modeling in R
plot(~ x + y + a)



## data frames: using in-built Iris Data

data(iris)
class(iris)
iris

plot(iris)
plot(iris[, 1:4])
## color by Species
plot(iris[,1:4], col = as.numeric(iris$Species))



## plotting funtions

plot(sin, from = -2 * pi, to = 2 * pi)
## new function
damped.sin <- function(x) sin(5 * x) * exp(-x^2)
plot(damped.sin, from = -pi, to = pi)

## This is equivalent to
curve(damped.sin, from = -pi, to = pi)

The only call we have seen so far is plot, which tries to guess at a suitable plot type given its arguments. Generally, for a particular type of plot, one uses a specific function designed for that purpose:

## scatter plot matrix (same as plot for data frames)
## produces scatter plot for each pair of variables

pairs(iris)

## See example(pairs) for more complicated examples


## bar plots (from ?barplot)

data(VADeaths)
barplot(VADeaths, plot = FALSE)
barplot(VADeaths, plot = FALSE, beside = TRUE)

mp <- barplot(VADeaths) # default
barplot(VADeaths, beside = TRUE,
        col = c("lightblue", "mistyrose", "lightcyan",
        "lavender", "cornsilk"),
        legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)


## Box and whisker plots

boxplot(x)
boxplot(x ~ a)
boxplot(Sepal.Length ~ Species, iris)


## strip charts (1-D scatter plot)

stripchart(x ~ a)
stripchart(x ~ a, method = "jitter", jitter = .1)

## stripchart can be used to create 'dot plots'
## (in R, dotplots refer to something different)

smp <- sample(20:40, 30, rep = TRUE)
stripchart(smp, method = "stack")
stripchart(smp, method = "stack", pch = 16)


## Stem and Leaf displays

stem(x)


## Histograms

hist(y)

data(singer, package = "lattice")
str(singer)
hist(singer$height, freq = TRUE)  # frequency histogram
hist(singer$height, freq = FALSE) # density histogram

## unequally spaces breakpoints
hist(singer$height,
     breaks = c(50, 60, 65, 68, 71, 75, 80),
     col = "cyan")


## Density plots
## similar principle, more computation intensive

plot(density(singer$height, kern = "rect"))


## Pie charts

pie(table(a))



## displaying many variables: star plots

data(mtcars)
stars(mtcars[, 1:7], key.loc = c(14, 2),
      main = "Motor Trend Cars : stars(*, full = F)", full = FALSE)
stars(mtcars[, 1:7], key.loc = c(14, 1.5), 
      main = "Motor Trend Cars : full stars()", flip.labels=FALSE)

Low level functions

A very standard practice in R is to incrementally add components to an existing graph. The functions to do this are called low-level functions --- distinguished by the fact that they don't create new plots themselves. In particular, they use the co-ordinate system from the existing graph. The most commonly used ones are lines, points, text.

plot(x, y)

## lowess() does some sort of smoothing (details not important) and
## returns a list with components x and y. This can be used to add
## lines

lws.fit <- lowess(x, y)
str(lws.fit)
lines(lws.fit, col = "green")

## add the y = x^2 function
## add = TRUE needed so that a new plot is not started

curve(x^2, add = TRUE)

text(x = 2, y = 20, lab = "Scatter plot with lowess fit",
     col = "blue", font = 4)

## abline: adds straight lines

## horizontal lines
abline(h = c(10, 15))

## lines of the form y = a + b.x
abline(a = 1, b = 4)

Shading and Polygons

Arbitrary geometric shapes can be drawn and shaded using polygon(). For example, the following code draws the histogram from a normal sample, adds the standard normal density curve and shades the area under the normal curve between 1 and 4.

nrm <- rnorm(1000) ## simulation from standard normal
hist(nrm, freq = FALSE)
curve(dnorm, add = TRUE)

## shade area between 1 and 4

tt <- seq(from = 1, to = 4, length = 30)
dtt <- dnorm(tt)
polygon(x = c(1, tt, 4),
        y = c(0, dtt, 0),
        col = "gray")

Interacting with plots

Standard R graphics has rather minimal support for interaction, mainly through the functions locator() and identify(). (There are interfaces to external software with better interaction facilities.)

plot(x, y)
## left-click to select points, right-click to stop
identify(x, y)

## click on 5 points to make a polygon out of them
polygon(locator(5))

Visualizing 3-D (matrix) data

## heights of a volcano in New Zealand

data(volcano)
str(volcano)

image(volcano)
contour(volcano)

## combining the two: use add = TRUE in the second call, so that a new
## plot is not started

image(volcano)
contour(volcano, add = TRUE)

## sophisticated version of image
filled.contour(volcano)

## Perspective Plots

persp(volcano)
demo(persp)
par(bg = "white")
Last modified: Thu Jan 21 11:39:23 PST 2010