Presentation is loading. Please wait.

Presentation is loading. Please wait.

AN INTRODUCTION TO GRAPHICS IN R. Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level.

Similar presentations


Presentation on theme: "AN INTRODUCTION TO GRAPHICS IN R. Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level."— Presentation transcript:

1 AN INTRODUCTION TO GRAPHICS IN R

2 Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level Plotting Functions Low-Level Plotting Functions Useful functions in conjunction with graphics Useful functions in conjunction with graphics –Expand your functional toolbox par par Devices Devices (WE WILL RETURN TO THESE CONCEPTS IN PRACTICAL EXAMPLES) PRACTICAL/EXPERIMENTATION PRACTICAL/EXPERIMENTATION

3 Overview One of the best things about R is the ability to create publication-quality graphs One of the best things about R is the ability to create publication-quality graphs Graph function syntax follows general functional rules of the R language: the fuzzy boundaries between dataset management, statistical analysis, and graphics ultimately is an advantage to the language, but can be a little overwhelming at first. Graph function syntax follows general functional rules of the R language: the fuzzy boundaries between dataset management, statistical analysis, and graphics ultimately is an advantage to the language, but can be a little overwhelming at first. Corollary: Understanding graphics will facilitate understanding R programming in general---and it gives pretty instant feedback Corollary: Understanding graphics will facilitate understanding R programming in general---and it gives pretty instant feedback There are numerous functions available for producing diverse output, with many commonalities in syntax between them There are numerous functions available for producing diverse output, with many commonalities in syntax between them There also are many ways to accomplish a given task. There also are many ways to accomplish a given task. How a plotting function deals with data often depends on the data type (e.g. matrix, factor, vector) given to it How a plotting function deals with data often depends on the data type (e.g. matrix, factor, vector) given to it

4

5

6 Ooooh….Aaaah…

7 A graph comprised of multiple plots par(“mfrow”=c(2,1)) #change default behavior of plot function to plot 2 graphs per device

8 And more: “ multi-bivariate” visualization…

9 Plotting Commands 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 and y boundaries). 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 and y boundaries). Low-Level: additions to existing plots (e.g. lines, additional series, text, shapes) Low-Level: additions to existing plots (e.g. lines, additional series, text, shapes) JUST LIKE OTHER COMMANDS IN R, PLOTTING USES FUNCTIONS THAT TAKE 1 OR MORE ARGUMENTS, WITH PRESET ARGUMENTS THAT CAN BE CHANGED AS DESIRED JUST LIKE OTHER COMMANDS IN R, PLOTTING USES FUNCTIONS THAT TAKE 1 OR MORE ARGUMENTS, WITH PRESET ARGUMENTS THAT CAN BE CHANGED AS DESIRED par “commands”: default plotting parameters that can be modified to change global behavior of subsequent commands. In essence, you are changing your global preferences for graphing. Some of the par variables can be temporarily changed by adding arguments to high or low level plotting commands; in this case, the par variable is changed for that command only. par “commands”: default plotting parameters that can be modified to change global behavior of subsequent commands. In essence, you are changing your global preferences for graphing. Some of the par variables can be temporarily changed by adding arguments to high or low level plotting commands; in this case, the par variable is changed for that command only.

10 Examples of high level plotting commands: plot() plot() hist() hist() boxplot() boxplot() barplot() barplot() dotchart() dotchart() pie() pie() qqplot() qqplot() qqnorm() qqnorm() pairs() pairs() “3D” functions “3D” functions –heatmap() –image() –persp() –contour() –filled.contour() –heatmap.2()

11 Low-level plotting commands points() points() lines() lines() abline() abline() arrows() arrows() segments() segments() rug() rug() … text() text() mtext() mtext() legend() legend() polygon() polygon() rectangle() rectangle() …

12 GRAPH COMMANDS ARE FUNCTIONS and can call other functions sort() sort() sum() sum() density() density() lowess() lowess() spline() spline() ifelse() ifelse() …

13 Par A list of graphical parameters that define the default behavior of all plot functions. A list of graphical parameters that define the default behavior of all plot functions. Just like other R objects, par elements are similarly modifiable, with slightly different syntax Just like other R objects, par elements are similarly modifiable, with slightly different syntax –e.g. par(“bg”=“lightcyan”) –This would change the background color of all subsequent plots to light cyan When par elements are modified directly (as above, this changes all subsequent plotting behavior When par elements are modified directly (as above, this changes all subsequent plotting behavior Some par elements can be modified from within high and low level plotting functions. In this case, Some par elements can be modified from within high and low level plotting functions. In this case,

14 Par parameter examples often modifiable from within plotting functions bg – plot background color bg – plot background color lty – line type (e.g. dot, dash, solid) lty – line type (e.g. dot, dash, solid) lwd – line width lwd – line width col – color col – color cex – text size inside plot cex – text size inside plot mex – text size in margins mex – text size in margins mfcol/mfrow – multiple plot option mfcol/mfrow – multiple plot option –2 element vector (#rows,#cols) … many, many more (you tend to learn them as you need them)

15 Add On Packages A huge number of additional high and low-level plotting functions are available within add-on packages. Genetics and social sciences are particularly well- represented. A huge number of additional high and low-level plotting functions are available within add-on packages. Genetics and social sciences are particularly well- represented. plot using the enhanced layout capabilities of the lattice package

16

17 Statistical Function Output Many statistical functions (regression, cluster analysis) create special objects. Some are special. These arguments will automatically format graphical output in a specific way. Many statistical functions (regression, cluster analysis) create special objects. Some are special. These arguments will automatically format graphical output in a specific way. e.g. e.g. lm() lm() hclust() hclust() agnes() agnes()

18 Devices Specify Destination of Graphics Output Specify Destination of Graphics Output Could be windows in R Could be windows in R Could be files Could be files –Not Scalable JPG JPG BMP BMP PNG PNG –Scalable: Postscript Postscript Pdf Pdf pictex pictex –Others Win.metafile Win.metafile

19 Practical 1) Demonstrate Tools (me) 2) Put them to use to solve silly problems (you)

20 WHO LEFT OUT THE MILK CLUB MILK?

21 FALSE COLOR IMAGERY “MUG SHOT” CHARACTERISTIC SPLATTER PATTERN

22 CRIME SCENE (AERIAL VIEW) MILK SPILL PERP’s PEN? OBLIQUE CARTON PLACEMENT: EVIDENCE OF ANTISOCIAL BEHAVIOR

23 Practical Make sure that the.Rdata file and the.R file are in the same folder. Make sure that the.Rdata file and the.R file are in the same folder. In Windows, double-clicking on an.RData file will 1) fire up R, 2) load the objects in the file, and 3) change working directory to the file location. Then open today’s R script. In Windows, double-clicking on an.RData file will 1) fire up R, 2) load the objects in the file, and 3) change working directory to the file location. Then open today’s R script. Alternatively, one can open the R script first, setwd(), and then use the load("RgraphExamples.RData") command in the script to load the objects for analysis Alternatively, one can open the R script first, setwd(), and then use the load("RgraphExamples.RData") command in the script to load the objects for analysis


Download ppt "AN INTRODUCTION TO GRAPHICS IN R. Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level."

Similar presentations


Ads by Google