An Introduction to R Graphics with examples

Similar documents
R Graphics. Feng Li School of Statistics and Mathematics Central University of Finance and Economics

STATISTICAL LABORATORY, April 30th, 2010 BIVARIATE PROBABILITY DISTRIBUTIONS

Statistical Programming with R

Graphics - Part III: Basic Graphics Continued

IST 3108 Data Analysis and Graphics Using R Week 9

Module 10. Data Visualization. Andrew Jaffe Instructor

Lab 1 Introduction to R

Data Visualization. Andrew Jaffe Instructor

Az R adatelemzési nyelv

Types of Plotting Functions. Managing graphics devices. Further High-level Plotting Functions. The plot() Function

Dr. Junchao Xia Center of Biophysics and Computational Biology. Fall /6/ /13

A (very) brief introduction to R

Graphics #1. R Graphics Fundamentals & Scatter Plots

Intro to R Graphics Center for Social Science Computation and Research, 2010 Stephanie Lee, Dept of Sociology, University of Washington

AA BB CC DD EE. Introduction to Graphics in R

Continuous-time stochastic simulation of epidemics in R

Namir s R 102 Plotting Tutorial by Namir Shammas Dedication

Advanced Econometric Methods EMET3011/8014

Basic Statistical Graphics in R. Stem and leaf plots 100,100,100,99,98,97,96,94,94,87,83,82,77,75,75,73,71,66,63,55,55,55,51,19

Introduction to the workbook and spreadsheet

Gnuplot. in Action. Understanding Data with Graphs MANNING. (74 w. long.) PHILIPP K. JANERT. Greenwich

Advanced Graphics with R

A Handbook of Statistical Analyses Using R. Brian S. Everitt and Torsten Hothorn

AN INTRODUCTION TO LATTICE GRAPHICS IN R

Visualizing in R advanced plotting

Introduction to R. Biostatistics 615/815 Lecture 23

Plotting: An Iterative Process

B ABC is mapped into A'B'C'

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

DEPARTMENT OF BIOSTATISTICS UNIVERSITY OF COPENHAGEN. Graphics. Compact R for the DANTRIP team. Klaus K. Holst

An Introduction to R 2.2 Statistical graphics

B ABC is mapped into A'B'C'

# R script for Applied Time Series Analysis for Ecologists # Day 1 # 24 March # written by Mark Scheuerell

Golden Software, Inc.

INTRODUCTION TO R. Basic Graphics

Package gibbs.met. February 19, 2015

Charts in Excel 2003

Package gibbs.met documentation

APPM 2460 PLOTTING IN MATLAB

Chapter 5 An Introduction to Basic Plotting Tools

Lecture 6 - Multivariate numerical optimization

Configuring Figure Regions with prepplot Ulrike Grömping 03 April 2018

POWERPOINT Build a Presentation to Remember

R Graphics. Paul Murrell. The University of Auckland. R Graphics p.1/47

Package ExceedanceTools

Analysing Spatial Data in R: Vizualising Spatial Data

Using R. Liang Peng Georgia Institute of Technology January 2005

Package basictrendline

Applied Calculus. Lab 1: An Introduction to R

Book 5. Chapter 1: Slides with SmartArt & Pictures... 1 Working with SmartArt Formatting Pictures Adjust Group Buttons Picture Styles Group Buttons

Topics for today Input / Output Using data frames Mathematics with vectors and matrices Summary statistics Basic graphics

B ABC is mapped into A'B'C'

The RcmdrPlugin.HH Package

Package arphit. March 28, 2019

Linkage analysis with paramlink Appendix: Running MERLIN from paramlink

Points Lines Connected points X-Y Scatter. X-Y Matrix Star Plot Histogram Box Plot. Bar Group Bar Stacked H-Bar Grouped H-Bar Stacked

Representing Graphical Data

form are graphed in Cartesian coordinates, and are graphed in Cartesian coordinates.

SPACE - A Manifold Exploration Program

MATH2070: LAB 3: Roots of Equations

Install RStudio from - use the standard installation.

R Graphics. SCS Short Course March 14, 2008

MTH 122 Calculus II Essex County College Division of Mathematics and Physics 1 Lecture Notes #11 Sakai Web Project Material

From Getting Started with the Graph Template Language in SAS. Full book available for purchase here.

y= sin( x) y= cos( x)

Package RCEIM. April 3, 2017

BusinessObjects Frequently Asked Questions

Display Lists in grid

Multivariate Numerical Optimization

3D Surface Plots with Groups

Graphics in R STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley

R Visualizing Data. Fall Fall 2016 CS130 - Intro to R 1

Making plots in R [things I wish someone told me when I started grad school]

Complex Numbers, Polar Equations, and Parametric Equations. Copyright 2017, 2013, 2009 Pearson Education, Inc.

Computation for the Introduction to MCMC Chapter of Handbook of Markov Chain Monte Carlo By Charles J. Geyer Technical Report No.

Plotting Complex Figures Using R. Simon Andrews v

Transient Stability Analysis with PowerWorld Simulator

R (and S, and S-Plus, another program based on S) is an interactive, interpretive, function language.

Name: Dr. Fritz Wilhelm Lab 1, Presentation of lab reports Page # 1 of 7 5/17/2012 Physics 120 Section: ####

Chapter 2: Graphical Summaries of Data 2.1 Graphical Summaries for Qualitative Data. Frequency: Frequency distribution:

Graphics in IT82. Representing Graphical Data. Graphics in IT82. Lectures Overview. Representing Graphical Data. Logical / Physical Representation

Fathom Dynamic Data TM Version 2 Specifications

Using Built-in Plotting Functions

Display Lists in grid

Tutorial on the R package TDA

Activity: page 1/10 Introduction to Excel. Getting Started

Package areaplot. October 18, 2017

Microsoft Excel 2007

FUNCTIONS AND MODELS

Exploratory Data Analysis - Part 2 September 8, 2005

Excel 2007 Tutorials - Video File Attributes

DSCI 325: Handout 18 Introduction to Graphics in R

8 Project # 2: Bézier curves

Introduction to RStudio

Desktop Studio: Charts. Version: 7.3

Technology Assignment: Scatter Plots

ECE 3793 Matlab Project 1

R version ( ) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN

Getting Started With Excel

R.devices. Henrik Bengtsson. November 19, 2012

Transcription:

An Introduction to R Graphics with examples Feng Li November 18, 2008 1 R graphics system A picture is worth a thousand words! The R graphics system can be broken into four distinct levels: graphics packages; graphics systems; a graphics engine, including standard graphics devices; and graphics device packages (Table 1). Things can be quite complicated because there are two sets of graphical systems: the classical ones and those, more complex but much more powerful, from the lattice and grid packages. The classic graphics system (e.g functions of plot, hist...) are called high-level graphics system but it has a few problems. First, if you choose to divide the screen to display several plots, you cannot go back to an already produced plot to add to it nor even to alter it. Second, you cannot nest those screen divisions. We will have an example in Section 2.2. The grid system is a low-level graphics library that comes form the Trellis graphics in S- Plus. It addresses those problems in the classical graphics method. It is used, for instance, by the lattice library. R Graphics, a very popular book mainly about grid system was written by Paul Murrell. Table 1: The R graphics system -------------------------------------------------------------------- Graphics Packages maps... lattice... ----------------- Graphics Systems graphics(classical) grid ---------------- Graphics Engine & Devices grdevives ------------------------- Graphics Device Packages gtkdevice... -------------------------------------------------------------------- Note: The full description of R graphics packages can be found in CRAN Task View http://cran.r-project.org/web/views/graphics.html R graphics output can be produced in a wide variety of graphical formats (see Table 2). In R s terminology, output is directed to a particular output device and that dictates the output format that will be produced. A device must be created or opened in order to receive graphical output and, for devices that create a File on disk, the device must also be closed in order to complete the output. A complete R graphic work should consider the following steps Department of Statistics, Stockholm University. Email: Feng.Li@stat.su.se 1

windows() pdf(...) plot(...)... dev.off() ## Start your favorite device ## File format and path if you want to save ## Some plot here ## Close the device Table 2: Graphical output formats ------------------------------------------------------------------- Screen/GUI Devices X11() X Window window windows() Microsoft Windows window quartz() Mac OS X Quartz window File Devices postscript() Adobe PostScript File (.ps,.eps) pdf() Adobe PDF File (.pdf) pictex() LATEX PicTEX File xfig() XFIG bitmap() GhostScript conversion to File png() PNG bitmap File jpeg() JPEG bitmap File Devices provided by add-on packages devgtk() GTK window (gtkdevice) devjava() Java Swing window (RJavaDevice) devsvg() SVG File (RSvgDevice) ------------------------------------------------------------------- 2 Some R graphics examples Here are some R graphics examples (mainly about the classical graphical system). The aim for now is simply to provide an overall impression of the range of graphical images that can be produced using R. 2.1 The par function par can be used to set or query graphical parameters. Here are some very common usages. For more details check par function. 2.1.1 The style of axis labels las numeric in 0,1,2,3; the style of axis labels. 0: always parallel to the axis (default), 1: always horizontal, 2: always perpendicular to the axis, 3: always vertical. Here is an example par(mfrow=c(2,2)) # we plot them in a picture par(las=0) plot(0,main="par(las=0) default") par(las=1) plot(0,main="par(las=1)") par(las=2) plot(0,main="par(las=2)") par(las=3) plot(0,main="par(las=3)") 2

