The Base Plotting System in R consists of core packages like graphics and grDevices for basic plotting, while lattice and grid packages are used for advanced graphics. Creating a plot involves initializing it and then annotating it, with the base system allowing for detailed customization through various parameters. The document emphasizes the flexibility and control offered by the base plotting system for creating 2-D graphics.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views20 pages
PlottingBase For R
The Base Plotting System in R consists of core packages like graphics and grDevices for basic plotting, while lattice and grid packages are used for advanced graphics. Creating a plot involves initializing it and then annotating it, with the base system allowing for detailed customization through various parameters. The document emphasizes the flexibility and control offered by the base plotting system for creating 2-D graphics.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
The Base Plotting System in R
Roger D. Peng, Associate Professor of Biostatistics
Johns Hopkins Bloomberg School of Public HealthPlotting System
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 /attice package
builds on top of grid; we seldom call functions from the grid package directly
210The Process of Making a Plot
When making a plot one must first make a few considerations (not necessarily in this order)
Where will the plot be made? On the screen? In a file?
- How will the plot be used?
Is the plot for viewing temporarily on the screen?
Will it be presented in a web browser?
Will it eventually end up in a paper that might be printed?
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?
30The Process of Making a Plot
- 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
4r20Base Graphics
Base graphics are used most commonly and are a very powerful system for creating 2-D graphics.
There are two phases to creating a base plot
~ Initializing a new plot
= Annotating (adding to) an existing plot
Calling plot(x, y) orhist(x) will launch a graphics device (if one is not already open) and
draw a new plot on the device
If the arguments to pot are not of some special class, then the default method for p1ot 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 2par; it wouldn't hurt to try to memorize this help page!
520Simple Base Graphics: Histogram
Library (datasets)
hist(airqualitysozone) ## Draw a new plot.
Histogram of airqualityS0zone
secusita0zeneSimple Base Graphics: Scatterplot
Library (datasets)
with(airquality, plot(Wind, Ozone))Simple Base Graphics: Boxplot
Library (datasets)
airquality <- transform(airquality, Month = factor (Month) )
boxplot (Ozone ~ Month, airquality, xlab = "Month", ylab = "ozone (ppb)")
ozme sb)Some Important Base Graphics
Parameters
Many base plotting functions share a set of parameters. Here are a few key ones:
pch: the plotting symbol (default is open circle)
ty: 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
+ x1ab: character string for the x-axis label
+ ylab: character string for the y-axis label
sroSome Important Base Graphics
Parameters
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 plot
+ bg: the background color
mar: the margin size
+ oma: the outer margin size (default is 0 for all sides)
m£row: number of plots per row, column (plots are filled row-wise)
+ m£col1: number of plots per row, column (plots are filled column-wise)
10720Some Important Base Graphics
Parameters
Default values for global graphics parameters
par("lty")
## (1) “solid”
par("col")
## (1) "black"
par("peh")
4120
#1) 1Some Important Base Graphics
Parameters
Default values for global graphics parameters
par("bg")
‘## (1) “transparent”
par("mar")
#1) 8.141 42 20
par ("mérow")
42100
wy itBase Plotting Functions
+ 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 plot
+ text: add text labels to a plot using specified x, y coordinates
+ title: add annotations to x, y axis labels, title, subtitle, outer margin
+ mtext: add arbitrary text to the margins (inner or outer) of the plot
+ axis: adding axis ticks/labels
13720Base Plot with Annotation
Library (datasets)
with(airquality, plot(Wind, Ozone))
title(main = "ozone and Wind in New York City") # Add a title
(Ozone and Wind in New York City
ovone
14120Base Plot with Annotation
with(airquality, plot (Wind, ozone, main = "ozone and Wind in New York city"))
with(subset(airquality, Month = 5), points(Wind, Ozone, col = “blue"))
(Ozone and Wind in New York City
‘ene
15120Base Plot with Annotation
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"))
Jegend("topright", pch = 1, col = ¢("blue", "red"), legend = c("May", "Other Months")
(Ozone and Wind in New York City
ovone
16720Base Plot with Regression Line
with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City",
peh = 20))
model <- Im(Ozone ~ Wind, airquality)
abline(model, Iwd = 2)
(Ozone and Wind in New York City
150
100
one
17720Multiple Base Plots
par(mfrow = ¢(1, 2))
with(airquality, {
plot(Wind, Ozone, main = "Ozone and Wind")
plot(Solar.R, Ozone, main = “Ozone and Solar Radiation")
»
ozone and Wind ‘ozone and Solar Radiation
o 8 6 of *
28 coo” 22 o, , @
BO]. eee i cdg"? ee
: we se . . ee gg
Poo” ° ge 2 ees
Bo cgeses ge ° ey oe 8 P08
2880 8st Rg go n,? Pandy, PS Hs 9 So MHS Po.
. SW org eg ae es 2 0”
5 ‘0 8 ~ om mm m0 m0
Wind Soin
18720Multiple Base Plots
par(mfrow = c(1, 3), mar = e(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)
»
‘Czane ana Weather in New Yor ty
19720Summary
+ Plots in the base plotting system are created by calling successive R functions to "build up" a plot
Plotting occurs in two stages:
- Creation of 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
20K20