Lrz kurse: r visualisation
-
Upload
ferdinand-jamitzky -
Category
Software
-
view
102 -
download
10
Transcript of Lrz kurse: r visualisation
Scientific Visualisation using R
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
Introduction to R
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
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")
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
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
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
Matrices II
t(a)
solve(a)a %*% solve(a)
data.matrix(a)
transpose matrix
inverse of matrix
get data as matrix
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
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
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
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
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
Standard graphics lib
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
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
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
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)
TeXmacs
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.
WYSIWYG
TeXmacs supportsmath notation and can export to Latex.
you can run computeralgebra sessions inside
TeXmacs
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()
iPlots and JGR
Installing JGR
goto http://rforge.net/JGR/
> Downloads
Load Data to Object Browser
library(MASS)data(Cars93)attach(Cars93)
Ctrl-B or
select Tools > Object Browser
from menu
Scatterplot
iplot(Horsepower, MPG.city)
Histogram
ihist(Horsepower)
Mixing them together
Select some points in the scatterplot, then a sub-regionof the histogram is selected
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])
Rainbow plot
Select from menu: View > Set Colors (rainbow)
Get Indices
You can obtain information about the selected points:
and select points by the commandiset.select
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()
opengl binding
opengl bindings for R
●library(rgl)●abstraction for opengl primitives:
osurfaces, spheres, triangles
opoints, lines, text, sprites
olights, background, materials, textures
Examples
example(bg3d)
demo(regression)example(particles3d)
Primitives
Appearance
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
rgl.spheres(x,y,z,col=col,radius=rad)
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
rgl.surface(x, z, y, back="lines")