2.1.2 The position of axis labels par(mfrow=c(2,2)) par(adj=0) plot(0,main="par(adj=0)",ylab="ylab",xlab="xlab") par(adj=.5) plot(0,main="par(adj=.5)default",ylab="ylab",xlab="xlab") par(adj=.7) plot(1,main="par(adj=.7)",ylab="ylab",xlab="xlab") par(adj=1) plot(1,main="par(adj=1)",ylab="ylab",xlab="xlab") 2.1.3 The type of box par(mfrow=c(2,4)) par(bty="o") plot(0,main="par(bty=\"o\")") par(bty="l") plot(0,main="par(bty=\"l\")") par(bty="7") plot(0,main="par(bty=\"7\")") par(bty="n") plot(0,main="par(bty=\"n\")") par(bty="c") plot(0,main="par(bty=\"c\")") par(bty="u") plot(0,main="par(bty=\"u\")") par(bty="]") plot(0,main="par(bty=\"]\")") 2.1.4 Figure in figure It is very useful to add a small graph in the main graph, the fig argument does exactly what we need. Look at this example x <- rt(100, df=12) # give 100 random t with df 12 hist( x, col = "light blue") # make a histgram op <- par(fig = c(.02,.42,.53,.99),new = TRUE) # prepar to add a new small graph qqnorm(x, xlab = "", ylab = "", main = "", axes = FALSE) # the qqnorm in the small graph qqline(x, col = "red", lwd = 2) # add a qq line in the small graph box(lwd=1) # the line width of the small frame box par(op) # apply 2.1.5 Double axises graph Some economists like double axis graphs. Here how it works in R set.seed(1) # set the seed par(mar=c(5,5,5,5)) # set the margin x1<-sort(rnorm(100,mean=20,sd=5)) # asumption this Interest rate x2<-x1^3 # this is GDP plot(x2,axes=false,type="l",col="blue",xlab="",ylab="") # plot GDP axis(1) # add the axis on bottom axis(2,col="blue") # add the asis on left par(new=true) # add new plot on the current graph 3

plot(x1,,axes=false,type="l",col="red",xlab="",ylab="") # plot Interest rate axis(4,col="red") # add the axis on right # We will give the three axises labels mtext(c("time","gdp(mil. $)","Interest rate(%)"),side=c(1,2,4),line=3) title("gdp with interest rate") # give the main title 2.2 Figures layout When we check section 2.1.3, anything looks ugly? Yes, we have 7 (odds) individual figures but we put them into a 2 4 frame which left a blank cell. To make things pretty we can do it like this: split.screen(c(2,1)) split.screen(c(1,2),2) # split display into two screens(1,2) # split bottom half in two(3,4) screen(1) # active screen (1), the top one plot((1:10)^2,type="l",main="screen(1)") # plot something on screen 1 screen(3) # active screen (3), the bottom left one plot((1:10)^.5,type="l",main="screen(3)") # plot something on screen 3 screen(4) # active screen (4), the bottom right one plot((1:10),type="l",main="screen(4)") # plot something on screen 4 close.screen(all = TRUE) # exit split-screen mode Or we can use the more powerful function layout in R. Check layout example 2.2.1 Add grid into the graph par(mfrow=c(2,2)) grid(nx=na, ny=5,col="gray") grid(nx=5,ny=na, col="red") grid(nx = 5, ny = 5,lty="dashed",col="blue") grid(nx = 3, ny = 5) # grid only in y-direction # only x-direction # both x and Y # x and y but different numbers 2.3 Math symbols & formula in graphics Sometimes it is necessary to add mathematical formula in the graphics body. See demo(plotmath) # make a hist but use the desity hist(rnorm(100),xlim=c(-3,3),freq=false) # add a normal curve here # expr must be a function or an expression containing x x<-seq(-5,5,.01) curve(dnorm(x),add=true) # add a density expression text(x=2, y=.35, expression(paste("f(x)=",frac(1,sqrt(2*pi)*sigma), exp(-frac(1,2*sigma^2)*((x-mu)^2))))) 4

