lrz kurse: r visualisation
TRANSCRIPT
![Page 2: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/3.jpg)
Introduction to R
![Page 4: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/15.jpg)
Standard graphics lib
![Page 16: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/20.jpg)
TeXmacs
![Page 21: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/22.jpg)
WYSIWYG
TeXmacs supportsmath notation and can export to Latex.
you can run computeralgebra sessions inside
TeXmacs
![Page 23: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/24.jpg)
iPlots and JGR
![Page 25: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/25.jpg)
Installing JGR
goto http://rforge.net/JGR/
> Downloads
![Page 26: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/27.jpg)
Scatterplot
iplot(Horsepower, MPG.city)
![Page 28: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/28.jpg)
Histogram
ihist(Horsepower)
![Page 29: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/29.jpg)
Mixing them together
Select some points in the scatterplot, then a sub-regionof the histogram is selected
![Page 30: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/31.jpg)
Rainbow plot
Select from menu: View > Set Colors (rainbow)
![Page 32: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/32.jpg)
Get Indices
You can obtain information about the selected points:
and select points by the commandiset.select
![Page 33: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/34.jpg)
opengl binding
![Page 35: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/36.jpg)
Examples
example(bg3d)
demo(regression)example(particles3d)
![Page 37: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/37.jpg)
Primitives
![Page 38: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/38.jpg)
Appearance
![Page 39: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/40.jpg)
rgl.spheres(x,y,z,col=col,radius=rad)
![Page 41: Lrz kurse: r visualisation](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081514/55c415a0bb61eb2b058b459d/html5/thumbnails/42.jpg)
rgl.surface(x, z, y, back="lines")