Advanced Scientific Computing with R 4. Plots Michael Hahsler - - PowerPoint PPT Presentation

advanced scientific computing with r
SMART_READER_LITE
LIVE PREVIEW

Advanced Scientific Computing with R 4. Plots Michael Hahsler - - PowerPoint PPT Presentation

Advanced Scientific Computing with R 4. Plots Michael Hahsler Southern Methodist University February 16, 2014 These slides are largely based on An Introduction to R http://CRAN.R-Project.org/ Michael Hahsler (SMU) Adv. Sci. Comp. with


slide-1
SLIDE 1

Advanced Scientific Computing with R

  • 4. Plots

Michael Hahsler

Southern Methodist University

February 16, 2014

These slides are largely based on “An Introduction to R” http://CRAN.R-Project.org/ Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 1 / 23

slide-2
SLIDE 2

Table of Contents

1

Simple Plots

2

High-level Graphics Functions

3

Low-level Graphics Functions

4

Exercises

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 2 / 23

slide-3
SLIDE 3

Introduction

Ploting is an integral part of R. R plots on devices (e.g., X11(), quarz(), windows(), pdf(), png()) Plotting commands are divided into three basic groups:

◮ High-level plotting functions create a new plot on the graphics

device, possibly with axes, labels, titles and so on.

◮ Low-level plotting functions add more information to an existing

plot, such as extra points, lines and labels.

◮ Interactive graphics functions allow you interactively add

information to, or extract information from, an existing plot, using a pointing device such as a mouse. We will only discuss ‘base’ graphics. An advanced graphics sub-system called ‘grid’ also exists.

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 3 / 23

slide-4
SLIDE 4

Table of Contents

1

Simple Plots

2

High-level Graphics Functions

3

Low-level Graphics Functions

4

Exercises

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 4 / 23

slide-5
SLIDE 5

plot

R> plot(1:10)

  • 2

4 6 8 10 2 4 6 8 10 Index 1:10

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 5 / 23

slide-6
SLIDE 6

plot

R> plot(1:10, type="l", col="red", lwd=3) R> abline(v=5, lty=2)

2 4 6 8 10 2 4 6 8 10 Index 1:10

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 6 / 23

slide-7
SLIDE 7

Getting help for plot

>? plot Shows that plot is a so called generic function. Generic functions have implementations dor different data types which get ” dispatched”at call-time. >? plot.default This is the default function for plot. >? par Graphical parameters which typically can be passed on as ... to plot.

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 7 / 23

slide-8
SLIDE 8

Scatterplot

R> plot(x=rnorm(500), y=rnorm(500), xlab="x", ylab="y", + main="Bi-variate Norm. Distr.")

  • −3

−2 −1 1 2 −3 −2 −1 1 2 3

Bi−variate Norm. Distr.

x y

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 8 / 23

slide-9
SLIDE 9

Scatterplot matrix (pairs)

R> data(iris) R> head(iris, n=1) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa R> plot(iris[,-5], col= iris[,5])

Sepal.Length

2.0 3.0 4.0

  • 0.5

1.5 2.5 4.5 5.5 6.5 7.5

  • 2.0

3.0 4.0

  • ●●
  • Sepal.Width
  • Petal.Length

1 2 3 4 5 6 7

  • 4.5

5.5 6.5 7.5 0.5 1.5 2.5

  • ●●●
  • 1

2 3 4 5 6 7

  • Petal.Width

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 9 / 23

slide-10
SLIDE 10

hist - Histogram

R> hist(iris$Sepal.Length, breaks=20)

Histogram of iris$Sepal.Length

iris$Sepal.Length Frequency 5 6 7 8 5 10 15

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 10 / 23

slide-11
SLIDE 11

hist - Histogram with estimated density

R> hist(iris$Sepal.Length, breaks=20, prob=TRUE) R> lines(density(iris$Sepal.Length), col="red")

