An Introduction to R graphics Elizabeth Garrett-Mayer Slides borrowed from Cody Chiuzan & Delia Voronca March 24, 2014.

Slides:



Advertisements
Similar presentations
Introduction to R Graphics
Advertisements

Graphics in R data analysis and visualization Katia Oleinik Scientific Computing and Visualization Boston University
Jeopardy Multiple Choice Fill in the Blank Modified T/F Vocabulary Positioning of data Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400.
R graphics  R has several graphics packages  The plotting functions are quick and easy to use  We will cover:  Bar charts – frequency, proportion 
CREATING A MULTIPLE PAGE REPORT Presented by: Dr. Ennis-Cole.
MICROSOFT OFFICE 2013 EXCEL 1. 2 File Tab 1 Title Bar 5. Group 7 Name Box 6 Active Cell 8 Formula Bar 4 Ribbon 9 Column 10. Row 11 Sheet Tabs 12 View.
Fundamental Features of Graphs All graphs have two, clearly-labeled axes that are drawn at a right angle. –The horizontal axis is the abscissa, or X-axis.
FIRST COURSE Excel Tutorial 4 Working with Charts and Graphics.
Customizing Graphs Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces.
Evan Girvetz Winkenwerder Introduction to Graphics in R © R Foundation, from
FISH 397C Winter 2009 Evan Girvetz More Complex Graphics in R © R Foundation, from
MATLAB Week 3 17 November Outline Graphics – Basic plotting – Editing plots from GUI – Editing plots from m-file – Advanced plotting commands.
COMPREHENSIVE Excel Tutorial 4 Working with Charts and Graphics.
Baburao Kamble (Ph.D) University of Nebraska-Lincoln Data Analysis Using R Week5: Charts/Plots in R.
Use a Large Bold Type for the Main Title Use Smaller Type for the Subtitle. Above type is 96 pt, this type is 66 pt Make Authors’ names smaller. This is.
Lesson 5: Using Tables.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
Excel Part 2 Formatting a Workbook. XP Objectives Format text, numbers, and dates Change font colors and fill colors Merge a range into a single cell.
AN INTRODUCTION TO GRAPHICS IN R. Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level.
Graphics in R. X
Baburao Kamble (Ph.D) University of Nebraska-Lincoln
R Graphics Lori Shepherd-Kirchgraber May 22, 2012.
R-Graphics Day 2 Stephen Opiyo. Basic Graphs One of the main reasons data analysts turn to R is for its strong graphic capabilities. R generates publication-ready.
Creating Graphs in R Graphing techniques Sharon style! (I am not sure if this is a good or bad thing but this is what I do...)
Ranjeet Department of Physics & Astrophysics University of Delhi Working with Origin.
Chapter 9 Creating and Designing Graphs. Creating a Graph A graph is a diagram of data that shows relationship among a set of numbers. Data can be represented.
Microsoft Office Illustrated Introductory, Premium Edition with Charts Working.
Creating Charts for the Agency Budget Creating Budget Charts, Slide 1Copyright © 2004, Jim Schwab, University of Texas at Austin.
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
Microsoft ® Office Excel 2007 Working with Charts.
Copyright 2006 South-Western/Thomson Learning Chapter 12 Tables.
Histo-Labels Easy to use and fun to setup. Create and edit all label menus to fulfill your slide labeling requirements. Print to any printer installed.
Learn R Toolkit D Kelly O'DayBox, Dot, Histogram, Strip Charts Mod 5 –Box Charts: 1 Module 5 Box, Dot, Histogram, Strip Charts Do See & HearRead Learn.
Session 9 Managerial Spreadsheet Modeling -- Prof. Juran1.
R-Graphics Stephen Opiyo. Basic Graphs One of the main reasons data analysts turn to R is for its strong graphic capabilities. R generates publication-ready.
PROC GPLOT GPLOT is used to make two dimensional scatter-plots. General Syntax: proc gplot data=data-set options; plot y-variable*x-variable/options; run;
CRSD Technology Training Tony Judice. Quick Access Toolbar – can be modifiedSave as… allows you to save the file to a different location and also as an.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff
Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1.
Excel 2007 Part (3) Dr. Susan Al Naqshbandi
Plotting Complex Figures Using R
ElVis Improvements Summer 2008 Eric Zatz PPPL Summer Intern Mentor – Eliot Feibush August 11, 2008.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Positioning Objects with CSS and Tables
Excel Part 4 Working with Charts and Graphics. XP Objectives Create an embedded chart Work with chart titles and legends Create and format a pie chart.
Excel Part 4 Working with Charts and Graphics. XP Objectives Create an embedded chart Work with chart titles and legends Create and format a pie chart.
Introduction to plotting data Fish 552: Lecture 4.
Microsoft Office XP Illustrated Introductory, Enhanced With Charts Working.
EGR 106 – Week 5 – 2-D Plots Question : Why do engineers use plots? Answer : To analyze, visualize, and present data. Matlab has many useful plotting options.
Review > mean(humidity, na.rm=T) > humidity[!is.na(humidity)] > X x Y Y[[1]][2,3] > zone.fac
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Statistical Programming Using the R Language Lecture 2 Basic Concepts II Darren J. Fitzpatrick, Ph.D April 2016.
Permeability (% of Control)
Permeability (% of Control)
Working with Charts © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using R Graphs in R.
Lecture 25: Exploring data
Chapter 8: ODS Graphics ODS graphics were not available prior to SAS 9.2 They have been implemented across a wide range of procedures Functionality isn’t.
Trevor A. Branch FISH 552 Introduction to R
Microsoft Excel Illustrated
Microsoft Office Illustrated Introductory, Windows XP Edition
Plotting Signals in MATLAB
Introduction to R plot Instructor: Li, Han
Use a Large Bold Type for the Main Title (80 pt):
Permeability (% of Control)
Use a Large Bold Type for the Main Title (80 pt):
Use a Large Bold Type for the Main Title (70 pt):
Permeability (% of Control)
Use a Large Bold Type for the Main Title (70 pt):
Presentation transcript:

