an introduction to r graphics

14
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012

Upload: truda

Post on 05-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

An Introduction to R graphics. Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012. R graphics – Nice and Simple. R has powerful graphics facilities for the production of publication-quality diagrams and plots. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: An Introduction to R graphics

An Introduction to R graphics

Cody Chiuzan

Division of Biostatistics and EpidemiologyComputing for Research I, 2012

Page 2: An Introduction to R graphics

R graphics – Nice and Simple

R has powerful graphics facilities for the production of publication-quality diagrams and plots.

Can produce traditional plots as well as grid graphics.

Great reference: Murrell P., R Graphics

Page 3: An Introduction to R graphics
Page 4: An Introduction to R graphics

Topics for today

Histograms

Plot, points, lines, legend, xlab, ylab, main, xlim, ylim, pch, lty, lwd.

Scatterplot matrix

Individual profiles

3D graphs

Page 5: An Introduction to R graphics

Data Puromycin – Before and After

Page 6: An Introduction to R graphics

R code

Data available in R; for a full description: help(Puromycin). We will start with the basic command plot() and tackle each

parameter.

Generate multiple graphs in the same window using: par(mfrow).

For a better understanding use help().

Page 7: An Introduction to R graphics

Change parameters using par()

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

Page 8: An Introduction to R graphics

Par examples modifiable from within plotting functions

bg – plot background color lty – line type (e.g. dot, dash, solid) lwd – line width col – color cex – text size inside plot xlab, ylab – axes labels main – title pch – plotting symbol … and many more (learn as you need them)

Page 9: An Introduction to R graphics

Plotting symbols for pch

Great website for choosing colors:

http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.pdf

Page 10: An Introduction to R graphics

Multiple plots

The number of plots on a page, and their placement on the page, can be controlled using par() or layout().

The number of figure regions can be controlled using mfrow and mfcol.

e.g. par(mfrow=c(3,2)) # Creates 6 figures arranged in 3 rows and 2 columns

Layout() allows the creation of multiple figure regions of unequal sizes.

e.g. layout(matrix(c(1,2)), heights=c(2,1))

Page 11: An Introduction to R graphics

Graph using statistical function output

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

e.g. Produce diagnostic plots from a linear model analysis (see R code)

# Reg = lm() # plot(Reg)

hclust() agnes() # hierarchical cluster analysis

Page 12: An Introduction to R graphics

Save the output

Specify destination of graphics output or simply right click and copy

Could be files Not Scalable

JPG # not recommended, introduces blurry artifacts around the lines BMP PNG

Scalable: Postscript # preferred in LaTex Pdf # great for posters

Page 13: An Introduction to R graphics

Save the output

setwd("") # this is where the plot will be savedpdf(file="Puromycin.pdf“, width = , height = , res = )dev.off()

Page 14: An Introduction to R graphics

Next - 3D graphs