an introduction to graphics in r. today overview overview –gallery of r graph examples high-level...

23
AN INTRODUCTION TO GRAPHICS IN R

Upload: carmella-beasley

Post on 26-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

AN INTRODUCTION TOGRAPHICS IN R

TodayToday

OverviewOverview– Gallery of R Graph Gallery of R Graph

examplesexamples High-Level Plotting High-Level Plotting

FunctionsFunctions Low-Level Plotting Low-Level Plotting

FunctionsFunctions Useful functions in Useful functions in

conjunction with conjunction with graphics graphics – Expand your functional Expand your functional

toolboxtoolbox par par DevicesDevices

(WE WILL RETURN TO THESE CONCEPTS (WE WILL RETURN TO THESE CONCEPTS IN PRACTICAL EXAMPLES)IN PRACTICAL EXAMPLES)

PRACTICAL/EXPERIMENTATIONPRACTICAL/EXPERIMENTATION

OverviewOverview

One of the best things about R is the One of the best things about R is the ability to create publication-quality ability to create publication-quality graphsgraphs

Graph function syntax follows Graph function syntax follows general functional rules of the R general functional rules of the R language: the fuzzy boundaries language: the fuzzy boundaries between dataset management, between dataset management, statistical analysis, and graphics statistical analysis, and graphics ultimately is an advantage to the ultimately is an advantage to the language, but can be a little language, but can be a little overwhelming at first.overwhelming at first.

Corollary: Understanding graphics Corollary: Understanding graphics will facilitate understanding R will facilitate understanding R programming in general---and it programming in general---and it gives pretty instant feedbackgives pretty instant feedback

There are numerous functions There are numerous functions available for producing diverse available for producing diverse output, with many commonalities in output, with many commonalities in syntax between themsyntax between them

There also are many ways to There also are many ways to accomplish a given task.accomplish a given task.

How a plotting function deals with How a plotting function deals with data often depends on the data type data often depends on the data type (e.g. matrix, factor, vector) given to (e.g. matrix, factor, vector) given to itit

Ooooh….AaaahOoooh….Aaaah……

A graph comprised of multiple plotsA graph comprised of multiple plots

par(“mfrow”=c(2,1)) #change default behavior of plot function to plot 2 graphs per device

And more:And more: “ “multi-bivariate” multi-bivariate” visualization…visualization…

Plotting CommandsPlotting Commands

High-level: create new graphs that inherit properties of one type of High-level: create new graphs that inherit properties of one type of plot or another and (e.g. scatterplot) and define graphspace (e.g. x plot or another and (e.g. scatterplot) and define graphspace (e.g. x and y boundaries). and y boundaries).

Low-Level: additions to existing plots (e.g. lines, additional series, Low-Level: additions to existing plots (e.g. lines, additional series, text, shapes)text, shapes)

JUST LIKE OTHER COMMANDS IN R, PLOTTING USES FUNCTIONS JUST LIKE OTHER COMMANDS IN R, PLOTTING USES FUNCTIONS THAT TAKE 1 OR MORE ARGUMENTS, WITH PRESET ARGUMENTS THAT TAKE 1 OR MORE ARGUMENTS, WITH PRESET ARGUMENTS THAT CAN BE CHANGED AS DESIREDTHAT CAN BE CHANGED AS DESIRED

par “commands”: default plotting parameters that can be modified par “commands”: default plotting parameters that can be modified to change global behavior of to change global behavior of subsequentsubsequent commands. In essence, commands. In essence, you are changing your global preferences for graphing. Some of the you are changing your global preferences for graphing. Some of the par variables can be par variables can be temporarily temporarily changed by adding arguments to changed by adding arguments to high or low level plotting commands; in this case, the par variable is high or low level plotting commands; in this case, the par variable is changed for that command only.changed for that command only.

Examples of high level Examples of high level plotting commands:plotting commands: plot()plot() hist()hist() boxplot()boxplot() barplot()barplot() dotchart()dotchart() pie()pie() qqplot()qqplot() qqnorm()qqnorm() pairs()pairs()

““3D” functions3D” functions– heatmap()heatmap()– image()image()– persp()persp()– contour()contour()– filled.contour()filled.contour()– heatmap.2()heatmap.2()

