Lrz kurse: r visualisation

42
Scientific Visualisation using R [email protected]

Transcript of Lrz kurse: r visualisation

Page 1: Lrz kurse: r visualisation

Scientific Visualisation using R

[email protected]

Page 2: Lrz kurse: r visualisation

Schedule

1.Introduction to R

2.Graphics library

3.texmacs notebook

4.iplots library

5.rgl 

overview for MATLAB and IDL convertitesstandard plotting

Mathematica style notebook with latex output

Page 3: Lrz kurse: r visualisation

Introduction to R

Page 4: Lrz kurse: r visualisation

Starting R

In the terminal type:$ RR version 2.5.1 (2007-06-27)Copyright (C) 2007 The R Foundation for Statistical ComputingISBN 3-900051-07-0> 

$ R --no-save < myfile.R

$ R CMD BATCH myfile.R

Page 5: Lrz kurse: r visualisation

Running programs

Open myfile.R in your favorite editor and type: print("Hello World from R!")

Load R source file and executesource("myfile.R")

Define a functionhello <- function(x) paste("Hello","World","from",x)

hello("R")

Page 6: Lrz kurse: r visualisation

Getting Information

Sys.info()

system("pwd",int=T)        

system.time(sum(1:100))

x=runif(100)        

str(x)                  print(x)              fix(x)                  summary(x)       

get system info

call shell command      

get elapsed time

random uniform numbers

structure of xprintquick editstatistical summary

Page 7: Lrz kurse: r visualisation

Vectors

a = c(4,5,3,4,3)

a=1:5

a=rep(1:5,2)

a=rep(1:4,1:4)

a[-(1:3)]

a[a > 2]

define vector (concat)

define range

repeat data

repeat individual

delete indices

select elements

Page 8: Lrz kurse: r visualisation

Matrices

a = matrix(runif(9),c(3,3))a= matrix(1, c(3,3))

b = a  %*%  ab = a %x% ab = a %o% a

d=diag(1:3)+1

d[2,]d[,3]

square matrix 3x3

matrix productkronecker productouter product

diagonal matrix

2nd row3rd column

Page 9: Lrz kurse: r visualisation

Matrices II

t(a)

solve(a)a %*% solve(a)

data.matrix(a)

transpose matrix

inverse of matrix

get data as matrix

Page 10: Lrz kurse: r visualisation

Arrays

a=array(runif(24),c(2,3,4))

apply(a,2,sum)

sum(a)

dim(a)

as.vector(a)c(a)

3-dim array

sum array over 2nd index

total sum of a

information about a

map to 1d vector

Page 11: Lrz kurse: r visualisation

Lists

a=list(x=1:3,y=c("a","b","c"))

names(a)

unlist(a)

str(a)

c(a,a)

define list

get names of a

make vector

structure of a

concatenate

Page 12: Lrz kurse: r visualisation

Factors

factors are also called categories or enunms

a=c("a","b","a","c","a")

a.f = factor(a)

levels(a.f)

str(a.f)

summary(a.f)

vector

define factor

get unique levels of a

structure of factor

histogram

Page 13: Lrz kurse: r visualisation

Data Frames

data frames are "excel" data sheets

a=data.frame(x=1:3,y=c("a","b","c"),z=runif(3))

str, summary

read.table()read.csv()read.delim()read.fwf()

 definition like a list

information about data frame

read data from source

Page 14: Lrz kurse: r visualisation

Loops

a=matrix(runif(100),c(10,10))

apply(a,2,sum)

apply(a,2,function(x) sum(x))

lapply(1:10, function(x) runif(10))

sapply(1:10, function(x) runif(10))

mapply, vapply, tapply

define matrix

sum columns

apply function to columns

apply function to list

apply function to vector

Page 15: Lrz kurse: r visualisation

Standard graphics lib

Page 16: Lrz kurse: r visualisation

Plotting

x=1:100y=runif(100)

plot(x,y)lines(y/2, col="green")points(y/3, col="red")

type="p""l""b""h""s"

define vectors

x-y dot plotoverplot linesoverplot points

pointslinesbothhistogramstairs

Page 17: Lrz kurse: r visualisation

Keywords

plot(x,y,...)

main="The Main Title"sub="the subtitle"xlab="x axis"ylab="y axis"log="x"

par()

main titlefigure captionlabels x and y

additional parameters

Page 18: Lrz kurse: r visualisation

2D Plots

data(volcano)

image(volcano)contour(volcano,add=T)

image(volcano,col=terrain.colors(100))

filled.contour(volcano)

load example data

image mapcontour plot

different color tablerainbow, hsv, heat.colors,topo.colors

nice contours

