14 lecture graphics r - university of...
TRANSCRIPT
11/21/2011
1
Week 14 Lecture:Week 14 Lecture:Graphics in R
Introduction to Programming and Geoprocessing Using R
GEO6938‐4172GEO4938‐4166
Overview
• R can plot data and draw graphics…b t l t f th ft… but so can lots of other software.
• So why choose R?
Customizable, extensible, programmable graphicsgraphics
R is permissive…
11/21/2011
2
R’s Graphics Ecosystem
• R’s Graphic Packages• R s Graphic Packages
• R’s 3 Distinct Chart Areas
• Script Components• Script Components – High level functions– Graphic parameters– Low level functions
Low Level Graphics
• R follows a “painters” model• We can add items like we would add ink/paint• R’s base graphics provide primitive graphics elements:
• Graphs and charts are just these elements arranged in meaningful ways
11/21/2011
3
R’s Graphics Packages
• R has several graphics subsystems:– graphics, traditional graphics– grid package (low level functions)– lattice package– ggplot2 package
• This lecture will cover the traditional, graphics system and a bit about ggplot2
R’s Graphics Ecosystem
• R’s core graphics capabilities focus on static, 2D l t2D plots
grDevices
grid
lattice ggplot2
graphics
• Possible to produce 3D and animated plots with other packages (e.g. misc3d, rgl)
lattice ggplot2
11/21/2011
4
R’s Three Distinct Chart Areas
• R’s 3 Chart Areas:1. Plot Area – within
axesaxes2. Figure Margin Area
– for axis labels and tick mark labels
3. Outer Margin Area
• R gives User precise control over content, f f llformat of all 3 Areas– High Level Graphic
Functions– Graphic Parameters– Low Level Graphic
Functions
Based on D Kelly O’Day’s Learn R Workshop, R Charts
Making a Graphic Scriptwith R’s Base Graphic Package
# R Script to produce scatter chartx_data <- ...data <
High Level GraphicFunction CallUse GP defaults
Low Level Graphicll y_data <- ...
## Graphic parameterspar( )## High level plot functionplot( ) ## Low level functiontext( )
Graphic Parameters 70 GPs defaults set adjust
w/ par()
Use GP defaultsor
w/ argument
Function CallUse GP defaults
orw/ argument
par(oma=c(2,2,2,2)); par(mar=c(3,3,1,1))
plot(x_data, y_data, main = "My Chart Title", type = "b", xlim = c(0,11), ylim = c(0,11), xlab ="X", ylab = "Y", pch=16, col = "red", bty = "n", xaxs="i", yaxs = "i", las=1)
text(2,7, "my note", col = "blue" )
11/21/2011
5
par(oma=c(2,2,2,2))par(mar=c(3,3,1,1))
Making a Graphic Scriptwith R’s Base Graphic Package
plot(x_data, y_data, main = "My Chart Title", type = "b", xlim = (0,11), ylim = c(0,11), xlab ="X", ylab = "Y", pch=16, col = "red", bty = "n", xaxs="i", yaxs = "i",las=1)
text(2,7, "my note", col = "blue" )
High Level Graphic Functions
Type R function()Type ()
Bar Chart barplot()
Box & Whisker Plot boxplot()
Dot Plot dotchartt()
Histogram hist()
High level functions produce complete charts…
We’re focusing on plot() and its "low" level functions, graphic parameters…
Scatter Plot plot()
Strip Chart stripchart()
11/21/2011
6
Low Level Plot FunctionsAdd graphic content to current plot
grid(nx, ny) Add grid lines to current plot. NA stop grid in corresponding direction
axis(side n,) Add axis at side n to current plot ( ,) p
box(which=, ) Add box around current plot, figure or outer margin area depending on which specified
legend Add legend to current plot
arrows(x,y)lines(x, y)points(x,y)
Add arrow line, line or points to current plot. type = can be used to specify style ("p","b", "l", etc)
abline(a b) Add li t t l t i i t t b i labline(a,b)abline(h= or v=)
Add line to current plot. a is intercept, b is slope.h/v for horizontal/ vertical line
segments(x0,x1,y0, y1) Add line segment(s) between pairs of points
polygon(x,y) Add polygon defined by vectors x and y
text(x,y, "note") Add text to current plot at x & y
Graphic Parameters (GP)• R has over 70 Graphic Parameters (GP)• Each GP has:
name: col =– name: col =– value: "red"
• Default values set at start of an R session
• To get par() values:– par() lists all GPs with current value– par("GP_name") gives current
value for specific GP> par("col")[1] "black"
• User can change GP values:– Global:
par("col" = "red")
– Local: plot(x,y, col="red")
11/21/2011
7
Essential Graphical Parametersadj= Text string justification: 0=left, 0.5=centered, 1=right
ann= T or F controls plot annotation
bg= Background color
bty= Type of box around plot: "n"=none, "l" = x & y, "o" = fully yp p , y,
cex= Magnification ratio for text relative to default point size
col= Color specification. R accepts "red", "blue", "dark grey", etc names for 657 colors
las= Style of tick mark labels: 0=parallel to axis, 1 =always horizontal, 3=always vertical
lty= Line type: 0=blank, 1=solid, 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash
lwd= Line width, default is 1.
* mar=, oma= Vector c(b,l,t,r) that sets number of lines for margin and outer margin areas by axis #
* mfcol=, mfrow= Vector c(nr,nc) to specify multiple plots. mfcol draws by columns, mfrow by rowmfrow=
pch= Integer specifying a symbol to be used in plotting points
* ps= Point size of text, not symbols
* pty= Plot region: s=square, m=maximum
srt= String rotation in degrees
* xaxs=, yaxs= Style of axis: "r" = offset of x & y to prevent points overplotting axis, "i" = no offset
* These parameters must be set with call to par().
pch = Plot Characters Graphic Parameter
• R has 25 plotting characters• pch = specifies plot character • Characters 19 – 20 can be filled
with selected coloro 19 – Solid circleo 20 – Bullet circle
• Characters 21: 25 can have selected fill and border colors
bg = Controls Border color col= Controls fill
11/21/2011
8
lty= Line Type Graphic Parameter
• R has 6 line types • lty = specifies the line type
• lty= can be specified as integer or character string:0 – "blank"1 – "solid"2 – "dashed"3 – "dotted"4 – "dotdash"5 – "longdash"6 – "twodash"
plot() Function Argumentsx,yy~x
Vectors for x and y values to be plotted
type = "p" individual points
plot(x,y, type="?",main = "??", sub = "??",
type=
type = p – individual pointstype = "l" – linetype = "b" – points connected with linestype = "h" – vertical lines from 0 type = "s" – step chart
main = " " sub = " "
Figure title top of plot, large fontSub title – below X axis, smaller font
xlab = " "ylab = " "
X and Y axis titles
sub ?? , xlab ="??", ylab ="??", ann = ?,axes = ?,xlim = c(x1,x2), ylim = c(y1,y2))
or
plot(y ~ x, data = "??", ylab = " "
ann= T or F Should default annotation be included
axes = T or F Set to F when you want custom axis
xlim=c( x1, x2)ylim=c(y1, y2)
Limits of plot for X and Y axes
p ot(y , data ,type="?", main="??", sub = "??",xlab="??", ylab ="??", ann = ?,axes = ?,xlim = c(x1,x2), ylim = c(y1,y2))
11/21/2011
9
plot() Function Defaults## Ex_Scr_4_1_default_plot.R ################### ##Script to produce default XY plot()## STEP 1: SETUP - Source File rm(list=ls())rm(list=ls())par(oma=c(2,1,0,1))script = "C:/Learn_R/Mod_4_R_Charts/Ex_Scr_4_1_default_plot.R"
## STEP 2: READ DATAy_data <- c(0,1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,0)x_data <- seq(0:20)
## STEP 3: MANIPULATE DATA
## STEP 4: PRODUCE CHART - REPORT plot(x_data,y_data, main = "plot() Defaults ")
## Outer Margin annotation my_date <- format(Sys.time(), "%m/%d/%y")mtext(script, side = 1, line = .75, cex=0.7, outer = T, adj = 0)mtext(my_date, side = 1, line =.75, cex = 0.7, outer = T, adj = 1)
plot() Function Defaults
11/21/2011
10
Adding Text within Margin Area
mtext( "note", side = , line = dj 0 1, outer = F, adj = 0, cex = 1,
col = "red")
• "note" – text to be added to margin area
• side – side of plot to place text(1= bottom, 2= left, 3= top, 4=right)
• line – line of margin to place text, starting at 0, counting outwards
AxisSides
1
2
3
4
Axis Numbering Convention
• outer – use outer area (T or F)• adj – text alignment, 0 = left, 0.5=
center, 1 = right• cex ‐ font adjustment factor• col ‐ color
Improving plot() Function Defaults
plot(x_data,y_data, main = "plot() Defaults" )
plot(x_data,y_data,main = "plot() Default Improvements", type = "b", col = "red", bty = "n", xaxs="i", yaxs = "i", las=1,xlim = c(0,25), ylim = c(0,12), xlab = "X", ylab = "Y" )
11/21/2011
11
Working With R Colors
• Users can specify colors by name; R has 657 colors
• col = most common way to specify color for lines, points, text
– "red"– "blue"– "darkgreen"
• To get list of R colors:>colors()
• Alternatives: rgb() and hsv()
• Color Conversions:– col2rgb()– convertColor()
plot() type argument type=?• "p" for points• "b" for both • "o" for both ‘overplotted’ • "l" for lines • "c" for the lines part alone of "b"• "h" for high‐density vertical lines • "s" for h/v stair steps • "S" for v/h stair steps • "n" for no plotting o o p ott g
11/21/2011
12
Example Scatter plot
################### ##Script to produce scatter plot## STEP 1 SETUP S Fil## STEP 1: SETUP - Source File par(oma=c(2,1,0,1))
## STEP 2: READ DATAx_data <- seq(0,10*pi,0.1*pi)y_data <- sin(x_data)
plot(x_data,y_data,type = "l")
Working With Text
Text Additions:– main= argument to add chart • Size: par(ps=10)
title in figure margin at top– sub= argument to add subtitle
below X axis – text() – low level function to add
text in plot area– mtext() – low level function to
add text in margin and outer areas
Size: par(ps 10)• Magnification: cex = 0.85• Color: col="red" (657 named color choices)
• font= 1‐4 (regular, bold, italic, bold italic)• family= ("sans", "serif", "mono")• Alignment: adj = 0,.5,1(left,center, right)• Rotation: srt=
11/21/2011
13
Adding text string in Plot Areatext () Function
text( x, y, "note", pos =0, cex = 1, col = "red" srt = 45)
• User can construct multi‐line text \
\n for Multi-line Text
col = "red", srt = 45)
x,y – plot area coordinates where text should be placed
"note" – text to be added to plotpos – position of text with respect to x, y
coordinates. Default is centered on point.
strings by using \n to designate new line
Note <- "This is an example \nof a multi-line note "
\" to add quote marks to text string
• Text strings designated by " " marksUser can embed q ote marks ithin te t
cex ‐ font magnification factorcol ‐ color srt – text rotation ‐ degrees
• User can embed quote marks within text string by using \" to have R not treat mark as begin/end of text string
Note <- "This is an example of using\"quote marks\" within text string"
text() font and family• font= font type for plot text
=1 Standard =2 Bold=3 Italic=4 Bold and italic
• Use for other figure text– font.axis; font.lab– font.main; font.sub
• family controls font family• family = controls font family= "sans"= "serif"= "mono"
11/21/2011
14
Adding Lines in Plot Areax <- 0:100y <- x^2x_pts <- c(15, 35, 35, 15, 15)y_pts <- c(8300, 8300, 6500, 6500,8300)
plot(x,y, type = "n", main="Add Lines, Points, Arrows Examples")
points(x, y,col = "black", lty=1, type = "l")
grid(col="lightgrey", lty=1)
abline(h=2500, col="red")abline(v=50, col = "blue")
arrows(65,8400, 85,8400, code=3, col="orange", length = 0.1)g , g )
lines(x_pts, y_pts, lty=1, col = "green")
points(x_pts,y_pts, type = "p",col ="red",pch=19)
Plot Box and Axis Styles
• Plot Box & Axis Options– Box type ox type
• bty="n"• bty="o"
– Axes• axes = T or F
– Axis Offset: • xaxs= "i" or "r"
"i" " "• yaxs = "i" or "r"
– Axis 3 & 4 tick marks• Axis(n, tick=T)
11/21/2011
15
Adding Mathematical Expressions
Basic graphics functions in Rabline Add Straight Lines to a Plotarrows Add Arrows to a Plotassocplot Association PlotsaxTicks Compute Axis Tickmark Locationsaxis Add an Axis to a Plotaxis.POSIXct Date and Date-time Plotting Functionsbarplot Bar Plotsbox Draw a Box around a Plotboxplot Box Plotsbxp Draw Box Plots from Summariescdplot Conditional Density Plotscontour Display Contourscoplot Conditioning Plotscurve Draw Function Plotsdotchart Cleveland Dot Plotsfilled.contour Level (Contour) Plotsfourfoldplot Fourfold Plotsframe Create / Start a New Plot Framegrid Add Grid to a Plothist Histogramshist.POSIXt Histogram of a Date or Date-Time Object
11/21/2011
16
Basic graphics functions in R
identify Identify Points in a Scatter Plotimage Display a Color Imagelayout Specifying Complex Plot Arrangementslegend Add Legends to Plotslines Add Connected Line Segments to a Plotlocator Graphical Inputmatplot Plot Columns of Matricesmosaicplot Mosaic Plotsmtext Write Text into the Margins of a Plotpairs Scatterplot Matricespanel.smooth Simple Panel Plotpar Set or Query Graphical Parameterspersp Perspective Plotsp p ppie Pie Charts
Basic graphics functions in Rpoints Add Points to a Plotpolygon Polygon Drawingrect Draw One or More Rectanglesrug Add a Rug to a Plotscreen Creating and Controlling Multiple Screens on a
Single Devicesegments Add Line Segments to a Plotspineplot Spine Plots and Spinogramsstars Star (Spider/Radar) Plots and Segment Diagramsstem Stem-and-Leaf Plotsstripchart 1-D Scatter Plotsstrwidth Plotting Dimensions of Character Strings and
Math Expressionssunflowerplot Produce a Sunflower Scatter Plotpsymbols Draw Symbols (Circles, Squares, Stars,
Thermometers, Boxplots) on a Plottext Add Text to a Plottitle Plot Annotationxinch Graphical Units
11/21/2011
17
Basic graphics functions in R
Generic function plot and its class‐specific methods:
plot Generic X-Y Plottingplot.data.frame Plot Method for Data Framesplot.default The Default Scatterplot Functionplot.design Plot Univariate Effects of a
'Design' or Modelplot.factor Plotting Factor Variablesplot.formula Formula Notation for Scatterplotsplot.histogram Plot Histogramsplot.histogram Plot Histogramsplot.table Plot Methods for 'table' Objectsplot.window Set up World Coordinates for
Graphics Windowplot.xy Basic Internal Plot Function
ggplot2
• High level plotting functionality• Provides a graphical “language” for building very nice figures
• Has a good book out now explaining the philosophy behind it:
http //had co nz/ggplot2/http://had.co.nz/ggplot2/
11/21/2011
18
ggplot2 Exampledata(mpg)ggplot(mpg, aes(displ, hwy))+
geom_point()+geom_line(aes(color = factor(cyl)))
11/21/2011
19
ggplot2 Examples
p <- ggplot(mpg, aes(displ, hwy))p + geom point() + stat smooth()p + geom_point() + stat_smooth()
ggplot2 Examples
By default, stat_smooth() has added a loess line ith th t d d t d b iwith the standard error represented by a semi‐
transparent ribbon. You could also specify the method to use to add a different smoothing line:
p + geom_point() + stat_smooth(method = "lm")
library(MASS)p + geom_point() + stat_smooth(method = "rlm")
11/21/2011
20
ggplot2 Shortcut
• qplot() is the “simple” version of ggplot() d i tand is very easy to use:
qplot(x=ShotDist, geom="histogram", fill=as.factor(DistToPin), main="Distribution of Accurate Shot Distances (< 20 yds.) by Shot Distance", binwidth=10, data=subset(raw_data, BallToPinDist < 20))
Distribution of Accurate Shot Distances (< 20 yds.) by Shot Distance
8
coun
t
4
6
as.factor(DistToPin)
125
150
175
ShotDist
0
2
100 120 140 160 180
11/21/2011
21
For further information:• "R Graphics, Second Edition"
http://www.stat.auckland.ac.nz/~paul/RG2e/
• “Learn R Toolkit”http://chartsgraphs wordpress com/learnr toolkit/http://chartsgraphs.wordpress.com/learnr‐toolkit/
• "Lattice: Multivariate Data Visualization with R"http://lmdvr.r‐forge.r‐project.org/
• "ggplot2: Elegant Graphics for Data Analysis"http://had.co.nz/ggplot2/book/
• The Graphics CRAN Task Viewhttp://cran.r‐project.org/web/views/Graphics.html
• The R Graphics Galleryp yhttp://addictedtor.free.fr/graphiques/
• Advance Charts for Climate and Process Trendshttp://processtrends.com/toc_r.htm
• The R Graphical Manualhttp://www.oga‐lab.net/RGM2/