Low-level plotting Low-level plotting commandscommands points()points()

lines()lines() abline()abline() arrows()arrows() segments()segments()

rug()rug()

……

text()text() mtext()mtext() legend()legend()

polygon()polygon() rectangle()rectangle()

……

GRAPH COMMANDS ARE FUNCTIONSGRAPH COMMANDS ARE FUNCTIONSand can call other functions and can call other functions

sort()sort() sum()sum() density()density() lowess()lowess() spline()spline() ifelse()ifelse() ……

ParPar

A list of graphical parameters that define the A list of graphical parameters that define the default behavior of all plot functions.default behavior of all plot functions.

Just like other R objects, par elements are Just like other R objects, par elements are similarly modifiable, with slightly different syntaxsimilarly modifiable, with slightly different syntax– e.g. par(“bg”=“lightcyan”)e.g. par(“bg”=“lightcyan”)– This would change the background color of all This would change the background color of all

subsequent plots to light cyansubsequent plots to light cyan When par elements are modified directly (as When par elements are modified directly (as

above, this changes all subsequent plotting above, this changes all subsequent plotting behaviorbehavior

Some par elements can be modified from within Some par elements can be modified from within high and low level plotting functions. In this case, high and low level plotting functions. In this case,

Par parameter examples often modifiable Par parameter examples often modifiable from within plotting functionsfrom within plotting functions

bg – plot background colorbg – plot background color lty – line type (e.g. dot, dash, solid)lty – line type (e.g. dot, dash, solid) lwd – line widthlwd – line width col – colorcol – color cex – text size inside plotcex – text size inside plot mex – text size in marginsmex – text size in margins mfcol/mfrow – multiple plot option mfcol/mfrow – multiple plot option

– 2 element vector (#rows,#cols)2 element vector (#rows,#cols)

… … many, many more (you tend to learn them as you many, many more (you tend to learn them as you need them)need them)

Add On PackagesAdd On Packages

A A hugehuge number of additional number of additional high and low-level plotting high and low-level plotting functions are available within functions are available within add-on packages. Genetics add-on packages. Genetics and social sciences are and social sciences are

particularly well-represented.particularly well-represented.

plot using the enhanced layout capabilities of the lattice package

Statistical Function OutputStatistical Function Output

Many statistical functions (regression, Many statistical functions (regression, cluster analysis) create special objects. cluster analysis) create special objects. Some are special. These arguments Some are special. These arguments will automatically format graphical will automatically format graphical output in a specific way. output in a specific way.

e.g.e.g. lm()lm() hclust()hclust() agnes()agnes()

DevicesDevices

Specify Destination of Graphics OutputSpecify Destination of Graphics Output Could be windows in RCould be windows in R Could be files Could be files

– Not ScalableNot Scalable JPGJPG BMPBMP PNGPNG

– Scalable:Scalable: PostscriptPostscript PdfPdf pictexpictex

– OthersOthers Win.metafileWin.metafile

PracticalPractical

1)1) Demonstrate Tools (me)Demonstrate Tools (me)

2)2) Put them to use to solve silly Put them to use to solve silly problems (you)problems (you)

WHO LEFT OUT THE MILK CLUB MILK?

FALSE COLOR IMAGERY

“MUG SHOT”

CHARACTERISTICSPLATTERPATTERN

CRIME SCENE (AERIAL VIEW)

MILK SPILL

PERP’s PEN?

OBLIQUE CARTON PLACEMENT: EVIDENCEOF ANTISOCIAL BEHAVIOR

PracticalPractical

Make sure that the .Rdata file and the .R file Make sure that the .Rdata file and the .R file are in the same folder.are in the same folder.

In Windows, double-clicking on an .RData In Windows, double-clicking on an .RData file will 1) fire up R, 2) load the objects in file will 1) fire up R, 2) load the objects in the file, and 3) change working directory to the file, and 3) change working directory to the file location. Then open today’s R script.the file location. Then open today’s R script.

Alternatively, one can open the R script Alternatively, one can open the R script first, setwd(), and then use the first, setwd(), and then use the load("RgraphExamples.RData") command load("RgraphExamples.RData") command in the script to load the objects for analysis in the script to load the objects for analysis