300 likes | 430 Views
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
E N D
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 • A graphical device opens automatically when needed, but can be customized by the user. • Can have more than one open.
Examples of Graphical Devices: • windows(width, height) -for viewing plots in Windows • X11(width, height) -for viewing plots in Unix • win.print(width, height, printer=” “) -outputs plot to printer • pdf(file=”D:\\Stat 6601\\plot.pdf”) -outputs plot to a pdf file
Misc. Tips • The par() function additionally customizes devices. • par(ask = T) -Asks the user to hit enter to clear the screen and view the next plot • To save the current plot on a 'windows' device to a file: • savePlot(filename=”D:\\Stat 6601\\Plot1”, type=”wmf”) • other file types include jpeg, jpg, bmp, pdf • graphics.off() closes all open graphical devices
Bar Chart Example • This example uses a data set of male and female deaths due to lung disease in the United Kingdom during the 1970’s. • barplot( matrix to be graphed, • names = vector of names to be plotted below each bar, • col = a vector of colors for bars, • main = string for main title )
Bar Chart Code • lung.deaths <- aggregate(ts.union(mdeaths,fdeaths),1) • barplot(t(lung.deaths), names=c("1974","1975","1976","1977","1978","1979"), col=c("blue", "green"), main="UK deaths from lung disease") • legend(locator(1), c("Males", "Females"), bg="white", fill=c("blue","green"))
Other Options to the barplot function: • beside = logical value • horiz = logical value • angle = slope of shading lines (instead of color) • density = density of shading lines • legend.text = vector used to construct legend • And Much More…
Graphical parameters for scatterplots and lines • Type=”c”: type of plot • Options are: • “p”: get only points • “l”: get only lines • “b”: get both points and lines • “s, S”: step functions(s at left end, S at right end) • “o”: points and lines are overlaid • “h”: high-density vertical line plotting • “n”: doesn’t plot anything(no points and lines just axes)
Graphical parameters for scatterplots and lines cont. • xlab=”string” and ylab=”string”: gives titles for the x-axis and y-axis • pch=”n”: gives symbols (have to insert a number in place of “n”) • axes=L • Options are: • “F”: axes are not constructed • “T”: axes are constructed • sub=”string” and main=”string”: gives a subtitle and gives a title for the plot • points(x,y,…): adds points to a plot • Option is: • cex: size of the character
Graphical parameters for scatterplots and lines cont. • lines(x,y,…): adds lines to a plot • Option is: • lty: line type • lwd: line width • text(x,y,labels,…): adds text to a plot • abline: adds a straight line to a plot • Options are: • (a,b): intercept and slope form • h=c: y-value for horizontal line • v=c: x-value for vertical line
If someone wants an example of a plot, points (plotting symbols), line, text or abline, he or she can use the R-command: “example(plot)”,”example(lines)”,“example (text)”, example(abline) or “example (points)”.
For example, when the R-command “example(plot)” is chosen, he or she would get something like this:
When the R-command “example(points)” is chosen, he or she would get something like this, which is the plotting symbols (cex refers to the size, which in this case is 3):
R-code for producing a sample scatterplot: • x<-c(.9,.1,.4,.4,.7,.7,.8,.5) • y<-c(.4,.6,.1,.4,.7,.2,.6,.3) • plot(x,y,xlab="x",ylab="y",main="Demonstration of a scatterplot", type="p") • points(x,y,type="p",pch=9,col='purple') • abline(0,1)
Dynamic Graphing • S Plus has limited ability for interactive graphing • Use ‘brush’ function to label data points, spin and resize graph, etc. • R has less ability • ‘identify’ function adds labels to data points returns row numbers in R session • XGOBI and GGOBI packages add interactive graphing capabilities to S Plus and R
Code for Interactive Graph in R • Hills dataset available in library(MASS) • Code from page 9 V&R: > attach(hills)‘allows columns to be available by name’ > plot(dist,time) > identify(dist,time,row.names(hills)) [1] 4 7 11 17 30 31 33 35 > detach(hills)‘clean up’
Multivariate Data • R and S Plus can produce a scatterplot or pairs plot showing a matrix of scatterplots for each pair of variables • S Plus – use ‘splom’ function • R – use ‘pairs’ function • Code for pairs plot (page 9 V&R): > pairs(hills)
Plots of Surfaces • The following displays a function defined on a two-dimensional regular grid • ‘contour’ function • ‘image’ function • ‘persp’ function
Code for Contour Plot • Topo data has 3 variables (page 76 V&R) > library(MASS) > 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.lo<-predict(topo.loess, expand.grid(topo.mar)) > par(pty="s") > contour(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", ylab="y axis", + levels = seq(700,1000,25), cex=0.7)
> image(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", ylab="y axis")
> persp(topo.mar$x, topo.mar$y, topo.lo, xlab="x axis", ylab="y axis", col="red")
Star Plot • mtcars data has several variables including: car name, mpg, hp, weight, etc. (code from star function description in R) > data(mtcars) > palette(rainbow(12, s=0.6, v=0.75)) > stars(mtcars[,1:7], len=0.8, key.loc=c(12,1.5), + main="Car Trends", draw.segments=TRUE)
The End • Any questions?