Histogram of iris$Sepal.Length

iris$Sepal.Length Density 5 6 7 8 0.0 0.1 0.2 0.3 0.4 0.5

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 11 / 23

slide-12
SLIDE 12

image

volcano is a R data set with elevation measurements of Maunga Whau on a 10m by 10m grid.

R> dim(volcano) [1] 87 61 R> image(volcano)

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 12 / 23

slide-13
SLIDE 13

contour

R> contour(volcano)

100 100 100 1 1 110 1 1 110 1 2 130 140 1 5 160 160 170 170 1 8 180 190

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 13 / 23

slide-14
SLIDE 14

persp

R> persp(volcano)

volcano Y Z

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 14 / 23

slide-15
SLIDE 15

Typical Arguments for plot functions

add=TRUE: Add to an existing plot? axes=FALSE: Plot axes? log="x", log="y" or log="xy": Log. axes? type="l": Plot lines instead of points xlab, ylab: Axis labels main: Figure title sub: Sub-title

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 15 / 23

slide-16
SLIDE 16

Table of Contents

1

Simple Plots

2

High-level Graphics Functions

3

Low-level Graphics Functions

4

Exercises

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 16 / 23

slide-17
SLIDE 17

Some low-level functions

These functions can be used to add elements to a plot. points(x, y) lines(x, y) text(x, y, labels, ...) abline(a, b) or abline(h=y) or abline(v=x) polygon(x, y, ...) legend(x, y, legend, ...) title(main, sub) axis(side, ...)

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 17 / 23

slide-18
SLIDE 18

Graphical parameter list: par

R maintains a list of graphics parameters to control line style, colors, figure arrangement and text justification. A separate list of graphics parameters is maintained for each active device.

R> oldpar <- par(col=4, pch=4) R> par(oldpar)

Many parameters from par() can also be passed to plot(). Try par() and ?par

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 18 / 23

slide-19
SLIDE 19

Important parameters in par

pch=4: Plotting symbol (0-25) lty=2: Line type lwd=2: Line width col=2: Color for points, lines, etc. cex=1.5: Character expansion (e.g., 50% larger than default text size) mai=c(1, 0.5, 0.5, 0): Widths of the bottom, left, top and right margins, respectively, measured in inches.

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 19 / 23

slide-20
SLIDE 20

Saving a plot as an image

R> png(file="plot.png") R> plot(1:10) R> dev.off() pdf 2 Other devices are jpeg(), tiff(), pdf(), postscript(), win.metafile() (Windows). Use ?Devices for a complete list.

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 20 / 23

slide-21
SLIDE 21

Interactive and Advanced Graphics

Interactive Graphics are available via several extension packages: ggplot2: Grammar of graphics. rggobi: GGobi interactive graphics system. iplots: Java based plotting (alpha blending, brushing, selection, etc.) playwith: Build interactive versions of R graphics rgl: OpenGL Advanced Graphics ggplot2: Grammar of graphics. Produces elegant visualizations (see http://ggplot2.org/). grid: Advanced graphics can be programmed using flexible low level ploting functions (viewports, different coordinate systems and units, lines, points, text, etc.) See also package lattice.

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 21 / 23

slide-22
SLIDE 22

Table of Contents

1

Simple Plots

2

High-level Graphics Functions

3

Low-level Graphics Functions

4

Exercises

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 22 / 23

slide-23
SLIDE 23

Exercises

1 Plot a sin(x)/x. Hint: Trigonometric functions in R use angles in

radians (see sin)

2 The“cars”data set gives the speed of cars and the distances taken to

  • stop. Note that the data were recorded in the 1920s. Plot the“cars”

data set as a scatter plot. Plot all data points with distances taken to stop greater than 80 in red.

3 Plot histograms for speed and dist in“cars”

.

Michael Hahsler (SMU)

  • Adv. Sci. Comp. with R

February 16, 2014 23 / 23