stat 6601 project: device controls and basic plotting functions (v&r 4.1 and 4.2)

30
Stat 6601 Project: Stat 6601 Project: Device Controls and Device Controls and Basic Plotting Basic Plotting Functions Functions (V&R 4.1 and 4.2) (V&R 4.1 and 4.2) Katherine Moore, Jackie Katherine Moore, Jackie Shaffer Shaffer and Terri Carroll and Terri Carroll Statistics, CSUH Statistics, CSUH

Upload: kasimir-bonner

Post on 02-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Stat 6601 Project: Device Controls and Basic Plotting Functions (V&R 4.1 and 4.2). Katherine Moore, Jackie Shaffer and Terri Carroll Statistics, CSUH. Graphical Devices in R. A graphical device receives graphical output. Most often this is a window on the screen - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Stat 6601 Project: Device Stat 6601 Project: Device Controls and Basic Plotting Controls and Basic Plotting

Functions Functions (V&R 4.1 and 4.2)(V&R 4.1 and 4.2)

Katherine Moore, Jackie Shaffer Katherine Moore, Jackie Shaffer

and Terri Carrolland Terri Carroll

Statistics, CSUHStatistics, CSUH

Page 2: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Graphical Devices in R Graphical Devices in R

A graphical device receives graphical A graphical device receives graphical output. output. Most often this is a window on the screenMost often this is a window on the screen A graphical device opens automatically A graphical device opens automatically

when needed, but can be customized by the when needed, but can be customized by the user.user.

Can have more than one open. Can have more than one open.

Page 3: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Examples of Graphical Devices: Examples of Graphical Devices:

windows(width, height) -for viewing plots windows(width, height) -for viewing plots in Windowsin Windows

X11(width, height) -for viewing plots in X11(width, height) -for viewing plots in UnixUnix

win.print(width, height, printer=” “) -win.print(width, height, printer=” “) -outputs plot to printeroutputs plot to printer

pdf(file=”D:\\Stat 6601\\plot.pdf”) -outputs pdf(file=”D:\\Stat 6601\\plot.pdf”) -outputs plot to a pdf file plot to a pdf file

Page 4: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Misc. TipsMisc. TipsThe par() function additionally customizes The par() function additionally customizes devices.devices. par(ask = T) -Asks the user to hit enter to par(ask = T) -Asks the user to hit enter to

clear the screen and view the next plot clear the screen and view the next plot

To save the current plot on a 'windows' device to To save the current plot on a 'windows' device to a file:a file: savePlot(filename=”D:\\Stat 6601\\Plot1”, savePlot(filename=”D:\\Stat 6601\\Plot1”,

type=”wmf”)type=”wmf”) other file types include jpeg, jpg, bmp, pdf other file types include jpeg, jpg, bmp, pdf

graphics.off() closes all open graphical devices graphics.off() closes all open graphical devices

Page 5: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Bar Chart ExampleBar Chart Example

This example uses a data set of male and This example uses a data set of male and female deaths due to lung disease in the female deaths due to lung disease in the United Kingdom during the 1970’s.United Kingdom during the 1970’s.

barplot( matrix to be graphed,barplot( matrix to be graphed, names = vector of names to be plotted below names = vector of names to be plotted below

each bar,each bar, col = a vector of colors for bars,col = a vector of colors for bars, main = string for main title ) main = string for main title )

Page 6: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Bar Chart CodeBar Chart Code

> lung.deaths <- lung.deaths <- aggregate(ts.union(mdeaths,fdeaths),1)aggregate(ts.union(mdeaths,fdeaths),1)

> barplot(t(lung.deaths), barplot(t(lung.deaths), names=c("1974","1975","1976","1977","1names=c("1974","1975","1976","1977","1978","1979"), col=c("blue", "green"), 978","1979"), col=c("blue", "green"), main="UK deaths from lung disease")main="UK deaths from lung disease")

> legend(locator(1), c("Males", "Females"), legend(locator(1), c("Males", "Females"), bg="white", fill=c("blue","green")) bg="white", fill=c("blue","green"))

Page 7: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Bar Chart Example cont.Bar Chart Example cont.

1974 1975 1976 1977 1978 1979

UK deaths from lung disease

050

0010

000

1500

020

000

2500

0

MalesFemales

Page 8: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Other Options to the barplot Other Options to the barplot function: function:

