REVISED: Saturday, March 2, 2013
R Graphics.
Before you start plotting you might want to tell R where you want the graph you are creating stored. One way to do this is to use the pdf( "filename.pdf") function. The pdf( ) function sets the graphical output file to pdf. dev.off( ) closes the graphical output file.
Before you start plotting you might want to tell R where you want the graph you are creating stored. One way to do this is to use the pdf( "filename.pdf") function. The pdf( ) function sets the graphical output file to pdf. dev.off( ) closes the graphical output file.
I. PLOTTING PACKAGES
The plotting and graphics engine in R is encapsulated in a few base and recommended packages.
A. graphics
graphics package contains plotting functions for the base graphing systems, including plot( ), hist( ), boxplot( ), and many others.
B. lattice
lattice package contains code for producing Trellis graphics, which are independent of the base graphics system, including functions like xyplot( ), bwplot( ), and levelplot( ).
C. grid
grid package implements a different graphing system independent of the base system. The lattice system builds on top of grid. We seldom call functions from the grid package directly.
D. grDevices
grDevices package, which means graphics devices, contains all the code implementing all the various graphics devices, including X11, PDF, PostScript, PNG; etc.
II. PLOTTING PROCESS
When plotting you must make a few choices.
A. DEVICE
To what device will the plot be sent? Do you want it sent to the screen? Do you want it sent to a file?
B. VIEWING
Is the plot for temporary viewing on a console or will it become part of a presentation or a paper? Plots included in a paper or presentation will need to use a file device rather than a screen device. You should also consider who the audience will be and where it will be presented if you are preparing a presentation.
C. DATA
Is there a lot of data going into the plot; or, is it just a few points?
D. RESIZE
Do you need to be able to resize the graphics?
E. GRAPHICS SYSTEM
What graphics system will you use: base grid, or lattice? These generally can not be mixed.
F. BASE GRAPHICS
Base graphics are generally constructed piecemeal, with a series of function calls handling each aspect of the plot separately. This allows plotting to mirror the thought process.
G. LATTICE AND GRID GRAPHICS
A single function call is used to create lattice or grid graphics; therefore, all of the graphics parameters have to be specified at one time. Specifying everything all at once allows R to automatically calculate the spacing and font sizes.
III. BASE GRAPHICS SYSTEM
The most commonly used base graphics are a very powerful system for creating 2-D graphics.
A. FUNCTION CALL
Calling plot(x,y), or hist(x) for a histogram of a single variable, will launch a graphics device, if one is not already open, and draw the plot on the device. The def.off( ) function turns off the graphics device.
For example, the following code makes a histogram:
> str(rnorm) # str( ) shows argument list
function (n, mean = 0, sd = 1)
> str(hist)
function (x, ...)
> z <- rnorm(500) # Example using default arguments
> hist(z)
>
You can control which device gets the plot with the dev.set( ) function. You can close all the graphics devices with the graphics.off( ) function.
B. DEFAULT PLOT METHOD
If the arguments to plot are not of a special class, then the default method for plot is called. The default method for plot( ) has many arguments which will let you set the title, the x axis label, the y axis label; etc.
C. BASE GRAPHICS SYSTEM
The base graphics system has many parameters that can be set and tweaked. These are documented in the ?par, for the par( ) function. You should consider memorizing this help page.
You can also use the example(points) function to see demonstration examples that come prepackaged with R.
IV. BASE GRAPHICS PARAMETERS
The par( ) function is used to specify global graphics parameters that affect all parameters in an R session. These parameters can often be overridden as arguments to specific plotting functions.
The following are commonly used par( ) function parameters.
A. pch
pch is the plotting symbol, the default is 1 which is an open circle.
B. lty
lty is the line type, the default is a solid line, and can be dashed, dotted; etc.
C. lwd
lwd is the line width specified as an integer multiple.
D. col
col is the plotting color, specified as a number, string, or hex code; the colors given give you a vector of colors by name. The default color is black.
E. las
las is the orientation of the axis labels on the plot.
The following are commonly used base graphics parameters.
A. bg
bg is the background color.
B. mar
mar is the margin size.
C. oma
oma is the outer margin size, the default is 0 for all sides.
D. mfrow
mfrow plots are filled row-wise.
E. mfcol
mfcol plots are filled column-wise.
V. BASE PLOTTING FUNCTIONS
A. plot( )
plot( ) function makes a scatter plot or other type of plot depending on the class of the object being plotted.
B. lines( )
lines( ) function adds lines to a plot given a vector of x values and a corresponding vector of y values. A two column matrix. The function connects the dots.
C. points( )
points( ) adds points to a plot.
D. text( )
text( ) function adds text labels to a plot using specified x, y coordinates.
E. title( )
title( ) function adds annotation to x, y axis labels, title, subtitle, and outer margin.
F. mtext( )
mtext( ) function adds arbitrary text to the margins, inner or outer of the plot.
G. axis( )
axis( ) function adds axis ticks or labels.
VI. LATTICE FUNCTIONS
A. xyplot( )
xyplot( ) is the main function for creating scatter plots.
> library(lattice)
> library(nlme)
> xyplot(distance ~ age | Subject, data = Orthodont )
> xyplot(distance ~ age | Subject, data = Orthodont, type = "b" )
bwplot( ) box-and-whiskers plots, also called box plots.
C. histogram( )
histogram( ) for histograms.
D. stripplot( )
stripplot( ) is like a box plot; however, it has actual points.
E. dotplot( )
dotplot( ) plots dots on "violin strings".
F. splom( )
splom( ) scattered matrix; like pairs in base graphics system.
G. levelplot( )
levelplot( ) is a contour plot for plotting image data.
VII. LATTICE FUNCTIONS
Lattice functions take a formula for their first argument; e.g.:
y ~ x| f * g
On the left of the ~ is the y variable. On the right of the ~ is the x variable.
After the | are the conditioning variables. The conditioning variables are optional.
The * indicates an interaction.
The data frame providing the variables in the formula is the second argument.
The parent frame is used if no data frame or list is passed.
There are defaults that can be used if no other arguments are passed.
VIII. IMPORTANT LATTICE HELP PAGES
A. ?par
B. ?plot
C. ?xyplot
D. ?plotmath
E. ?axis
Enjoy R Graphics.
A. graphics
graphics package contains plotting functions for the base graphing systems, including plot( ), hist( ), boxplot( ), and many others.
B. lattice
lattice package contains code for producing Trellis graphics, which are independent of the base graphics system, including functions like xyplot( ), bwplot( ), and levelplot( ).
C. grid
grid package implements a different graphing system independent of the base system. The lattice system builds on top of grid. We seldom call functions from the grid package directly.
D. grDevices
grDevices package, which means graphics devices, contains all the code implementing all the various graphics devices, including X11, PDF, PostScript, PNG; etc.
II. PLOTTING PROCESS
When plotting you must make a few choices.
A. DEVICE
To what device will the plot be sent? Do you want it sent to the screen? Do you want it sent to a file?
B. VIEWING
Is the plot for temporary viewing on a console or will it become part of a presentation or a paper? Plots included in a paper or presentation will need to use a file device rather than a screen device. You should also consider who the audience will be and where it will be presented if you are preparing a presentation.
C. DATA
Is there a lot of data going into the plot; or, is it just a few points?
D. RESIZE
Do you need to be able to resize the graphics?
E. GRAPHICS SYSTEM
What graphics system will you use: base grid, or lattice? These generally can not be mixed.
F. BASE GRAPHICS
Base graphics are generally constructed piecemeal, with a series of function calls handling each aspect of the plot separately. This allows plotting to mirror the thought process.
G. LATTICE AND GRID GRAPHICS
A single function call is used to create lattice or grid graphics; therefore, all of the graphics parameters have to be specified at one time. Specifying everything all at once allows R to automatically calculate the spacing and font sizes.
III. BASE GRAPHICS SYSTEM
The most commonly used base graphics are a very powerful system for creating 2-D graphics.
A. FUNCTION CALL
Calling plot(x,y), or hist(x) for a histogram of a single variable, will launch a graphics device, if one is not already open, and draw the plot on the device. The def.off( ) function turns off the graphics device.
For example, the following code makes a histogram:
> str(rnorm) # str( ) shows argument list
function (n, mean = 0, sd = 1)
> str(hist)
function (x, ...)
> z <- rnorm(500) # Example using default arguments
> hist(z)
>
You can control which device gets the plot with the dev.set( ) function. You can close all the graphics devices with the graphics.off( ) function.
B. DEFAULT PLOT METHOD
If the arguments to plot are not of a special class, then the default method for plot is called. The default method for plot( ) has many arguments which will let you set the title, the x axis label, the y axis label; etc.
C. BASE GRAPHICS SYSTEM
The base graphics system has many parameters that can be set and tweaked. These are documented in the ?par, for the par( ) function. You should consider memorizing this help page.
You can also use the example(points) function to see demonstration examples that come prepackaged with R.
IV. BASE GRAPHICS PARAMETERS
The par( ) function is used to specify global graphics parameters that affect all parameters in an R session. These parameters can often be overridden as arguments to specific plotting functions.
The following are commonly used par( ) function parameters.
A. pch
pch is the plotting symbol, the default is 1 which is an open circle.
B. lty
lty is the line type, the default is a solid line, and can be dashed, dotted; etc.
C. lwd
lwd is the line width specified as an integer multiple.
D. col
col is the plotting color, specified as a number, string, or hex code; the colors given give you a vector of colors by name. The default color is black.
E. las
las is the orientation of the axis labels on the plot.
The following are commonly used base graphics parameters.
A. bg
bg is the background color.
B. mar
mar is the margin size.
C. oma
oma is the outer margin size, the default is 0 for all sides.
D. mfrow
mfrow plots are filled row-wise.
E. mfcol
mfcol plots are filled column-wise.
V. BASE PLOTTING FUNCTIONS
A. plot( )
plot( ) function makes a scatter plot or other type of plot depending on the class of the object being plotted.
B. lines( )
lines( ) function adds lines to a plot given a vector of x values and a corresponding vector of y values. A two column matrix. The function connects the dots.
C. points( )
points( ) adds points to a plot.
D. text( )
text( ) function adds text labels to a plot using specified x, y coordinates.
E. title( )
title( ) function adds annotation to x, y axis labels, title, subtitle, and outer margin.
F. mtext( )
mtext( ) function adds arbitrary text to the margins, inner or outer of the plot.
G. axis( )
axis( ) function adds axis ticks or labels.
VI. LATTICE FUNCTIONS
A. xyplot( )
xyplot( ) is the main function for creating scatter plots.
> library(lattice)
> library(nlme)
> xyplot(distance ~ age | Subject, data = Orthodont )
> xyplot(distance ~ age | Subject, data = Orthodont, type = "b" )
>
B. bwplot( ) bwplot( ) box-and-whiskers plots, also called box plots.
C. histogram( )
histogram( ) for histograms.
D. stripplot( )
stripplot( ) is like a box plot; however, it has actual points.
E. dotplot( )
dotplot( ) plots dots on "violin strings".
F. splom( )
splom( ) scattered matrix; like pairs in base graphics system.
G. levelplot( )
levelplot( ) is a contour plot for plotting image data.
VII. LATTICE FUNCTIONS
Lattice functions take a formula for their first argument; e.g.:
y ~ x| f * g
On the left of the ~ is the y variable. On the right of the ~ is the x variable.
After the | are the conditioning variables. The conditioning variables are optional.
The * indicates an interaction.
The data frame providing the variables in the formula is the second argument.
The parent frame is used if no data frame or list is passed.
There are defaults that can be used if no other arguments are passed.
VIII. IMPORTANT LATTICE HELP PAGES
A. ?par
B. ?plot
C. ?xyplot
D. ?plotmath
E. ?axis
Enjoy R Graphics.
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment