Introduction to R Graphics

Slides:



Advertisements
Similar presentations
Summary Statistics/Simple Graphs in SAS/EXCEL/JMP.
Advertisements

ESTEC Photo Club How to improve Contrast & Brightness in your Photos Roberto Donadio TIA/TTG.
Introduction to Lattice Graphics Richard Pugh 4th December 2012.
Graphics in R data analysis and visualization Katia Oleinik Scientific Computing and Visualization Boston University
Chapter 2 Minitab for Data Analysis KANCHALA SUDTACHAT.
R graphics  R has several graphics packages  The plotting functions are quick and easy to use  We will cover:  Bar charts – frequency, proportion 
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.
DATA VISUALIZATION UNIVARIATE (no review- self study) STEM & LEAF BOXPLOT BIVARIATE SCATTERPLOT (review correlation) Overlays; jittering Regression line.
SPSS Statistical Package for the Social Sciences is a statistical analysis and data management software package. SPSS can take data from almost any type.
Introduction to SPSS Short Courses Last created (Feb, 2008) Kentaka Aruga.
How To Make Graphs in Microsoft Excel Outline Making Bar Graphs Making Scatter Plots – 1 series Making Scatter Plots – Multiple Series.
Examining Univariate Distributions Chapter 2 SHARON LAWNER WEINBERG SARAH KNAPP ABRAMOWITZ StatisticsSPSS An Integrative Approach SECOND EDITION Using.
Let’s Review for… AP Statistics!!! Chapter 1 Review Frank Cerros Xinlei Du Claire Dubois Ryan Hoshi.
SW388R7 Data Analysis & Computers II Slide 1 Assumption of Homoscedasticity Homoscedasticity (aka homogeneity or uniformity of variance) Transformations.
How do scientists show the results of investigations?
Thinking Mathematically
An Introduction to R graphics Elizabeth Garrett-Mayer Slides borrowed from Cody Chiuzan & Delia Voronca March 24, 2014.
Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National Taiwan University.
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 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.
Engineering H193 - Team Project Spring Quarter Gateway Engineering Education Coalition P. 1 Data Analysis and Presentation for Written and Oral Reports.
Examples of different formulas and their uses....
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
Honors Physics Lab Journals Graphing. Lab Journal Entries  Number pages in consecutive order  Date all entries  Title all activities and investigations.
Introduction to SPSS. Object of the class About the windows in SPSS The basics of managing data files The basic analysis in SPSS.
Graphs in Physics PowerPoint #4. A graph is… A convenient way to show data.
Plot Using Excel. Select Data Insert →Charts →Scatter.
Notes 9.6 – Statistics and Data - Graphically. I. Variables A.) Def: characteristics of individuals being identified or measured. 1.) CATEGORICAL – Class.
Chapter 3 – Graphical Displays of Univariate Data Math 22 Introductory Statistics.
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.
Area of a circle Radius r (m) Area A (m 2 ) when r = 2.5 A = 20 (to 2sf) when A = 30 r = 3.1 (to 2sf)
LSP 120: Quantitative Reasoning and Technological Literacy Topic 1: Introduction to Quantitative Reasoning and Linear Models Lecture Notes 1.3 Prepared.
Plotting Complex Figures Using R
Chapter 16 Exploratory data analysis: numerical summaries CIS 2033 Based on Textbook: A Modern Introduction to Probability and Statistics Instructor:
Data Science and Big Data Analytics Chap 3: Data Analytics Using R
Statistical Data Analysis 2011/2012 M. de Gunst Lecture 2.
2-3 LINEAR FUNCTIONS AND SLOPE- INTERCEPT FORM. OBJECTIVES To graph linear equations –Linear function- a function whose graph is a line To write linear.
MATLAB ® for Engineers, Holly Moore Fourth Edition, Global Edition © Pearson Education Limited 2015 All rights reserved. Figure 5.1 Simple Plot of Time.
2.2 Stem & Leaf Plots, Dot Plots and Shape. 2 Stem-and-Leaf Displays Stem-and-leaf displays contain more information than frequency distributions and.
Presenting Multivariate Data Harry R. Erwin, PhD School of Computing and Technology University of Sunderland.
Introduction to plotting data Fish 552: Lecture 4.
IENG-385 Statistical Methods for Engineers SPSS (Statistical package for social science) LAB # 1 (An Introduction to SPSS)
Linear Equation in Two Variables
Inference for Regression (Chapter 14) A.P. Stats Review Topic #3
Using R Graphs in R.
Graphic display of data
1.6 A Library of Parent Functions
DEPARTMENT OF COMPUTER SCIENCE
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.
Using Excel to Graph Data
Using Excel to Graph Data
Linear Equations in two variables
Recoding II: Numerical & Graphical Descriptives
INTRODUCTION TO SGPLOT Zahir Raihan OVERVIEW  ODS Graphics  SGPLOT overview  Plot Content  High value plot statements  High value plot options 
You might want to start your title ‘A graph to show....’
Presentation, data and programs at:
Graphic display of data
9.2 Representing Linear Functions
Program This course will be dived into 3 parts: Part 1 Descriptive statistics and introduction to continuous outcome variables Part 2 Continuous outcome.
Plotting Signals in MATLAB
Using Excel to Graph Data
How to Create Tables & Graphs in Excel
Association between 2 variables
The Normal Distribution
Simulate Multiple Dice
Let’s review some of the statistics you’ve learned in your first class: Univariate analyses (single variable) are done both graphically and numerically.
1: Slope from Equations Y = 8x – 4 B) y = 6 – 7x
Introductory Statistics
Presentation transcript:

Introduction to R Graphics Delia Voronca 2013

Objectives Create basic graphical displays such as scatter plots, boxplots, histograms, interaction plots and 3-D plots Change plot symbols, add an arbitrary straight line, add points or lines, add an OLS line fit to points, add a normal density curve to a histogram Add titles, footnotes, mathematical symbols, arrows and shapes Add a legend Change size of the graph, point, text, margins, put multiple plots per page Changes axis, line styles, add colors Save graphs

Useful Plots Scatterplot Multiple y values plot(x, y) plot(x, y1) points(x, y2) points(x, y3) ... Use pch=“d” and pch=“h” to change the plotting symbols

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 Histogram Stem-and-leaf Plot hist(x) stem(x) The decimal point is at the | 1 | 0000000 2 | 0000000000 3 | 000 4 | 0000000000 5 | 6 | 0 7 | 8 | 0

Useful Plots Side-by-side boxplots 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 Quantile-quantile plots qqnorm(x) qqline(x) Quantile – Quantile-Quantile plot qqplot(x, y)

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

Adding Elements Add an arbitrary straight line: Plot symbols 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 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 abline(lm(y~x))

Adding Elements Add a Normal Curve h<-hist(x, breaks=10, col="red", xlab=“Xlab", main="Histogram with Normal Curve") xfit<-seq(min(x),max(x),length=40) yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) yfit <- yfit*diff(h$mids[1:2])*length(x) lines(xfit, yfit, col="blue", lwd=2)

Adding Elements Titles Mathematical Symbols Arrows and Shapes 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)

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”

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

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

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

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

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

Go Over R Code

In Class Activity Any ideas on how to reproduce this graph? What are some things you need to know? Data and ICC formula Add a title Change axis labels Change tick marks Change color Add legend Change font and size *Use a for loop

In Class Activity Let’s go over the code Questions Homework

References SAS and R – data Management, Statistical Analysis, and Graphics, Ken Kleinman and Nicholas J. Horton Quick – R : http://www.statmethods.net/index.html R help