An Introduction to R graphics Elizabeth Garrett-Mayer Slides borrowed from Cody Chiuzan & Delia Voronca March 24, 2014

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

Topics for today  Histograms  Plot, points, lines, legend, xlab, ylab, main, xlim, ylim, pch, lty, lwd.  Scatterplot matrix  Individual profiles  3D graphs

Useful Plots  Quantile-quantile plots qqnorm(x) qqline(x) Quantile – Quantile-Quantile plot qqplot(x, y)

Useful Plots  Barpot barplot(table(x1, x2), legend=c(“x1.grp1", “x1.grp2"), xlab="X2“, beside=TRUE) Or library(lattice) barchart(table(x1,x2,x3))

Useful Plots  Boxplots and Violin Plots boxplot(x)  horizontal = TRUE library(vioplot) vioplot(x1, x2, x3) Side-by-side boxplots boxplot(y~x) Or library(lattice) bwplot(y~x)

Useful Plots  Interaction plot  Display means by 2 variables (in a two-way analysis of variance) interaction.plot(x1, x2, y)  fun (option to change default statistic which is the mean)

Useful Plots  Empirical probability density plot  Density plots are non-parametric estimates of the empirical probability density function  #univariate density plot(density(x))  One could compare groups by looking at kernel density plots

Useful Plots  3 – D plots persp(x, y, z) contour(x, y, z) Image(x, y, z) OR library(scatterplot3d) scatterplot3d(x, y, z)  The values for x and y must be in ascending order

Data Puromycin – Before and After

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

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.

Options and Parameters  Size of margins  par(mar=c(bot, left, top, right))  Save graphical settings  par() #view currents settings  opar <- par() #make a copy of current settings  par(opar) #restore original settings  Multiple plots per page  par(mfrow=c(a, b)) #a rows and b columns  par(mfcol=c(a,b))

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)

Adding Elements  Add an arbitrary straight line:  plot(x, y)  abline(intercept, slope)  Plot symbols  plot(x, y, pch=pchval) PCH symbols used in R  “col=“ and “bg=” are also specified  PCH can also be in characters such as “A”, “a”, “%” etc.

