R Graphics II: Graphics for Exploratory Data Analysis

Similar documents
Viewing Ecological data using R graphics

Graphics in R. Biostatistics 615/815

Tutorial 3: Graphics and Exploratory Data Analysis in R Jason Pienaar and Tom Miller

Lecture 2: Exploratory Data Analysis with R

Getting Started with R and RStudio 1

Each function call carries out a single task associated with drawing the graph.

Getting started with qplot

Data Visualization in R

Data Mining: Exploring Data. Lecture Notes for Chapter 3. Slides by Tan, Steinbach, Kumar adapted by Michael Hahsler

Tutorial 2: Descriptive Statistics and Exploratory Data Analysis

Iris Sample Data Set. Basic Visualization Techniques: Charts, Graphs and Maps. Summary Statistics. Frequency and Mode

R Graphics Cookbook. Chang O'REILLY. Winston. Tokyo. Beijing Cambridge. Farnham Koln Sebastopol

Prof. Nicolai Meinshausen Regression FS R Exercises

Data Exploration Data Visualization

Advanced Statistical Methods in Insurance

Data Mining: Exploring Data. Lecture Notes for Chapter 3. Introduction to Data Mining

COM CO P 5318 Da t Da a t Explora Explor t a ion and Analysis y Chapte Chapt r e 3

Time Series Analysis AMS 316

Big Data and Scripting. Plotting in R

Regression and Programming in R. Anja Bråthen Kristoffersen Biomedical Research Group

5 Correlation and Data Exploration

Graphical Representation of Multivariate Data

Data Mining: Exploring Data. Lecture Notes for Chapter 3. Introduction to Data Mining

Diagrams and Graphs of Statistical Data

The Forgotten JMP Visualizations (Plus Some New Views in JMP 9) Sam Gardner, SAS Institute, Lafayette, IN, USA

Visualizing Data. Contents. 1 Visualizing Data. Anthony Tanbakuchi Department of Mathematics Pima Community College. Introductory Statistics Lectures

Introduction Course in SPSS - Evening 1

MetroBoston DataCommon Training

Exploratory data analysis (Chapter 2) Fall 2011

Table of Useful R commands

Linear Discriminant Analysis

Data exploration with Microsoft Excel: analysing more than one variable

Introduction to R and Exploratory data analysis

Exploratory Data Analysis and Plotting

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

SPSS Manual for Introductory Applied Statistics: A Variable Approach

Générer des graphiques 3D - Cours Université de Sfax

R: A self-learn tutorial

Computational Assignment 4: Discriminant Analysis

Exercise 1.12 (Pg )

Lab 13: Logistic Regression

Graphs. Exploratory data analysis. Graphs. Standard forms. A graph is a suitable way of representing data if:

Using R for Windows and Macintosh

Exploratory Data Analysis

Public Health Activities and Services Tracking (PHAST) Interactive Data Visualization Tool User Manual

A short course in Longitudinal Data Analysis ESRC Research Methods and Short Course Material for Practicals with the joiner package.

Analysis of System Performance IN2072 Chapter M Matlab Tutorial

Acknowledgements. S4 Classes and Methods. Overview. Introduction. S4 has been designed and written by. John Chambers. These slides contain material by

Census Data with Tableau Public

Applying MapCalc Map Analysis Software

TIBCO Spotfire Business Author Essentials Quick Reference Guide. Table of contents:

Exploratory Data Analysis

Visualization of missing values using the R-package VIM

STATGRAPHICS Online. Statistical Analysis and Data Visualization System. Revised 6/21/2012. Copyright 2012 by StatPoint Technologies, Inc.

Visualization Quick Guide

Lecture 1: Review and Exploratory Data Analysis (EDA)

Plotting: Customizing the Graph

Multi-Dimensional Data Visualization. Slides courtesy of Chris North

Psychology 205: Research Methods in Psychology

Mapped Data Visualization and Summary

Integrating SAS with JMP to Build an Interactive Application

TIPS FOR DOING STATISTICS IN EXCEL

R Commander Tutorial

Data Mining and Visualization

MicroStrategy Desktop

Formulas, Functions and Charts

Gestation Period as a function of Lifespan

Example: Credit card default, we may be more interested in predicting the probabilty of a default than classifying individuals as default or not.

CSU, Fresno - Institutional Research, Assessment and Planning - Dmitri Rogulkin

Statistical Models in R

Practical Differential Gene Expression. Introduction

Business Objects 4.1 Quick User Guide

ISyE 2028 Basic Statistical Methods - Fall 2015 Analysis of NFL Team Performance Against the Spread Betting Line

an introduction to VISUALIZING DATA by joel laumans

Journal of Statistical Software

Exploratory Data Analysis

Data Exploration and Preprocessing. Data Mining and Text Mining (UIC Politecnico di Milano)

Data Mining with R. Decision Trees and Random Forests. Hugh Murrell

Continuous Random Variables

Engineering Problem Solving and Excel. EGN 1006 Introduction to Engineering

Data mining as a tool of revealing the hidden connection of the plant

Summary of R software commands used to generate bootstrap and permutation test output and figures in Chapter 16

Data Visualization with R Language

Data analysis process

Time Series Analysis with R - Part I. Walter Zucchini, Oleg Nenadić

Data Preparation Part 1: Exploratory Data Analysis & Data Cleaning, Missing Data

Part 2: Data Visualization How to communicate complex ideas with simple, efficient and accurate data graphics

Basic Statistics and Data Analysis for Health Researchers from Foreign Countries

NICAR Hands-On: Census Data with Tableau Public

Package survpresmooth

STC: Descriptive Statistics in Excel Running Descriptive and Correlational Analysis in Excel 2013

Transcription:

UCLA Department of Statistics Statistical Consulting Center Irina Kukuyeva ikukuyeva@stat.ucla.edu April 26, 2010

Outline 1 Summary Plots 2 Time Series Plots 3 Geographical Plots 4 3D Plots 5 Simulation Plots 6 Useful Links for R

1 Summary Plots Basic Plots Looking at Distributions Identify-ing Observations Exercise I 2 Time Series Plots 3 Geographical Plots 4 3D Plots 5 Simulation Plots 6 Useful Links for R

Basic Plots Basic Plot I 1 # Step 1: Load the Data 2 library ( alr3 ) 3 data ( UN2 ) 4 attach ( UN2 ) 5 # Step 2: Subset appropriately 6 ind <- which ( Purban >50) 7 # Step 3: Plot 8 plot ( logfertility ~ logppgdp, xlab =" logppgdp ", ylab =" logfertility ", main =" loggdp vs logfertility Plot ") 9 points ( logfertility [ ind ]~ logppgdp [ ind ], col =" red ", pch =19) 10 legend (" topright ", pch =c (1,19), col =1:2, c(" Purban <50 ", " Purban >=50 "))

Basic Plots Basic Plot II 8 10 12 14 0.0 0.5 1.0 1.5 2.0 loggdp vs logfertility Plot logppgdp logfertility Purban<50 Purban>=50

Basic Plots Segmented bar charts I Displays two categorical variables at a time: 1 1 survey = read. table (" http :// www. stat. ucla. edu /~ mine / students _ survey _ 2008. txt ", header = TRUE, sep = "\t") 2 attach ( survey ) 3 barplot ( table ( gender, hand ), col = c(" skyblue ", " blue "), main = " Segmented Bar Plot \n of Gender ") 4 legend (" topleft ", c(" females "," males "), col = c(" skyblue ", " blue "), pch = 16, inset = 0.05)

Basic Plots Segmented bar charts II Segmented Bar Plot of Gender females males 0 200 400 600 800 ambidextrous left right female 9 67 806 male 11 45 387 ambidextrous left right 1 These two slides are modified from the SCC Mini-Course Introductory Statistics with R by Mine Çetinkaya

Basic Plots Dot charts I To compare values for variables in each category: 1 # Step 1: Load the data 2 data ( iris ) 3 attach ( iris ) 4 # Step 2: Calculate means for each species : 5 aggregate ( iris [, -5], list ( Species = Species ), mean ) ->a 6 # Step 3: Assign row names 7 row. names (a) <-a[, 1] 8 # Step 4: Plot 9 dotchart (t(a[, -1]), xlim = c (0,10), main =" Plots of Means for Iris Data Set ", xlab =" Mean Value ")

Basic Plots Dot charts II Plots of Means for Iris Data Set setosa Petal.Width Petal.Length Sepal.Width Sepal.Length versicolor Petal.Width Petal.Length Sepal.Width Sepal.Length virginica Petal.Width Petal.Length Sepal.Width Sepal.Length 0 2 4 6 8 10 Mean Value

Looking at Distributions Histograms Adding Summary Statistics to Plots Add the mean and median to a histogram: 1 hist ( ageinmonths, main =" Histogram of Age (Mo)") 2 abline (v= mean ( ageinmonths ), col = " blue ", lwd =3) 3 abline (v= median ( ageinmonths ), col = " red ", lwd =3) 4 legend (" topright ", c(" Mean ", " Median "), pch = 16, col = c(" blue ", " red ")) Frequency 0 100 200 300 400 Histogram of Age (Mo) Mean Median 200 250 300 350 ageinmonths

Looking at Distributions Histograms I Checking Normality One of the methods to test for normality of a variable is to look at the histogram (the sample density is in red, the theoretical normal density in blue): 1 data ( presidents ) 2 hist ( presidents, prob =T, ylim =c(0, 0.04), breaks =20) 3 lines ( density ( presidents, na.rm= TRUE ), col =" red ") 4 mu <- mean ( presidents, na.rm= TRUE ) 5 sigma <-sd( presidents, na.rm= TRUE ) 6 x <-seq (10,100, length =100) 7 y <- dnorm (x,mu, sigma ) 8 lines (x,y,lwd =2, col =" blue ")

Looking at Distributions Histograms II Checking Normality Histogram of Presidents' Approval Ratings Density 0.00 0.01 0.02 0.03 0.04 20 30 40 50 60 70 80 90 presidents

Looking at Distributions Box and Whisker Plot I Another method of looking at the distribution of the data is via boxplot: 1 data ( quakes ) 2 # Subset the magnitude : 3 ind <- ifelse ( quakes [, 4] <4.5, 0, 1) 4 ind <-as. factor ( ind ) 5 library ( lattice ) 6 bwplot ( quakes [, 4]~ind, xlab =c("mag <4.5 ", "Mag >=4.5 "), ylab =" Magnitude ")

Looking at Distributions Box and Whisker Plot II Fiji EQ since 1964 6.5 6.0 5.5 Magnitude 5.0 4.5 4.0 Mag<4.5 0 1 Mag>=4.5

Looking at Distributions Beanplot I An alternative to the boxplot is the beanplot(): 1 library ( beanplot ) 2 par ( mfrow =c (1,2) ) 3 data ( airquality ) 4 boxplot ( airquality [, 2], main =" Boxplot ", xlab = " Solar ") 5 beanplot ( airquality [, 2], main =" Beanplot ", xlab =" Solar ")

Looking at Distributions Beanplot II Boxplot Beanplot 0 50 100 150 200 250 300 0 100 200 300 400 Solar Solar

Looking at Distributions Scatterplots I A method of looking at the distribution and correlation of the data is via scatterplot.matrix(): 1 data ( quakes ) 2 library ( car ) 3 scatterplot. matrix ( quakes [, 1:4])

Looking at Distributions Scatterplots II lat 165 170 175 180 185 4.0 4.5 5.0 5.5 6.0 35 25 15 165 170 175 180 185 long depth 100 300 500 700 35 25 15 4.0 4.5 5.0 5.5 6.0 100 300 500 700 mag

Looking at Distributions Checking Equality of Distributions I Approach 1 A method of checking equality of distributions is via qq(): 1 library ( lattice ) 2 survey = read. table (" http :// www. stat. ucla. edu /~ mine / students _ survey _ 2008. txt ", header = TRUE, sep = "\t") 3 attach ( survey ) 4 qq( gender ~ ageinmonths )

Looking at Distributions Checking Equality of Distributions II Approach 1

Looking at Distributions Checking Equality of Distributions I Approach 2 Another method for checking equality of distributions is via beanplot(): 1 survey = read. table (" http :// www. stat. ucla. edu /~ mine / students _ survey _ 2008. txt ", header = TRUE, sep = "\t") 2 attach ( survey ) 3 beanplot ( ageinmonths ~ gender, data = survey, col = list ( grey (0.5),c( grey (0.8)," white ")), border = NA, overallline = " median ", ll =0.01, side =" both ") 4 legend (" bottomleft ",fill =c( grey (0.5),grey (0.8) ), legend =c(" Female "," Male "))

Looking at Distributions Checking Equality of Distributions II Approach 2

Identify-ing Observations Identify-ing Observations I Preliminaries 1 # Generate data and fit a regression curve : 2 set. seed (3012008) 3 x= rnorm (100) ; y=-x+i(x ^2) + rnorm (100) 4 fit <-lm(y~x+i(x ^2) ); fit Intercept x x 2 0.1307-0.9701 0.9549 1 # Plot the resulting regression curve : 2 plot (y~x, pch =19) 3 curve ( expr = fit [[1]][1]+ fit [[1]][2] *x+ fit [[1]][3] *I(x ^2), from = range (x)[1], to= range (x)[2], add =TRUE, col =" blue ", lwd =2)

Identify-ing Observations Identify-ing Observations II Preliminaries 2 1 0 1 2 2 0 2 4 6 x y

Identify-ing Observations Identify-ing Observations I Left-click on the observations in the graphics window to see the row number. Right-click on the observation to exit the function. 1 # Plot the data and fit the regression curve : 2 plot (y~x, pch =19) 3 curve ( expr = fit [[1]][1]+ fit [[1]][2] *x+ fit [[1]][3] *I(x ^2), from = range (x)[1], to= range (x)[2], add =TRUE, col =" blue ", lwd =2) 4 # Identify the " outlying " observations : 5 index <- identify (y~x); index

Identify-ing Observations Identify-ing Observations II

Exercise I Exercise I Compare the distributions of the sepal widths for the three species of irises (using the iris data set).

1 Summary Plots 2 Time Series Plots Multivariate Plots Exercise II 3 Geographical Plots 4 3D Plots 5 Simulation Plots 6 Useful Links for R

Multivariate Plots Multivariate Time Series Plots I Approach 1 To plot more than variables one at a time, use plot(): 1 # Convert data to a time series via ts () or zoo (): 2 data ( airquality ) 3 a <- airquality [, 1:3] 4 time <-ts (1: nrow (a), start =c (1973, 5), frequency =365) 5 # If your data is stored as a data frame, 6 # coerce it to be a matrix via as. matrix () 7 class (a) 8 a. mat <-as. matrix (a) 9 10

Multivariate Plots Multivariate Time Series Plots II Approach 1 11 # Make a time series of the two ( or more variables ) 12 library ( zoo ) 13 name. zoo <-zoo ( cbind (a. mat [, 1], a. mat [, 2]) ) 14 colnames ( name. zoo ) <-c(" Ozone ", " Solar ") 15 # ### Plot the variables 16 plot ( name. zoo )

Multivariate Plots Multivariate Time Series Plots III Approach 1 Plots of Ozone and Solar Solar Ozone 0 100 200 300 0 50 100 150 0 50 100 150 Time

Multivariate Plots Multivariate Time Series Plots Approach 2 To plot more than variables one at a time, use mvtsplot(): 2 1 # Load the R Code for the function 2 source (" http :// www. biostat. jhsph. edu /~ rpeng /RR / mvtsplot / mvtsplot.r") 3 # After processing data as in Approach 1 4 # Plot the variables 5 mvtsplot ( name. zoo ) 6 # Purple =low, grey = medium, green =high, white = missing values 2 For documentation, go to: www.jstatsoft.org/v25/c01/paper

Multivariate Plots Multivariate Time Series Plots Approach 2 Solar Ozone 300 0 50 100 150 200 250 300 250 Level 200 150 100 50 0 0 20 40 60 80 100 120 140

Multivariate Plots Multivariate Time Series Plots I Approach 3 To plot more than variables one at a time, use xyplot(): 1 # After processing data as in Approach 1 2 # load both libraries : 3 library ( lattice ) 4 library ( zoo ) 5 data ( EuStockMarkets ) 6 z <- EuStockMarkets 7 xyplot (z, screen = c (1,1,2,2), col = 1:4, strip = FALSE, key = list ( title =" EuStockMarkets ", columns =2, points = FALSE, lines =TRUE, col =1:4, text = list ( colnames (z) )))

Multivariate Plots Multivariate Time Series Plots II Approach 3 EuStockMarkets DAX SMI CAC FTSE 2000 4000 6000 8000 2000 3000 4000 5000 6000 1992 1994 1996 1998 Index

Exercise II Exercise II Analyze the EuStockMarkets data using the function mtvsplot(). What stands out and why?

1 Summary Plots 2 Time Series Plots 3 Geographical Plots Maps Projection Maps Exercise III 4 3D Plots 5 Simulation Plots 6 Useful Links for R

Maps Geographic Maps Map of Fiji Earthquakes Since 1964 To overlay a map to a plot containing latitude and longitude, load the package maps: 1 data ( quakes ) 2 library ( maps ) 3 plot ( quakes [, 2], quakes [, 1], xlim = c (100, 190), ylim = c( -40, 0)) 4 map (" world ", add =T) 100 120 140 160 180 40 30 20 10 0 quakes[, 2] quakes[, 1]

Maps Geographic Maps I Map of Fiji Earthquakes Since 1964 To include information on magnitude of earthquake, use cex argument of the plot() function: 1 # Step 1: Recode magnitude to have 3 categories only 2 ind <- which (mag <5) 3 ind2 <- which ( mag <6 & mag >=5) 4 ind3 <- which (mag >=6) 5 color <-rep (NA, length ( mag )) 6 color [ ind ] <-1; color [ ind2 ] <-2; color [ ind3 ] <-3 7 8 9 10

Maps Geographic Maps II Map of Fiji Earthquakes Since 1964 11 # Step 2: Plot 12 plot ( quakes [, 2], quakes [, 1], cex = color, pch =19, col = color, xlim =c (100, 190), ylim = c ( -40,0), xlab =" Longitude ", ylab =" Latitude ") 13 legend (" topright ", pch =19, col =1:3, c("mag <5", "5<= Mag <6", "Mag >6"), pt.cex =1:3) 14 map (" world ", add =T)

Maps Geographic Maps III Map of Fiji Earthquakes Since 1964 100 120 140 160 180 40 30 20 10 0 Longitude Latitude Mag<5 5<=Mag<6 Mag>6

Projection Maps Projection Maps I Map of Fiji Earthquakes Since 1964 For a different perspective of a map, use mapproject(): 1 library ( mapproj ) 2 library ( maps ) 3 m <- map ( world,plot = FALSE ) 4 # Projection is Azimuthal with equal - area 5 map ( world,proj = azequalarea, orient =c( longitude =0, latitude =180, rotation =0) ) 6 map. grid (m,col =2) 7 points ( mapproject ( list (y= quakes [ which ( quakes [, 4] >=6), 1],x= quakes [ which ( quakes [, 4] >=6), 2]) ),col =" blue ",pch ="x",cex =2)

Projection Maps Projection Maps II Map of Fiji Earthquakes Since 1964 0 50 100 50 84 60 100 150 180 150 40 20 0 x 20 60 40 85

Projection Maps Bonus Feature of the maps package: To determine in which part of the world the observations are (based on latitude and longitude), use map.where(): 1 in. what. country <-map. where ( database =" world ", quakes [, 2], quakes [, 1]) To determine which observations are in the ocean: 1 # Number of points in ocean after filtering : 2 ind <-sum (is.na(in. what. country )); ind 3 # Number of observations : 1000 4 # Number in Ocean : 993

Exercise III Exercise III For the ozone data set 3, determine the region of the world that the observations correspond to and overlay the appropriate map. 3 http://www.ats.ucla.edu/stat/r/faq/ozone.csv

1 Summary Plots 2 Time Series Plots 3 Geographical Plots 4 3D Plots lattice library ggplot2 library rgl library Saving Plots as a PDF Exercise IV 5 Simulation Plots 6 Useful Links for R

lattice library 3D Images with Package lattice Method 1: Using wireframe(): 1 library ( lattice ) 2 wireframe ( volcano, col. regions = terrain. colors ( 100), asp = 1, color. key =TRUE, drape =TRUE, scales = list ( arrows = FALSE )) 180 160 volcano 140 120 100 60 50 40 30 20 column 10 20 40 row 60 80 200 180 160 140 120 100

lattice library 3D Images with Package lattice Method 2: Same image with the levelplot() function: 60 200 1 library ( lattice ) 2 levelplot ( volcano, asp =1, col. regions = terrain. colors ) column 50 40 30 20 180 160 140 120 10 100 20 40 60 80 row

y lattice library 3D Images with Package lattice Method 3: Same image with the image() function: 1 x <-10*(1: nrow ( volcano ) ) 2 y <-10*(1: ncol ( volcano ) ) 3 image (x, y, volcano, col = terrain. colors ( 100), axes = TRUE ) 4 contour (x, y, volcano, add = TRUE, col = 1) 100 200 300 400 500 600 110 110 190 120 180 170 160 180 160 110 150 170 140 110 130 100 100 100 200 400 600 800 x

ggplot2 library 3D Images with Package ggplot2 I Another way to create 3D images is with the package ggplot2: 1 # Step 1: Load the data 2 data ( quakes ) 3 attach ( quakes ) 4 # Step 2: Create a categorical variable for earthquake magnitude 5 ind <- which (mag <5) 6 ind2 <- which ( mag <6 & mag >=5) 7 ind3 <- which (mag >=6) 8 color <-rep (NA, length ( mag )) 9 color [ ind ] <-1; color [ ind2 ] <-2; color [ ind3 ] <-3 10 color <-as. factor ( color ) 11 12

ggplot2 library 3D Images with Package ggplot2 II 13 # Step 3: Plot 14 library ( ggplot2 ) 15 ggplot ( data = quakes, aes (long, lat ))+ 16 geom _ point ( aes (long, lat ))+ 17 borders (" world ")+ 18 coord _ cartesian ( xlim =c (100,190), ylim =c ( -40,0) )

ggplot2 library 3D Images with Package ggplot2 III long lat 0 100 150

ggplot2 library 3D Images with Package ggplot2 Adding Another Dimension I To include information on magnitude of earthquake, use geom point(): 1 ggplot ( data = quakes, aes (long, lat ))+ 2 borders (" world ")+ 3 coord _ cartesian ( xlim =c (100,190), ylim =c ( -40,0) )+ 4 geom _ point ( data = quakes, colour =as. numeric ( color ), size =2*as. numeric ( color ))

ggplot2 library 3D Images with Package ggplot2 II Adding Another Dimension long lat 0 100 150

rgl library 3D Images with Package rgl A way to create 3D interactive images is with the package rgl: 1 library ( rgl ) 2 data ( quakes ) 3 plot3d (x= quakes [, 2], y= quakes [, 1], z= quakes [, 3], xlab = " Longitude ", ylab = " Latitude ", zlab =" Depth ")

Saving Plots as a PDF Saving Plots as a PDF For Static Plots Note: The files will be saved in the folder specified with setwd(). To save a plot in R as a PDF, you can use pdf(): 1 # To save the image to the Desktop : 2 setwd ("~/ Desktop ") 3 pdf (" Volcano. pdf ", width =6, height =6, onefile = FALSE ) 4 wireframe ( volcano, col. regions = terrain. colors (100), asp = 1, color. key =TRUE, drape = TRUE, scales = list ( arrows = FALSE )) 5 dev. off ()

