The core plotting and graphics engine in R is encapsulated in the following packages:
graphics: contains plotting functions for the “base” graphing systems, including plot
, hist
, boxplot
and many others.
grDevices: contains all the code implementing the various graphics devices, including X11, PDF, PostScript, PNG, etc.
The lattice plotting system is implemented using the following packages:
lattice: contains code for producing Trellis graphics, which are independent of the “base” graphics system; includes functions like xyplot
, bwplot
, levelplot
grid: implements a different graphing system independent of the “base” system; the lattice package builds on top of grid; we seldom call functions from the grid package directly
When making a plot one must first make a few considerations (not necessarily in this order):
Are you using it in a presentation?
Is there a large amount of data going into the plot? Or is it just a few points?
Do you need to be able to dynamically resize the graphic?
What graphics system will you use: base, lattice, or ggplot2? These generally cannot be mixed.
Base graphics are usually constructed piecemeal, with each aspect of the plot handled separately through a series of function calls; this is often conceptually simpler and allows plotting to mirror the thought process
Lattice graphics are usually created in a single function call, so all of the graphics parameters have to specified at once; specifying everything at once allows R to automatically calculate the necessary spacings and font sizes.
ggplot2 combines concepts from both base and lattice graphics but uses an independent implementation
We focus on using the base plotting system to create graphics on the screen device.
Base graphics are used most commonly and are a very powerful system for creating 2-D graphics.
Annotating (adding to) an existing plot
Calling plot(x, y)
or hist(x)
will launch a graphics device (if one is not already open) and draw a new plot on the device
If the arguments to plot
are not of some special class, then the default method for plot
is called; this function has many arguments, letting you set the title, x axis label, y axis label, etc.
The base graphics system has many parameters that can set and tweaked; these parameters are documented in ?par
; it wouldn’t hurt to try to memorize this help page!
library(datasets)
hist(airquality$Ozone) ## Draw a new plot
library(datasets)
with(airquality, plot(Wind, Ozone))
library(datasets)
airquality <- transform(airquality, Month = factor(Month))
boxplot(Ozone ~ Month, airquality, xlab = "Month", ylab = "Ozone (ppb)")
Many base plotting functions share a set of parameters. Here are a few key ones:
pch
: the plotting symbol (default is open circle)
lty
: the line type (default is solid line), can be dashed, dotted, etc.
lwd
: the line width, specified as an integer multiple
col
: the plotting color, specified as a number, string, or hex code; the colors()
function gives you a vector of colors by name
xlab
: character string for the x-axis label
ylab
: character string for the y-axis label
The par()
function is used to specify global graphics parameters that affect all plots in an R session. These parameters can be overridden when specified as arguments to specific plotting functions.
las
: the orientation of the axis labels on the plotbg
: the background colormar
: the margin sizeoma
: the outer margin size (default is 0 for all sides)mfrow
: number of plots per row, column (plots are filled row-wise)mfcol
: number of plots per row, column (plots are filled column-wise)Default values for global graphics parameters
par("lty")
## [1] "solid"
par("col")
## [1] "black"
par("pch")
## [1] 1
Default values for global graphics parameters
par("bg")
## [1] "white"
par("mar")
## [1] 5.1 4.1 4.1 2.1
par("mfrow")
## [1] 1 1
plot
: make a scatterplot, or other type of plot depending on the class of the object being plotted
lines
: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots
points
: add points to a plottext
: add text labels to a plot using specified x, y coordinatestitle
: add annotations to x, y axis labels, title, subtitle, outer marginmtext
: add arbitrary text to the margins (inner or outer) of the plotaxis
: adding axis ticks/labels
library(datasets)
with(airquality, plot(Wind, Ozone))
title(main = "Ozone and Wind in New York City") ## Add a title
with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City"))
with(subset(airquality, Month == 5), points(Wind, Ozone, col = "blue"))
with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City", type = "n"))
with(subset(airquality, Month == 5), points(Wind, Ozone, col = "blue"))
with(subset(airquality, Month != 5), points(Wind, Ozone, col = "red"))
legend("topright", pch = 1, col = c("blue", "red"), legend = c("May", "Other Months"))
with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City", pch = 20))
model <- lm(Ozone ~ Wind, airquality)
abline(model, lwd = 2)
par(mfrow = c(1, 2))
with(airquality, {
plot(Wind, Ozone, main = "Ozone and Wind")
plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
})
par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
with(airquality, {
plot(Wind, Ozone, main = "Ozone and Wind")
plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
plot(Temp, Ozone, main = "Ozone and Temperature")
mtext("Ozone and Weather in New York City", outer = TRUE)
})
Plots in the base plotting system are created by calling successive R functions to “build up” a plot
Annotation of a plot (adding lines, points, text, legends)
The base plotting system is very flexible and offers a high degree of control over plotting