Page 19: Lrz kurse: r visualisation

3D Plots

wire frame (simple version)persp(volcano)

wire frame (nice version)z <- 2 * volcano       x <- 10 * (1:nrow(z))  y <- 10 * (1:ncol(z)) persp(x, y, z, theta = 135, phi = 30, col = "green3",     scale = FALSE, ltheta = -120, shade = 0.75,     border = NA, box = FALSE)

Page 20: Lrz kurse: r visualisation

TeXmacs

Page 21: Lrz kurse: r visualisation

Overview

GNU TeXmacs is a free wysiwyw (what you see is what you want) editing platform with special features for scientists. TeXmacs runs on all major Unix platforms and Windows. Documents can be saved in TeXmacs, Xml or Scheme format and printed as Postscript or Pdf files. Converters exist for TeX/LaTeX and Html/Mathml.

Page 22: Lrz kurse: r visualisation

WYSIWYG

TeXmacs supportsmath notation and can export to Latex.

you can run computeralgebra sessions inside

TeXmacs

Page 23: Lrz kurse: r visualisation

Using R inside TeXmacs

log in to gvs

module load texmacs

> Insert > Session > R

> library(TeXmacs)> data(volcano)> image(volcano)> contour(volcano,add=T)> v()

Page 24: Lrz kurse: r visualisation

iPlots and JGR

Page 25: Lrz kurse: r visualisation

Installing JGR

goto http://rforge.net/JGR/

> Downloads

Page 26: Lrz kurse: r visualisation

Load Data to Object Browser

library(MASS)data(Cars93)attach(Cars93)

Ctrl-B or 

select     Tools >     Object Browser 

from menu

Page 27: Lrz kurse: r visualisation

Scatterplot

iplot(Horsepower, MPG.city)

Page 28: Lrz kurse: r visualisation

Histogram

ihist(Horsepower)

Page 29: Lrz kurse: r visualisation

Mixing them together

Select some points in the scatterplot, then a sub-regionof the histogram is selected

Page 30: Lrz kurse: r visualisation

Multiplot

imosaic(data.frame(AirBags,Cylinders,Origin))ipcp(Cars93[c(4:8,12:15,17,19:25)])iplot(Horsepower,EngineSize)ihist(Horsepower)ibox(Cars93[4:6])

Page 31: Lrz kurse: r visualisation

Rainbow plot

Select from menu:      View > Set Colors (rainbow)

Page 32: Lrz kurse: r visualisation

Get Indices

You can obtain information about the selected points:

and select points by the commandiset.select

Page 33: Lrz kurse: r visualisation

Overview

●iset.brush()●iset.col()●iset.cur()●iset.df()●iset.list()●iset.new()●iset.next()●iset.prev()●iset.sel.changed()●iset.select()●iset.selectAll()●iset.selected()●iset.selectNone()●iset.set()

● iplot.cur()● iplot.data()● iplot.list()● iplot.new()● iplot.next()● iplot.off()● iplot.opt()● iplot.prev()● iplot.set()

● iobj.cur()● iobj.get()● iobj.list()● iobj.next()● iobj.opt()● iobj.prev()● iobj.rm()● iobj.set()

Page 34: Lrz kurse: r visualisation

opengl binding

Page 35: Lrz kurse: r visualisation

opengl bindings for R

●library(rgl)●abstraction for opengl primitives:

osurfaces, spheres, triangles

opoints, lines, text, sprites

olights, background, materials, textures

Page 36: Lrz kurse: r visualisation

Examples

example(bg3d)

demo(regression)example(particles3d)

Page 37: Lrz kurse: r visualisation

Primitives

Page 38: Lrz kurse: r visualisation

Appearance

Page 39: Lrz kurse: r visualisation

Simple Example: colored spheres

rgl.clear("all")rgl.bg(sphere = T, back = "lines")rgl.light()rgl.bbox()x=runif(1000)y=runif(1000)z=runif(1000)col=rainbow(1000)rad=runif(1000)/10rgl.spheres(x,y,z,col=col,radius=rad)

clear spaceset backgroundsome lightbounding boxsome coordinates

set colorsset radiigenerate the spheres

Page 40: Lrz kurse: r visualisation

rgl.spheres(x,y,z,col=col,radius=rad)

Page 41: Lrz kurse: r visualisation

Simple Example: surface

rgl.clear("shapes")

data(volcano)y = 2*volcanox=10 * (1:nrow(y))z=10 * (1:ncol(y))rgl.surface(x, z, y, back="lines")

clear space

some datadefine coordinates

plot surface

Page 42: Lrz kurse: r visualisation

rgl.surface(x, z, y, back="lines")