beside = logical valuebeside = logical value

horiz = logical valuehoriz = logical value

angle = slope of shading lines (instead of color)angle = slope of shading lines (instead of color)

density = density of shading linesdensity = density of shading lines

legend.text = vector used to construct legendlegend.text = vector used to construct legend

And Much More…And Much More…

Page 9: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Bar Chart with beside=T & horiz=TBar Chart with beside=T & horiz=T

197

419

7519

7619

7719

7819

79

UK deaths from lung disease

0 5000 10000 15000

MalesFemales

Page 10: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Graphical parameters for Graphical parameters for scatterplots and lines scatterplots and lines

Type=”c”: type of plotType=”c”: type of plot Options are:Options are:

““p”: get only pointsp”: get only points

““l”: get only linesl”: get only lines

““b”: get both points and linesb”: get both points and lines

““s, S”: step functions(s at left end, S at right end)s, S”: step functions(s at left end, S at right end)

““o”: points and lines are overlaido”: points and lines are overlaid

““h”: high-density vertical line plottingh”: high-density vertical line plotting

““n”: doesn’t plot anything(no points and lines just n”: doesn’t plot anything(no points and lines just axes)axes)

Page 11: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Graphical parameters for Graphical parameters for scatterplots and lines cont.scatterplots and lines cont.

xlab=”string” and ylab=”string”: gives titles for the x-axis xlab=”string” and ylab=”string”: gives titles for the x-axis and y-axisand y-axispch=”n”: gives symbols (have to insert a number in place pch=”n”: gives symbols (have to insert a number in place of “n”)of “n”)axes=Laxes=L

Options are:Options are:““F”: axes are not constructedF”: axes are not constructed““T”: axes are constructedT”: axes are constructed

sub=”string” and main=”string”: gives a subtitle and gives sub=”string” and main=”string”: gives a subtitle and gives a title for the plota title for the plotpoints(x,y,…): adds points to a plotpoints(x,y,…): adds points to a plot

Option is:Option is:cex: size of the charactercex: size of the character

Page 12: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Graphical parameters for Graphical parameters for scatterplots and lines cont.scatterplots and lines cont.

lines(x,y,…): adds lines to a plotlines(x,y,…): adds lines to a plot Option is:Option is:

lty: line typelty: line type

lwd: line widthlwd: line width

text(x,y,labels,…): adds text to a plottext(x,y,labels,…): adds text to a plot

abline: adds a straight line to a plotabline: adds a straight line to a plot Options are:Options are:

(a,b): intercept and slope form(a,b): intercept and slope form

h=c: y-value for horizontal lineh=c: y-value for horizontal line

v=c: x-value for vertical linev=c: x-value for vertical line

Page 13: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

If someone wants an example of a plot, If someone wants an example of a plot, points (plotting symbols), line, text or points (plotting symbols), line, text or abline, he or she can use the R-command: abline, he or she can use the R-command: “example(plot)”,”example(lines)”,“example “example(plot)”,”example(lines)”,“example (text)”, example(abline) or “example (text)”, example(abline) or “example (points)”.(points)”.

Page 14: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

For example, when the R-command “example(plot)” is For example, when the R-command “example(plot)” is chosen, he or she would get something like this:chosen, he or she would get something like this:

Page 15: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

When the R-command “example(points)” is chosen, he or When the R-command “example(points)” is chosen, he or she would get something like this, which is the plotting she would get something like this, which is the plotting symbols (cex refers to the size, which in this case is 3):symbols (cex refers to the size, which in this case is 3):

Page 16: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

R-code for producing a sample R-code for producing a sample scatterplot:scatterplot:

> x<-c(.9,.1,.4,.4,.7,.7,.8,.5)x<-c(.9,.1,.4,.4,.7,.7,.8,.5)> y<-c(.4,.6,.1,.4,.7,.2,.6,.3)y<-c(.4,.6,.1,.4,.7,.2,.6,.3)> plot(x,y,xlab="x",ylab="y",main="Demonsplot(x,y,xlab="x",ylab="y",main="Demons

tration of a scatterplot", type="p")tration of a scatterplot", type="p")> points(x,y,type="p",pch=9,col='purple')points(x,y,type="p",pch=9,col='purple')> abline(0,1)abline(0,1)

Page 17: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Output obtained after R-code was Output obtained after R-code was run:run:

