Week 8 Lecture: Graphics in R - Forrest R. Stevensforreststevens.com/teaching/r/lectures/Week 08 -...
Transcript of Week 8 Lecture: Graphics in R - Forrest R. Stevensforreststevens.com/teaching/r/lectures/Week 08 -...
Week 8 Lecture:Graphics in R
Introduction to Programming and Geoprocessing Using R
GEOG-590-75, Spring 2015
Week 14 Lecture:Graphics in R
Introduction to Programming and Geoprocessing Using R
GEO6938-4172GEO4938-4166
Overview
• R can plot data and draw graphics…… but so can lots of other software.
• So why choose R?
Customizable, extensible, programmable graphics
R is permissive…
R’s Graphics Ecosystem
• R’s Graphic Packages
• R’s 3 Distinct Chart Areas
• 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
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 plots
• Possible to produce 3D and animated plots with other packages (e.g. misc3d, rgl)
grDevices
grid
lattice ggplot2
graphics
R’s Three Distinct Chart Areas
• R’s 3 Chart Areas:1. Plot Area – within
axes2. Figure Margin Area –
for axis labels and tick mark labels
3. Outer Margin Area
• R gives User precise control over content, format 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
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" )
# R Script to produce scatter
chart
x_data <- ...
y_data <- ...
## Graphic parameters
par( )
## High level plot function
plot( )
## Low level function
text( )
Graphic Parameters 70 GPs defaults set w/
par()
High Level GraphicFunction Call
Use GP defaultsor
w/ argument
Low Level GraphicFunction Call
Use GP defaults or
w/ 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 = (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" )
Making a Graphic Scriptwith R’s Base Graphic Package
High Level Graphic Functions
Type R function()
Bar Chart barplot()
Box & Whisker Plot boxplot()
Dot Plot dotchartt()
Histogram hist()
Scatter Plot plot()
Strip Chart stripchart()
High level functions produce complete charts…
We’re focusing on plot() and its "low" level functions, graphic parameters…
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
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)
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 =– 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
• User can change GP values:– Global:
par("col" = "red")
– Local: plot(x,y, col="red")
> par("col")
[1] "black"
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" = full
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 row
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 color
o 19 – Solid circle
o 20 – Bullet circle
• Characters 21: 25 can have selected fill and border colors
bg = Controls Border color
col= Controls fill
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,y
y~xVectors for x and y values to be plotted
type=
type = "p" – individual points
type = "l" – line
type = "b" – points connected with lines
type = "h" – vertical lines from 0
type = "s" – step chart
main = " "
sub = " "
Figure title top of plot, large font
Sub title – below X axis, smaller font
xlab = " "
ylab = " " X and Y axis titles
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
plot(x,y, type="?",
main = "??",
sub = "??",
xlab ="??",
ylab ="??", ann = ?,
axes = ?,
xlim = c(x1,x2),
ylim = c(y1,y2))
or
plot(y ~ x, data = "??",
type="?", main="??",
sub = "??",
xlab="??",
ylab ="??", ann = ?,
axes = ?,
xlim = c(x1,x2),
ylim = c(y1,y2))
plot() Function Defaults## Ex_Scr_4_1_default_plot.R ###################
##Script to produce default XY plot()
## STEP 1: SETUP - Source File
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 DATA
y_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
Adding Text within Margin Area
mtext( "note", side = , line =
, 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
• outer – use outer area (T or F)
• adj – text alignment, 0 = left, 0.5= center, 1 = right
• cex - font adjustment factor
• col - color
Axis
Sides
1
2
3
4
Axis Numbering Convention
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" )
Working With R Colors
• Users can specify colors by name; R has 657 colors
– "red"
– "blue"
– "darkgreen"
• To get list of R colors:
>colors()
• col = most common way to specify color for lines, points, text
• 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
Example Scatter plot
###################
##Script to produce scatter plot
## STEP 1: SETUP - Source File
par(oma=c(2,1,0,1))
## STEP 2: READ DATA
x_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 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=
Adding text string in Plot Areatext () Function
text( x, y, "note", pos =0, cex = 1,
col = "red", srt = 45)
x,y – plot area coordinates where text should be placed
"note" – text to be added to plot
pos – position of text with respect to x, y coordinates. Default is centered on point.
cex - font magnification factor
col - color
srt – text rotation - degrees
• User can construct multi-line text
strings by using \n to designate new line
Note <- "This is an example \n
of a multi-line note "
\n for Multi-line Text
\" to add quote marks to text string
• Text strings designated by " " marks
• 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
= "sans"
= "serif"
= "mono"
Adding Lines in Plot Areax <- 0:100
y <- x^2
x_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)
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
• bty="n"
• bty="o"
– Axes
• axes = T or F
– Axis Offset:
• xaxs= "i" or "r"
• yaxs = "i" or "r"
– Axis 3 & 4 tick marks
• Axis(n, tick=T)
Adding Mathematical Expressions
Basic graphics functions in R
abline Add Straight Lines to a Plot
arrows Add Arrows to a Plot
assocplot Association Plots
axTicks Compute Axis Tickmark Locations
axis Add an Axis to a Plot
axis.POSIXct Date and Date-time Plotting Functions
barplot Bar Plots
box Draw a Box around a Plot
boxplot Box Plots
bxp Draw Box Plots from Summaries
cdplot Conditional Density Plots
contour Display Contours
coplot Conditioning Plots
curve Draw Function Plots
dotchart Cleveland Dot Plots
filled.contour Level (Contour) Plots
fourfoldplot Fourfold Plots
frame Create / Start a New Plot Frame
grid Add Grid to a Plot
hist Histograms
hist.POSIXt Histogram of a Date or Date-Time Object
Basic graphics functions in R
identify Identify Points in a Scatter Plot
image Display a Color Image
layout Specifying Complex Plot Arrangements
legend Add Legends to Plots
lines Add Connected Line Segments to a Plot
locator Graphical Input
matplot Plot Columns of Matrices
mosaicplot Mosaic Plots
mtext Write Text into the Margins of a Plot
pairs Scatterplot Matrices
panel.smooth Simple Panel Plot
par Set or Query Graphical Parameters
persp Perspective Plots
pie Pie Charts
Basic graphics functions in R
points Add Points to a Plot
polygon Polygon Drawing
rect Draw One or More Rectangles
rug Add a Rug to a Plot
screen Creating and Controlling Multiple Screens on a
Single Device
segments Add Line Segments to a Plot
spineplot Spine Plots and Spinograms
stars Star (Spider/Radar) Plots and Segment Diagrams
stem Stem-and-Leaf Plots
stripchart 1-D Scatter Plots
strwidth Plotting Dimensions of Character Strings and
Math Expressions
sunflowerplot Produce a Sunflower Scatter Plot
symbols Draw Symbols (Circles, Squares, Stars,
Thermometers, Boxplots) on a Plot
text Add Text to a Plot
title Plot Annotation
xinch Graphical Units
Basic graphics functions in R
Generic function plot and its class-specific methods:
plot Generic X-Y Plotting
plot.data.frame Plot Method for Data Frames
plot.default The Default Scatterplot Function
plot.design Plot Univariate Effects of a 'Design' or Model
plot.factor Plotting Factor Variables
plot.formula Formula Notation for Scatterplots
plot.histogram Plot Histograms
plot.table Plot Methods for 'table' Objects
plot.window Set up World Coordinates for Graphics Window
plot.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/
ggplot2 Example
data(mpg)
ggplot(mpg, aes(displ, hwy))+
geom_point()+
geom_line(aes(color = factor(cyl)))
ggplot2 Examples
p <- ggplot(mpg, aes(displ, hwy))
p + geom_point() + stat_smooth()
ggplot2 Examples
By default, stat_smooth() has added a loess line with 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")
ggplot2 Shortcut
• qplot() is the “simple” version of ggplot() and 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
ShotDist
co
un
t
0
2
4
6
8
100 120 140 160 180
as.factor(DistToPin)
125
150
175
For further information:• The R Graphics Cookbook
http://www.cookbook-r.com/Graphs/
– Another great R resource: http://www.cookbook-r.com/
• "ggplot2: Elegant Graphics for Data Analysis"http://ggplot2.org/
• "R Graphics, Second Edition"http://www.stat.auckland.ac.nz/~paul/RG2e/
• “Learn R Toolkit”http://chartsgraphs.wordpress.com/learnr-toolkit/
• "Lattice: Multivariate Data Visualization with R"http://lmdvr.r-forge.r-project.org/
• The Graphics CRAN Task Viewhttp://cran.r-project.org/web/views/Graphics.html