1 / 30

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

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

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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

  4. 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

  5. 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 )

  6. 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"))

  7. Bar Chart Example cont.

  8. 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…

  9. Bar Chart with beside=T & horiz=T

  10. 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)

  11. 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

  12. 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

  13. 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)”.

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

  15. 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):

  16. 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)

  17. Output obtained after R-code was run:

  18. 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

  19. 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’

  20. Interactive Graph

  21. 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)

  22. Pairs Plot in R

  23. Plots of Surfaces • The following displays a function defined on a two-dimensional regular grid • ‘contour’ function • ‘image’ function • ‘persp’ function

  24. 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)

  25. Contour Plot

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

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

  28. 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)

  29. Star Plot

  30. The End • Any questions?

More Related