IST 3108 Data Analysis and Graphics Using R Week 9

Similar documents
Statistical Programming with R

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

Az R adatelemzési nyelv

CIND123 Module 6.2 Screen Capture

Using Built-in Plotting Functions

R Workshop Module 3: Plotting Data Katherine Thompson Department of Statistics, University of Kentucky

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

Advanced Graphics with R

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

Introduction to R: Day 2 September 20, 2017

Graphics - Part III: Basic Graphics Continued

INTRODUCTION TO R. Basic Graphics

An Introduction to R 2.2 Statistical graphics

AA BB CC DD EE. Introduction to Graphics in R

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

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

Statistics 251: Statistical Methods

DSCI 325: Handout 18 Introduction to Graphics in R

Stats with R and RStudio Practical: basic stats for peak calling Jacques van Helden, Hugo varet and Julie Aubert

Spring 2017 CS130 - Intro to R 1 R VISUALIZING DATA. Spring 2017 CS130 - Intro to R 2

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

Chapter 5 An Introduction to Basic Plotting Tools

Advanced Econometric Methods EMET3011/8014

GS Analysis of Microarray Data

Common Sta 101 Commands for R. 1 One quantitative variable. 2 One categorical variable. 3 Two categorical variables. Summary statistics

Module 10. Data Visualization. Andrew Jaffe Instructor

Install RStudio from - use the standard installation.

Statistical Software Camp: Introduction to R

STATISTICS: AN INTRODUCTION USING R. By M.J. Crawley. Exercises 1. PLOTS: GRAPHICAL METHODS OF DATA EXPLORATION

Introduction to R. Biostatistics 615/815 Lecture 23

Common R commands used in Data Analysis and Statistical Inference

R Graph Essentials. Use R's powerful graphing capabilities to design and create professional-level graphics. David Alexander Lillis

The nor1mix Package. August 3, 2006

An Introduction to R Graphics with examples

Introduction Basics Simple Statistics Graphics. Using R for Data Analysis and Graphics. 4. Graphics

Visualizing in R advanced plotting

Statistical Programming Camp: An Introduction to R

Package visualizationtools

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

Solution to Tumor growth in mice

Introduction to RStudio

Data Visualization. Andrew Jaffe Instructor

Stat 290: Lab 2. Introduction to R/S-Plus

Introduction to R. Richard Wang, PhD CBI fellow Nelson & Miceli labs

The nor1mix Package. June 12, 2007

R is a programming language of a higher-level Constantly increasing amount of packages (new research) Free of charge Website:

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

GS Analysis of Microarray Data

Using R. Liang Peng Georgia Institute of Technology January 2005

Chapter 2: Descriptive Statistics: Tabular and Graphical Methods

Introduction to R for Epidemiologists

An introduction to WS 2015/2016

Plotting Complex Figures Using R. Simon Andrews v

Univariate Data - 2. Numeric Summaries

Introduction to R for Beginners, Level II. Jeon Lee Bio-Informatics Core Facility (BICF), UTSW

Namir s R 102 Plotting Tutorial by Namir Shammas Dedication

Package kofnga. November 24, 2015

Package gains. September 12, 2017

LAB #1: DESCRIPTIVE STATISTICS WITH R

Univariate Data - 2. Numeric Summaries

Computer Statistics with R

Lab 4: Distributions of random variables

Plotting: An Iterative Process

Tutorial 3: Probability & Distributions Johannes Karreth RPOS 517, Day 3

Practice for Learning R and Learning Latex

A (very) brief introduction to R

The Basics of Plotting in R

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Outline day 4 May 30th

R programming. 19 February, University of Trento - FBK 1 / 50

Package munfold. R topics documented: February 8, Type Package. Title Metric Unfolding. Version Date Author Martin Elff

Package simed. November 27, 2017

Package OmicCircos. R topics documented: November 17, Version Date

R Bootcamp Part I (B)

This document is designed to get you started with using R

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

Graphics #1. R Graphics Fundamentals & Scatter Plots