2.4 Long title problem We have the situation like this: we have a lot to write in the title line which make the graphics really ugly. But we can wrap it like this plot(0,main=paste(strwrap( This is a really long title that I can not type it properly but we can use strwap function to wrap it, width=55))) 2.5 Legend in graphics Adding a legend is a easy thing. You can define color, type, position etc yourself. Look at this example x<-seq(0,1,.01) par(lwd=2) plot(x,dbeta(x,5,1),type="l", ylim=c(0,3), col="green", lty=1,xlab="",ylab="") lines(x,dbeta(x,.5,.5), col="red",lty=2) lines(x,dbeta(x,1,3),col="blue",lty=3) lines(x,dbeta(x,2,2), col="pink",lty=4) lines(x,dbeta(x,2,5), col="black",lty=5) lines(x,dbeta(x,1,1),col="orange",lty=6) legend("top",c("beta(5,1)","beta(.5,.5)","beta(1,3)","beta(2,2)","beta(2,5)","beta(1,1)"), lty=c(1,2,3,4,5,6), col=c("green","red","blue","pink","black","orange"), ncol=2) 2.6 R graphics in Multivariate Analysis The graphics in multivariate analysis is quite easy to use, most of which can be picked up easily. 2.6.1 Stars The build-in data set mtcars is the data extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973-74 models). We can make the Star graph as below. palette(rainbow(12)) # 12 color rainbow stars(mtcars[, 1:7], len = 0.8, key.loc = c(12, 1.5), main = "Motor Trend Cars", draw.segments = TRUE) 2.6.2 Faces To make a faces graph, a package aplpack is needed. library("aplpack") faces(matrix(sample(1:1000,128,),16,8),main="random faces") 2.7 Time series plot # prepare the monthly time series data (8 series), from the Jan, 1961 z <- ts(matrix(rt(200 * 8, df = 3), 200, 8), start = c(1961, 1), frequency = 12) # we can plot some of them for a period (1961.1-1969.12) in multiple frames z1 <- window(z[,1:3], end = c(1969,12)) plot(z1, type = "b") # or we can plot them in a single frame plot(z, plot.type="single", lty=1:3, col=4:2) # the leg opeator plot # we use the monthly mean relative sunspot numbers from 1749 to 1983 in "sunspots" 5

# x_{t-1} aginst x_t plot(lag(sunspots, 1), sunspots, pch = ".") # the acf and pacf # use the data "Luteinizing Hormone in Blood Samples" acf(lh) pacf(lh) 2.8 Grey band for standard deviation Some time we need to show the confidence interval for the model. polygon can handle this easily. Look at this example set.seed(125) x<-1:200 y<-cumsum(rnorm(200)) int1<-y-1.96 int2<-y+1.96 plot(y,xlab="t",ylab=expression(y[t])) # set the seed # simulate an AR(1) model # calculate the 95% level confident interval # plot the data polygon(c(x, rev(x)), c(int1, rev(int2)),col="grey",border=na) # show the band lines(y,type="l",col="blue",lwd=2) # plot the line again 2.9 3D plot (non-interactive) We can use persp to plot non-interactive 3 dimensional graph. Let s take the unnormalized sinc function as example. ( ) ( ) sin x2 + y 2 sinc (z) = sinc x2 + y 2 = x2 + y 2 This is the R code. x <- seq(-10, 10,.3) y <- x rotsinc <- function(x,y) { sinc <- function(x) { y <- sin(x)/x ; y[is.na(y)] <- 1; y } sinc( sqrt(x^2+y^2) ) } z <- outer(x, y, rotsinc) persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightgreen", ltheta = 120, shade = 0.75, ticktype = "detailed", xlab = "X", ylab = "Y", zlab = "Z") title(main = expression(z == Sinc(sqrt(x^2 + y^2)))) 2.10 Project 3D data onto 2D with different colors Recall the sinc function we have tried in Section 2.6. We can plot them in a 2D picture with color by using the image function. x<-seq(-10,10,.3) y<-x z<-outer(x, y, rotsinc) # the rotsinc is defined in section 2.6 image(x, y, z, col=rainbow(50,start=.7,end=.1)) filled.contour(x,y,z) # show the color legend 6

2.11 Contour plot Here is anather way to disply 3D data in a 2D graph N <- 50 x <-y<- seq(-1, 1, length=n) xx <- matrix(x, nrow=n, ncol=n) yy<-t(xx) z <- 1 / (1 + xx^2 + (yy +.5 * sin(5*yy))^2) # here is the contour plot contour(x, y, z, main = "Contour plot") 2.12 3D interactive plot rgl package is a 3D visualization device system (OpenGL). Since it is a little complicate, I just show some demos. You can use rgl.snapshot to export screen shot. There are several other packages/drivers are under development. 7