Saving Plots as a PDF Saving Plots as a PDF For Dynamic Plots Note: The files will be saved in the folder specified with setwd(). To save a plot in R as a PDF, you can use pdf(): 1 # To save the image to the Desktop : 2 setwd ("~/ Desktop ") 3 # Step 1: Produce the 3 D plot 4 plot3d (x= quakes [, 2], y= quakes [, 1], z= quakes [, 3], xlab =" Longitude ", ylab =" Latitude ", zlab =" Depth ") 5 # Step 2: Can rotate before taking a snapshot : 6 rgl. snapshot (" quakes. png ")

Exercise IV Exercise IV Use the rgl library to create a 3D plot of the ozone data set 4. 4 http://www.ats.ucla.edu/stat/r/faq/ozone.csv

1 Summary Plots 2 Time Series Plots 3 Geographical Plots 4 3D Plots 5 Simulation Plots Preliminaries Performing the Simulation and Visualizing the Results Exercise V 6 Useful Links for R

Preliminaries Simulations Preliminaries: The function outer() 1 x =5:6; y =1:3 2 outer (x,y) [,1] [,2] [,3] [1,] 5 10 15 [2,] 6 12 18 1 fcn <- function (x,y){z=x+y} 2 outer (x,y,fcn ) [,1] [,2] [,3] [1,] 6 7 8 [2,] 7 8 9

Performing the Simulation and Visualizing the Results Simulations I Visualize with persp() Suppose we want to know what the function y sin(x) looks like: 1 # Sample from the random uniform : 2 x <- sort ( runif (100, min =0, max =10) ) 3 y <- x+ runif (1, min =0, max =20) 4 f <- function (x, y){r <-y* sin (x)} 5 z <- outer (x,y,f) 6 persp (x, y, z, col = " lightblue ", shade = 0.1, ticktype = " detailed ", expand =0. 7)

Performing the Simulation and Visualizing the Results Simulations II Visualize with persp()

Performing the Simulation and Visualizing the Results Simulations I Visualize with image() 1 # Format the data appropriate for the image () fcn : 2 x. seq <-seq ( from = range (x)[1], to= range (x)[2], length =100) 3 y. seq <-seq ( from = range (y)[1], to= range (y)[2], length =100) 4 n1 <- length (x. seq ) 5 n2 <- length (y. seq ) 6 mat1 <- matrix (z, nrow =n1, ncol =n2, byrow = FALSE ) 7 image (x.seq, y.seq, mat1 ) 8 # to overlay a contour : 9 contour (x.seq, y.seq, mat1, add = TRUE )

2 0 0 0 4 Performing the Simulation and Visualizing the Results Simulations II Visualize with image() y.seq 4 6 8 10 12 4 12 10 6 8 2 2 12 10 8 6 4 2 2 12 10 8 6 4 2 2 2 4 6 8 x.seq

Performing the Simulation and Visualizing the Results Simulations I Visualize with plot3d() Suppose we want to know what the same function y sin(x) looks like with a dynamic plot: 1 # Sample from the random uniform : 2 x1 <- runif (10000, min =0, max =10) 3 y1 <- x1+ runif (1000, min =0, max =20) 4 z1 <- y1*sin (x1) 5 plot3d (x1, y1, z1)

Performing the Simulation and Visualizing the Results Simulations II Visualize with plot3d()

Exercise V Exercise V Visualize the following function: z = sin( 3 2 x) y (1)

1 Summary Plots 2 Time Series Plots 3 Geographical Plots 4 3D Plots 5 Simulation Plots 6 Useful Links for R

Online Resources for R Download R: Search Engine for R: http:// cran.stat.ucla.edu/ http:// rseek.org R Reference Card: http:// cran.r-project.org/ doc/ contrib/ Short-refcard.pdf R Graphics Gallery: http:// research.stowers-institute.org/ efg/ R/ R Graph Gallery: http:// addictedtor.free.fr/ graphiques/ UCLA Statistics Information Portal: http:// info.stat.ucla.edu/ grad/ UCLA Statistical Consulting Center: http:// scc.stat.ucla.edu

Upcoming Mini-Course This Wednesday 5:00PM: R Graphics III: Customizing Graphics

We Want to Hear From You! Please complete our online survey. Your feedback will help us improve our mini-course series. We greatly appreciate your time. http:// scc.stat.ucla.edu/ survey

Thank you. Any questions?