plot(seq(0,10,1), seq(0,10,1), main = "the Title", xlim=c(1,20), ylim=c(1,20), col="darkblue");

Package npregfast. November 30, 2017

Package sciplot. February 15, 2013

Middle Years Data Analysis Display Methods

Applied Calculus. Lab 1: An Introduction to R

Welcome to Workshop: Making Graphs

Package splicegear. R topics documented: December 4, Title splicegear Version Author Laurent Gautier

Package pwrrasch. R topics documented: September 28, Type Package

Introduction to R 21/11/2016

Stat 302 Statistical Software and Its Applications Density Estimation

Mixed models in R using the lme4 package Part 2: Lattice graphics

UP School of Statistics Student Council Education and Research

Package glogis. R topics documented: March 16, Version Date

R Graphs Cookbook. Hrishi V. Mittal BIRMINGHAM - MUMBAI.

Estimating R 0 : Solutions

Data Visualization in R

Lab1 Part 1: Exploratory Data Analysis & R Basics Big Data Analytics Feb5, 2018

Intro to R for Epidemiologists

Package ClamR. R topics documented: July 29, Type Package

Package OmicCircos. R topics documented: November 1, Version Date Title High-quality circular visualization of omic data

8. MINITAB COMMANDS WEEK-BY-WEEK

Graph tool instructions and R code

Package Daim. February 15, 2013

Transcription:

IST 3108 Data Analysis and Graphics Using R Week 9 Engin YILDIZTEPE, Ph.D 2017-Spring Introduction to Graphics >y <- rnorm(20) >plot (y) In R, pictures are presented in the active graphical device or window. When a graphical function is executed, R will open such a window. You can print directly from the graphics window, or choose to copy the graph to the clipboard.you can also resize the graph. A graph can also be saved in many other formats, pdf, bitmap, metafile, jpeg, postscript, png or TIFF. (File Save as (when the graphics window is active)) 2 1

Introduction to Graphics When you produce a new plot, the old one is lost. In MS Windows, you can save a history of your graphs by activating the Recording feature under the History menu (seen when the graphics window is active). You can access old graphs by using the Page Up and Page Down keys. You can simply open a new active graphics window by using the function windows() in Windows, X11() in Unix and quartz() on a Mac OS X. 3 The traditional graphics model - plot If a single vector object is given the values are plotted on the y-axis against the row numbers or index. >y <- rnorm(20) >plot (y) The type= argument controls the type of plot produced, as follows: plot(y, type="p") plot(y, type="l") plot(y, type="b") plot(y, type="h") plot(y, type= s") plot(y, type= n") 4 2

The traditional graphics model - plot The plot types are: "p" points (i.e. a scatterplot) "l" lines (i.e. a line plot) "b" both (points and lines) "o" points and lines overplotted "h" high-density needles "s" step function, horizontal step first "n" nothing (i.e. no plot contents) 5 The traditional graphics model plot types 6 3

The traditional graphics model plot types 7 Standard arguments > y <- rnorm(20) > plot(y, type="l", lwd=3) > plot(y, type="b", lwd=5) > plot(y, type="l", col="grey") > plot(y, type="l", col="red", lwd=5) > plot(y, type="l", lty="dashed") > plot(y, type="l", ylim=c(-4, 4)) > plot(y, type="l", ylim=c(-3, 3)) > plot(y, type="l", ylim=c(-3, 3), xlim=c(0,50)) 8 4

The traditional graphics model plot line types Line types can either be specified as an integer; 0 :blank, 1 :solid (default), 2 :dashed, 3 :dotted, 4 :dotdash, 5 :longdash, 6 :twodash or as one of the character strings "blank", "solid", "dashed", "dotted", "dotdash", "longdash 9 Plots of one or two variables If x and y are vectors, plot(x, y) produces a scatterplot of y against x Example: Plot of sin(x) y -1.0-0.5 0.0 0.5 1.0 2 4 6 8 10 x 10 5

Plots of one or two variables Example; > x<-seq(1,10,length=100) > y<-sin(x) > plot(x, y, main = "Plot of sin(x)", type="l, col="blue", lwd=3) 11 Plots of one or two variables Using pressure dataset?pressure plot(pressure$temperature, pressure$pressure) plot(pressure ~ temperature, data=pressure) plot(pressure ~ temperature, data=pressure, type='l') 12 6

Exercise >plot (pressure$temperature, pressure$pressure, xlab = "Temperature (deg C)", ylab = "Pressure (mm of Hg)", main = "pressure data: Vapor Pressure of Mercury") > plot(mtcars$wt, mtcars$mpg, ylab = "miles per gallon", xlab = "Weight (lb/1000)", main="mtcars data: fuel consumption and weight", type="p", col="red", lwd=3) 13 Permanent changes: The par() function The par() function can be used to modify traditional graphics state settings by specifying a value via an argument with the appropriate setting name. The following code sets new values for the col and lty settings. >par(col="red", lty="dashed") >plot(y, type="l") # line is red and dashed y -1.0-0.5 0.0 0.5 1.0 0 20 40 60 80 100 Index 14 7

Permanent changes: The par() function >par(mfrow = c(2, 2)) >plot(pressure ~ temperature, data=pressure) >plot(pressure ~ temperature, data=pressure, type="l") >plot(pressure ~ temperature, data=pressure, type="l", lty=1) >plot(pressure ~ temperature, data=pressure, type="b", lty=1) pressure 0 600 0 600 0 50 100 200 300 temperature 0 50 100 200 300 temperature pressure 0 600 pressure 0 600 pressure 0 50 100 200 300 temperature 0 50 100 200 300 temperature 15 Permanent changes: The par() function >par(mfrow=c(1,1), bg = "cornsilk") >plot(pressure ~ temperature, data=pressure) pressure 0 200 400 600 800 0 50 100 150 200 250 300 350 temperature 16 8

The par () function - Example Std. Normal t df=1 t df=5 Density 0.0 0.2 0.4-4 -2 0 2 4 dt(x, 1) 0.05 0.20-4 -2 0 2 4 dt(x, 5) 0.0 0.2-4 -2 0 2 4 x value x x t df=10 t df=30 dt(x, 10) 0.0 0.2 0.4-4 -2 0 2 4 dt(x, 30) 0.0 0.2 0.4-4 -2 0 2 4 x x 17 The par () function - Example x <- seq(-4, 4, length=100) par(mfrow = c(2, 3), lty=1, lwd=3) plot(x, dnorm(x), type="l", xlab="x value",ylab="density", main="std. Normal") plot(x, dt(x,1), type="l", main="t df=1", col="red") plot(x, dt(x,5), type="l", main="t df=5", col="blue") plot(x, dt(x,10), type="l", main="t df=10", col="darkgreen") plot(x, dt(x,30), type="l", main="t df=30", col="gold") 18 9

The lines() function - Example Comparison of t Distributions Density 0.0 0.1 0.2 0.3 0.4 Distributions df=1 df=5 df=10 df=30 norma -4-2 0 2 4 x value 19 The lines() function - Example x <- seq(-4, 4, length=100) px <- dnorm(x) degf <- c(1, 5, 10, 30) colors <- c("red", "blue", "darkgreen", "gold,"black") labels <- c("df=1,"df=5,"df=10", "df=30,"normal") par(mfrow=c(1,1),lwd=2, lty=1, col="black") plot(x, px, type="l", lty=2, lwd=4,xlab= x value", ylab="density", main="comparison of t Distributions") 20 10

The lines() function - Example lines(x, dt(x,degf[1]), col=colors[1]) lines(x, dt(x,degf[2]), col=colors[2]) lines(x, dt(x,degf[3]), col=colors[3]) lines(x, dt(x,degf[4]), col=colors[4]) legend("topright", legend=labels,title="distributions", lwd=2, lty=c(1, 1, 1, 1, 2), col=colors) 21 boxplot Produce box-and-whisker plot(s) of the given (grouped) values. >boxplot(mtcars$mpg) >boxplot(mtcars$mpg,notch=t) >boxplot(mtcars$mpg~mtcars$cyl) >boxplot(mtcars$mpg~mtcars$cyl,main="milage by cyl", col=c("red", "blue", "gold")) 22 11

boxplot >boxplot(mtcars$mpg~mtcars$cyl,main="milage by cyl", xlab="milage", horizontal=true,col=c("red", "blue", "gold")) >boxplot(mtcars$mpg~mtcars$cyl,main="milage by Cyl",xlab="Milage", horizontal=true,col=terrain.colors(3)) >boxplot(mtcars$mpg~mtcars$cyl,main="milage by Cyl",xlab="Milage", horizontal=true,col=rainbow(3)) 23 Histogram The generic function hist computes a histogram of the given data values. y<-rnorm(1000) hist(y) Histogram of y Frequency 0 50 100-3 -2-1 0 1 2 3 y 24 12

> hist(y,breaks="scott") Histogram > hist(y,breaks="fd") > hist(y,breaks="sturges") 25 Histogram > hist(y,breaks=seq(-4,4,0.1)) Histogram of y Frequency 0 10 30-4 -2 0 2 4 y 26 13

Histogram > hist(y,breaks=50,col="tomato") Histogram of y Frequency 0 10 30-3 -2-1 0 1 2 3 y 27 Histogram > hist(y,breaks=20,col=colors()) Histogram of y Frequency 0 50 100-3 -2-1 0 1 2 3 y 28 14

Histogram y için histogram Frekanslar 0 50 100 150 200-4 -2 0 2 4 y değerleri 29 Histogram 30 15

pie Draw a pie chart. >x<-c(14, 34, 20, 10, 22,40) >names(x)<-c( A, B, C, D, E, F ) >pie(x,main = "Energy Consumption", col=rainbow(6)) Energy Consumption B D C A E F 31 pie >pie(x,main = "Energy Consumption", col=rainbow(6),init.angle=110,clockwise=t) Energy Consumption A F B E D C 32 16

barplot Creates a bar plot with vertical or horizontal bars >barplot(x, ylim = c(0, 40),col = rainbow(6), main = "Energy Consumption, ylab = "Percent in Category") Energy Consumption Percent in Category 0 10 20 30 40 A B C D E F 33 barplot >barplot(x, xlim = c(0, 40),col = rainbow(6), main = "Energy Consumption", ylab = "Percent in Category", horiz=t) Energy Consumption Percent in Category A B C D E F 0 10 20 30 40 34 17

barplot >barplot(x, ylim = c(0, 40), xlim=c(0,8), col=rainbow(6), main="energy Consumption", ylab="percent in Category", axis.lty=1, legend=names(x)) Energy Consumption Percent in Category 0 10 20 30 40 A B C D E F A B C D E F 35 example How can we draw this pie chart? 36 18

curve function Draws a curve corresponding to a function over the interval [from, to]. curve(expr, from = NULL, to = NULL, ) >curve(dnorm(x,m=10,sd=2), from=0, to=20, main= "Normal distribution") Normal distribution dnorm(x, m = 10, sd = 2) 0.00 0.10 0.20 0 5 10 15 20 x 37 curve function >curve(dgamma(x, scale=1.5, shape=2),from=0, to=15, main="gamma distribution") 38 19

curve function >curve(dweibull(x, scale=2.5, shape=1.5),from=0, to=15, main="weibull distribution") 39 qqnorm - qqline This plot is used to determine if your data is close to being normally distributed. You cannot be sure that the data is normally distributed, but you can rule out if it is not normally distributed. y<-rnorm(1000,mean=10,sd=2) qqnorm(y) Normal Q-Q Plot qqline(y,col=2) Sample Quantiles 4 6 8 12 16-3 -2-1 0 1 2 3 Theoretical Quantiles 40 20

qqnorm - qqline x.wei<-rweibull(n=200,shape=2.2,scale=1.2) qqnorm(x.wei) qqline(x.wei,col=2) Normal Q-Q Plot Sample Quantiles 0.0 1.0 2.0-3 -2-1 0 1 2 3 Theoretical Quantiles 41 21