Page 18: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Dynamic GraphingDynamic Graphing

S Plus has limited ability for interactive S Plus has limited ability for interactive graphinggraphing Use ‘brush’ function to label data points, spin Use ‘brush’ function to label data points, spin

and resize graph, etc.and resize graph, etc.

R has less abilityR has less ability ‘‘identify’ function adds labels to data points identify’ function adds labels to data points

returns row numbers in R sessionreturns row numbers in R session

XGOBI and GGOBI packages add XGOBI and GGOBI packages add interactive graphing capabilities to S Plus interactive graphing capabilities to S Plus and R and R

Page 19: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Code for Interactive Graph in RCode for Interactive Graph in R

Hills dataset available in library(MASS)Hills dataset available in library(MASS)

Code from page 9 V&R:Code from page 9 V&R:

> attach(hills)> attach(hills) ‘allows columns to be ‘allows columns to be available by name’available by name’

> plot(dist,time)> plot(dist,time)

> identify(dist,time,row.names(hills))> identify(dist,time,row.names(hills))

[1] 4 7 11 17 30 31 33 35[1] 4 7 11 17 30 31 33 35> detach(hills)> detach(hills) ‘clean up’‘clean up’

Page 20: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Interactive GraphInteractive Graph

Page 21: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Multivariate DataMultivariate Data

R and S Plus can produce a scatterplot or R and S Plus can produce a scatterplot or pairs plot showing a matrix of scatterplots pairs plot showing a matrix of scatterplots for each pair of variablesfor each pair of variables

S Plus – use ‘splom’ functionS Plus – use ‘splom’ function

R – use ‘pairs’ functionR – use ‘pairs’ function Code for pairs plot (page 9 V&R):Code for pairs plot (page 9 V&R):

> pairs(hills)> pairs(hills)

Page 22: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Pairs Plot in RPairs Plot in R

Page 23: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Plots of SurfacesPlots of Surfaces

The following displays a function defined The following displays a function defined on a two-dimensional regular gridon a two-dimensional regular grid ‘‘contour’ functioncontour’ function ‘‘image’ functionimage’ function ‘‘persp’ functionpersp’ function

Page 24: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Code for Contour PlotCode for Contour Plot

Topo data has 3 variables (page 76 V&R)Topo data has 3 variables (page 76 V&R)

> library(MASS)> library(MASS)

> topo.loess<-loess(z~x*y, topo, degree=2, span=0.25)> topo.loess<-loess(z~x*y, topo, degree=2, span=0.25)

> topo.mar<-list(x=seq(0, 6.5, 0.2), y=seq(0, 6.5, 0.2))> topo.mar<-list(x=seq(0, 6.5, 0.2), y=seq(0, 6.5, 0.2))

> topo.lo<-predict(topo.loess, expand.grid(topo.mar))> topo.lo<-predict(topo.loess, expand.grid(topo.mar))

> par(pty="s")> par(pty="s")

> contour(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", > contour(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", ylab="y axis",ylab="y axis",

+ levels = seq(700,1000,25), cex=0.7)+ levels = seq(700,1000,25), cex=0.7)

Page 25: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Contour PlotContour Plot

Page 26: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

> image(topo.mar$x, topo.mar$y, topo.lo, > image(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", ylab="y axis")xlab="x axis", ylab="y axis")

Page 27: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

> persp(topo.mar$x, topo.mar$y, topo.lo, > persp(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", ylab="y axis", col="red")xlab="x axis", ylab="y axis", col="red")

Page 28: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Star PlotStar Plot

mtcars data has several variables mtcars data has several variables including:including:

car name, mpg, hp, weight, etc. (code car name, mpg, hp, weight, etc. (code from star function description in R)from star function description in R)

> data(mtcars)> data(mtcars)

> palette(rainbow(12, s=0.6, v=0.75))> palette(rainbow(12, s=0.6, v=0.75))

> stars(mtcars[,1:7], len=0.8, > stars(mtcars[,1:7], len=0.8, key.loc=c(12,1.5),key.loc=c(12,1.5),

+ main="Car Trends", draw.segments=TRUE)+ main="Car Trends", draw.segments=TRUE)

Page 29: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

Star PlotStar Plot

Page 30: Stat 6601 Project: Device Controls and Basic Plotting Functions  (V&R 4.1 and 4.2)

The EndThe End

Any questions?Any questions?