Adding Elements  Titles  title(main=“main”, sub = “sub”, xlab=“xlab”, ylab=“ylab”)  Mathematical Symbols  plot(x, y)  expr = expression(paste(mathexpression)))  title(xlab=c(expr))  Arrows and Shapes  arrows(x, y)  rect(xleft, ybottom, xright, ytop)  polygon(x, y)  library(plotrix)  draw.circle(x, y, r)

Options and Parameters  Line styles, line width and colors  plot(….)  lines(x, y, lty=ltyval, lwd = lwdval, col=colval)  col Default plotting color. Some functions (e.g. lines) accept a vector of values that are recycled.  col.axis color for axis annotation  col.lab color for x and y labels  col.main color for titles  col.sub color for subtitles  fg plot foreground color (axes, boxes - also sets col= to same)  bg plot background color

Adding Elements  Legend  plot(x, y)  legend(xval, yval, legend = c(“Grp1”, “Grp2”), lty=1:2, col=3:4, bty=“box type”)  Add a legend at the location at (xval, yval)  A vector of legend labels, line types, and colors can be specified using legend, lty and col options.  bty =“o” or “n”

Adding Elements  Adding Points or Lines to an Existing Graphic  plot(x, y)  points(x, y)  lines(x, y, type=“type”) type =  p points  l lines  o overplotted points and lines  b, c points (empty if "c") joined by lines  s, S stair steps  h histogram-like vertical lines  n does not produce any points or lines  OLS line fit to the points  plot(x, y)  abline(lm(y~x))

Options and Parameters  Graph Size  pdf(“filename.pdf”, width = Xin, height = Yin)  Point and text size  plot(x, y, cex = cexval)  cex number indicating the amount by which plotting text and symbols should be scaled relative to the default. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller, etc.  cex.axis magnification of axis annotation relative to cex  cex.lab magnification of x and y labels relative to cex  cex.main magnification of titles relative to cex  cex.sub magnification of subtitles relative to cex  Box around plots  plot(x, y, bty = btyval)

Options and Parameters  Axis labels, values, and tick marks  plot(x, y, lab=c(x, y, len), #number of tick marks las=lasval, #orientation of tick marks tck = tckval, #length of tick marks xaxp = c(x1, x2, n), #coordinates of the extreme tick marks yaxp = c(x1, x2, n), xlab = “X axis label”, ylab=“Y axis label”)  las = 0 labels are parallel to axis  las=2 labels are perpendicular to axis  tck = 0 suppresses the tick mark

Options and Parameters  Axis Range and Style  plot(x, y, xlim = c(minx, maxx), ylim = c (miny, maxy), xaxs=“i”, yaxs=“i”)  The xaxs and yaxs control whether the tick marks extend beyond the limits of the plotted observations (default) or are constrained to be internal (“i”)  See also:  axis()  mtext()  Omit axis  plot(x, y, xaxt = “n”, yaxy=“n”)

Options and Parameters  Fonts  font Integer specifying font to use for text. 1=plain, 2=bold, 3=italic, 4=bold italic, 5=symbol  font.axis font for axis annotation  font.lab font for x and y labels  font.main font for titles  font.sub font for subtitles  ps font point size (roughly 1/72 inch) text size=ps*cex  family font family for drawing text. Standard values are "serif", "sans", "mono", "symbol".

Options and Parameters  More on how to change colors  You can specify colors in R by index, name, hexadecimal, or RGB.  For example col=1, col="white", and col="#FFFFFF" are equivalent.  colors() #list of color names

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

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

Saving graphs  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

Saving Graphs  pdf(“file.pdf”)  plot(….)  dev.off()  jpeg(“file.jpeg”)  plot(…)  dev.off()  win.metafile(file.wmf)  plot(…)  dev.off()  Similar code for BMP, TIFF, PNG, POSTSCRIPT  PNG is usually recommended  The dev.off() function is used to close the graphical device

3D graphs

In Class Activity  Any ideas on how to reproduce this graph?  What are some things you need to know? 1. Data and ICC formula 2. Add a title 3. Change axis labels 4. Change tick marks 5. Change color 6. Add legend 7. Change font and size 8. *Use a for loop

References  SAS and R – data Management, Statistical Analysis, and Graphics, Ken Kleinman and Nicholas J. Horton  Quick – R :  R help