R Graphics Handout

advertisement
Advanced Graphics in R Notes
Tuesday, March 8, 2011
Module 1: Dressing Up Basic Graphs
Packages: graphics (base package in R)
Skills: Changing axis labels, adding Greek letters/math symbols, making stylistic changes
1. Go to: http://www.education.uiowa.edu/StatOutreach/shortcourses/index.htm
2. Under “March 8: Advanced Graphics in R,” save the script file and the prac_data.csv file to a
convenient location.
3. Open R from the Start Menu.
4. Open your script file in R: File  Open Script  browse for script file.
PART A
Refer to the table on the next page and the help files for par and plotmath to fill in the code given in
the Script file to make the following changes to the simple plot of SampVars versus SampMeans:
a. main =
Add a main title “Sample Statistics”
b. font.main =
Make the main title bold and italic.
c. xlab =
Change the x axis label to “Sample Means ( )” (See hint below)
d. ylab =
Change the y axis label to “Sample Variances ( ” (See hint below)
e. cex.lab =
Change the size of the axis labels to 1.2
f. mgp =
Change the margin space for the axis titles to 2.5 and the space for the
axis labels to 0.75
g. pch =
Change the plotting symbols to solid circles
h. las =
Change the orientation of the axis labels so they are all horizontal.
Hint: Here is the code to parts (c) and (d):
xlab = expression(paste("Sample Means ",(bar(x)) ))
ylab = expression(paste("Sample Variance ",(hat(sigma)^2) ))
*The expression code lets us add math symbols and Greek letters
*The paste code lets us combine math symbols/Greek letters with regular text
*For Greek letters, just spell it out. If you capitalize the first letter, then you will get the capital version of
the Greek letter. For instance,
sigma  σ
Sigma  Σ
Labels
Axes
Lines/Points
Appearance
(Colors, Font style,
Size)
xlab
ylab
main
xlim
ylim
xaxp
yaxp
=
=
=
=
=
=
=
las =
=
=
=
mgp =
Argument
" "
" "
" "
c(min, max)
c(min, max)
c(min, max, n)
c(min, max, n)
0 parallel to axis (default)
1 horizontal to axis
2 perpendicular to axis
3 vertical to axis
c(3,1,0)
axes = TRUE
= FALSE
xaxt = "n", yaxt = "n"
type = "p" for points
= "l" for lines
= "b" for both
= "o" for both ‘overplotted’
= "n" for no plotting
*(see help for more options)
lty = 0 blank
= 1 solid (default)
= 2 dashed
= 3 dotted
= 4 dotdash
= 5 longdash
= 6 twodash
lwd = 1 (default)
pch = 21 (default)
col
= " "
col.main = " "
col.lab = " "
col.axis = " "
font = 1 plain (default)
= 2 bold
= 3 italic
= 4 bold italic
cex =
cex.lab =
cex.main =
Purpose
Specify the x axis
Specify the y axis
Specify the title
Specify the range of values on the x and y axis
Specify the range of values on the x and y axis as well
as the number of intervals (n)
Specify the orientation of the numbers/labels on the x
and y axis
Specify the margin space for the axis titles, axis labels
(i.e. the numbers on x and y axis) and axis line
Specify if you want the axis lines to be shown or not.
Specifies that you do not want to plot the x or y axis.
Specify the type of plot
Specify the type of line
Specify line width (must be a positive number)
Specify the type of plotting symbol.
Specify the color of the points, main axis, labels, and
axes, respectively. For all possible colors, go to:
http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
Specify font style for text in plot – Can also specify:
font.main, font.axis, font.lab,
font.axis, font.sub
Adjust size of points, axis labels, and title
PART B
Now, we’re going to plot two graphs in one Graphics Window.
Splitting up Your Graphics Window
You can use the par(mfrow=c(rows,columns)) function or the layout function. The first of
these two methods is easier, so we will do that, but you can learn how to use layout by looking up help
on it later by typing ?layout. Also check out this website for examples of different layouts:
http://www.statmethods.net/advgraphs/layout.html
Plot 1
Just copy and paste your code for the graph you made in Part A and rerun it.
Plot 2
Boxplots of the sample means and variances by group
For the SampMeans by Group boxplot, fill in the missing code with the directions given in a-d:
boxplot(SampMeans~Group,
data=prac_data,
main= "
",
xlab="Group",
col="goldenrod1",
xlim=
,
ylim=
,
at=1:2,
xaxt=
)
a.
b.
c.
d.
##a
##b
##c
##d
Title the graph “Boxplots of Sample Statistics”
Make the x axis range from 0 to 5.
Make the y axis range from 20 to 60.
Suppress graphing of the x axis.
For the SampVars by Group boxplot, make the adjustments specified in e-f:
boxplot(SampVars~Group,
data=prac_data,
add=TRUE,
col="darkseagreen",
at= ,
xaxt=
)
##e
##f
e. Specify that these boxplots should appear at x = 3 and 4 (Hint look at the “at” code used for the
sample mean boxplots)
f. Suppress graphing of the x axis.
Take note of the axis and legend commands that are already filled in for you.
Module 2: Arrows, Shading, and Curves – Oh my!
Packages: sfsmisc (for pretty arrows)
Skills: Shading in areas of your plots, adding arrows to plots, and adding text within plots
PART A
We’re going to add vertical and horizontal lines to our scatterplot from Module 1 as well as shade in
certain portions of it.
(1) Copy code for making your scatterplot in Part A of Module 1.
(2) Run the provided code to add vertical lines at x =50 and x =55
(3) Look up help on abline (?abline) to add horizontal lines at y = 25 and y = 35
(4) Use the provided code to shade in the lower left hand box.
(5) Look up help on rect (?rect)to shade in the upper right hand box with the following
specifications
a. Choose the x and y coordinates so that the shaded area fills the entire upper right hand
box
b. Make the color green
c. Make the density of the lines 20
d. Make the border of the box black
e. Make the angle of the lines at -45 degrees
PART B
We’re going to plot a normal distribution (i.e., the bell-curve) using the “curve” function. Then we will
use the “p.arrows” function from the “sfsmisc” package to add arrows to our plot, and we will use the
“text” function to add text within our plot.
(1) Use the provided code to plot a normal distribution.
(2) Use the provided code to add an arrow to your plot that points to the peak of the curve. The
needed function “p.arrows” is in the “sfsmisc” package so we first need to call it using the
“library” function.
(3) Label the arrow using the provided “text” code.
(4) Your turn – Add an arrow on the other side of the curve that also points to the peak. Use
?p.arrows to look up help on the available arguments.
(5) Your turn – Add the label “Mean” below your new arrow. Use ?text to look up help on the text
function.
Module 3: Taking Plotting to the Next Level
Packages: ggplot2 for complex and cool looking plots
Skills: Adding a regression line to a scatterplot, adding a density curve to a histogram, making plots
using the ggplot2 package, saving plots as pdf files
PART A
We’re going to make a scatterplot and add the least-squares regression line to the cars data.
Method 1 – Using basic R graphics
The code for doing this is provided but feel free to add arguments to the plot and abline function to
make the graph prettier. For instance, you could change the plotting symbols, add a title, make the line
dashed or a different color.
Rerun the code for the graph again, but this time uncomment the pdf and dev.off (i.e. remove the
## in front of these lines) statements to save the graph. In the pdf statement, specify a path name where
you want to save your graph. The graph will not appear in R, but if you open the folder where you saved
the graph, a pdf file will be there with the graph!
Method 2 – Use the ggplot2 package functions
2(a) -- Using qplot
 Just run the provided code
2(b) - Using ggplot
 Just run the provided code
PART B
We’re going to plot histograms of the speed variable and add a density line.
Method 1 – Using basic R graphics
 Just run the provided code
Method 2 – Using ggplot2
 Just run the provided code
Great R Graphics Resources











Summary of different Graphing Tools in R
o http://cran.r-project.org/web/views/Graphics.html
Producing Simple Graphs in R
o http://www.harding.edu/fmccown/R/
Basic Graphs (be sure to check out all the links on the right of the page for all the different types
of plots from density plots to scatterplots!)
o http://www.statmethods.net/graphs/creating.html
Advanced Graphics – info about different layouts and more
o http://www.statmethods.net/advgraphs/layout.html
Graphs and code from Paul Murrell’s book R Graphics
o http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html
Information of the lattice package
o http://www.ci.tuwien.ac.at/Conferences/DSC-2001/Proceedings/Murrell.pdf
Shading in area under a curve
o http://www.feferraz.net/en/P/Shaded_areas_in_R
A presentation on Graphing in R
o http://faculty.smu.edu/ngh/stat6304/class_sgraph09.pdf
All about the ggplot2 package including a link to the ggplot book
o http://had.co.nz/ggplot2/
Interactive plots –
o
playwith  manual at http://cran.r-project.org/web/packages/playwith/playwith.pdf
 info at http://code.google.com/p/playwith/
o
o
iplots - http://www.rosuda.org/iPlots/
rggobi - http://www.ggobi.org/rggobi/introduction.pdf
 http://www.ggobi.org
3-D plotting with the rgl package
o http://rgl.neoscientists.org/about.shtml
Download