Professional Documents
Culture Documents
R Graphics
R Graphics
Slide 1/76
Overview
Specialty Graphics
Slide 2/76
Outline
Overview
Specialty Graphics
Overview
Slide 3/76
Graphics in R
Powerful environment for visualizing scientic data Integrated graphics and statistics infrastructure Publication quality graphics Fully programmable Highly reproducible
A Full L TEX
Link
& Sweave
Link
support
Overview
Slide 4/76
Documentation on Graphics in R
R Graphical Manual
Paul Murrells book R (Grid) Graphics Interactive graphics rggobi (GGobi) iplots
Link Link Link
Open GL (rgl)
Overview
Slide 5/76
Graphics Environments
Viewing and saving graphics in R On-screen graphics postscript, pdf, svg jpeg/png/wmf/ti/... Four major graphic environments Low-level infrastructure
R Base Graphics (low- and high-level) grid : Manual Link , Book Link
High-level infrastructure
lattice: Manual Link , Intro Link , Book Link ggplot2: Manual Link , Intro Link , Book Link
Overview
Slide 6/76
Outline
Overview
Specialty Graphics
Graphics Environments
Slide 7/76
Outline
Overview Graphics Environments Base Graphics Grid Graphics lattice ggplot2 Specialty Graphics
Graphics Environments
Base Graphics
Slide 8/76
Graphics Environments
Base Graphics
Slide 9/76
Graphics Environments
Base Graphics
Slide 10/76
0.8
y[, 2]
0.6
0.4
q q q
0.2
q q q
0.2
0.4 y[, 1]
0.6
0.8
Graphics Environments
Base Graphics
Slide 11/76
0.4
0.6
0.8
q
q q q q q q q
q q q
0.8
q q
q q
0.6
0.4
q q q q q q
B
q q q
q q
0.2
q q
q q q q q q q
q q q q q
q q q
0.2
0.4
0.6
0.8
0.0
0.2
0.4
0.6
0.8
1.0
Graphics Environments
Base Graphics
0.0
0.2
0.4
0.6
0.8
1.0
0.2
0.4
0.6
0.8
Slide 12/76
j
q
0.8
e g
0.6
y[, 2]
a
q
0.4
f
q
0.2
d
q
i 0.6 0.8
0.2
0.4 y[, 1]
Graphics Environments
Base Graphics
Slide 13/76
Important arguments mar: species the margin sizes around the plotting area in order: c(bottom, left, top, right) col: color of symbols pch: type of symbols, samples: example(points) lwd: size of symbols cex.*: control font sizes For details see ?par
Graphics Environments
Base Graphics
Slide 14/76
Add a regression line to a plot > plot(y[,1], y[,2]) > myline <- lm(y[,2]~y[,1]); abline(myline, lwd=2) > summary(myline) Same plot as above, but on log scale > plot(y[,1], y[,2], log="xy") Add a mathematical expression to a plot > plot(y[,1], y[,2]); text(y[1,1], y[1,2], > expression(sum(frac(1,sqrt(x^2*pi)))), cex=1.3)
Graphics Environments
Base Graphics
Slide 15/76
Graphics Environments
Base Graphics
Slide 16/76
0.6
0.8
6 Index
10
Graphics Environments
Base Graphics
Slide 17/76
Intensity
0.0
0.2
0.4
0.6
0.8
1.0
6 Measurement
10
Graphics Environments
Base Graphics
Slide 18/76
1.2
0.93
a b c d
0.6
0.8
1.0
0.4
0.31
0.12 0.05 0 C
0.0
Graphics Environments
Base Graphics
Slide 19/76
10
Graphics Environments
Base Graphics
Slide 20/76
Histograms
> hist(y, freq=TRUE, breaks=10)
Histogram of y
4 Frequency 0 0.0 1 2 3
0.2
0.4 y
0.6
0.8
1.0
Graphics Environments
Base Graphics
Slide 21/76
Density Plots
> plot(density(y), col="red")
density.default(x = y)
Density
0.0
0.2
0.4
0.6
0.8
1.0
0.0
1.0
Graphics Environments
Base Graphics
Slide 22/76
Pie Charts
> pie(y[,1], col=rainbow(length(y[,1]), start=0.1, end=0.8), clockwise=TRUE) > legend("topright", legend=row.names(y), cex=1.3, bty="n", pch=15, pt.cex=1.8, + col=rainbow(length(y[,1]), start=0.1, end=0.8), ncol=1)
j i
a b
c g
a b c d e f g h i j
d f e
Graphics Environments
Base Graphics
Slide 23/76
"gray"
> palette(rainbow(5, start=0.1, end=0.2)) > palette() [1] "#FF9900" "#FFBF00" "#FFE600" "#F2FF00" "#CCFF00" > palette("default") The gray function allows to select any type of gray shades by providing values from 0 to 1 > gray(seq(0.1, 1, by= 0.2)) [1] "#1A1A1A" "#4D4D4D" "#808080" "#B3B3B3" "#E6E6E6" Color gradients with colorpanel function from gplots library > library(gplots) > colorpanel(5, "darkblue", "yellow", "white") Much more on colors in R see Earl Glynns color chart
Link
Graphics Environments
Base Graphics
Slide 24/76
10
10
q q q q
q q q q
10
q q q q
1:10
1:10
q q q q q
q q q q q
1:10
q q q q q q
6 Index
10
6 Index
10
4 2
6 Index
10
10
10
q q q q
q q q q
10
q q q q
1:10
1:10
q q q q q
q q q q q
1:10
q q q q q q
6 Index
10
6 Index
10
4 2
6 Index
10
Graphics Environments
Base Graphics
Slide 25/76
10
Graphics Environments
Base Graphics
Slide 26/76
After the pdf() command all graphs are redirected to le test.pdf. Works for all common formats similarly: jpeg, png, ps, ti, ... > pdf("test.pdf"); plot(1:10, 1:10); dev.off() Generates Scalable Vector Graphics (SVG) les that can be edited in vector graphics programs, such as InkScape. > library("RSvgDevice"); devSVG("test.svg"); plot(1:10, 1:10); dev.off()
Graphics Environments
Base Graphics
Slide 27/76
virginica 50
Graphics Environments Base Graphics Slide 28/76
Outline
Overview Graphics Environments Base Graphics Grid Graphics lattice ggplot2 Specialty Graphics
Graphics Environments
Grid Graphics
Slide 29/76
What is grid ?
Low-level graphics system Highly exible and controllable system Does not provide high-level functions Intended as development environment for custom plotting functions Pre-installed on new R distributions
Graphics Environments
Grid Graphics
Slide 30/76
Outline
Overview Graphics Environments Base Graphics Grid Graphics lattice ggplot2 Specialty Graphics
Graphics Environments
lattice
Slide 31/76
lattice Environment
What is lattice ?
High-level graphics system Developed by Deepayan Sarkar Implements Trellis graphics system from S-Plus Simplies high-level plotting tasks: arranging complex graphical features Syntax similar to Rs base graphics
Graphics Environments
lattice
Slide 32/76
A
8
4
q
2
q
1:8
D
q q q q
1:8
Graphics Environments
lattice
Slide 33/76
Min
versicolor
Max
Min
setosa
Max
Graphics Environments
lattice
Slide 34/76
Outline
Overview Graphics Environments Base Graphics Grid Graphics lattice ggplot2 Specialty Graphics
Graphics Environments
ggplot2
Slide 35/76
ggplot2 Environment
What is ggplot2 ?
High-level graphics system Implements grammar of graphics from Leland Wilkinson Streamlines many graphics workows for complex plots Syntax centered around main ggplot function Simpler qplot function provides many shortcuts
Link
Graphics Environments
ggplot2
Slide 36/76
ggplot2 Usage
ggplot function accepts two arguments
Data set to be plotted Aesthetic mappings provided by aes function
Additional parameters such as geometric objects (e.g. points, lines, bars) are passed on by appending them with + as separator. List of available geom_* functions:
Link
Settings of plotting theme can be accessed with the command theme_get() and its settings can be changed with opts(). Preferred input data object
qgplot: data.frame (support for vector, matrix, ...) ggplot: data.frame
qplot Function
Graphics Environments
ggplot2
Slide 38/76
Graphics Environments
ggplot2
Slide 39/76
q
8
q
q
q
q
y
4
q
2
q
q
10
Graphics Environments
ggplot2
Slide 40/76
25000
20000
q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q
15000
q q q q q q q q q q q q q q q q
price
10000
5000
0.5
1.0
1.5
2.0
2.5
3.0
3.5
carat
Graphics Environments
ggplot2
Slide 41/76
15000
q q
price
10000
5000
0.5
1.0
1.5
2.0
2.5
3.0
3.5
carat
Graphics Environments
ggplot2
Slide 42/76
ggplot Function
More important than qplot to access full functionality of ggplot2 Main arguments data set, usually a data.frame aesthetic mappings provided by aes function General ggplot syntax ggplot(data, aes(...)) + geom_*() + ... + stat_*() + ...
Layer specications geom_*(mapping, data, ..., geom, position) stat_*(mapping, data, ..., stat, position) Additional components scales coordinates facet aes() mappings can be passed on to all components (ggplot, geom_*, etc.). Eects are global when passed on to ggplot() and local for other components. x, y color: grouping vector (factor) group: grouping vector (factor)
Graphics Environments
ggplot2
Slide 43/76
Theme settings can be accessed with theme_get() Their settings can be changed with opts() Some examples Change background color to white
... + opts(panel.background=theme_rect(fill = "white", colour = "black"))
Graphics Environments
ggplot2
Slide 44/76
Plots and layers can be stored in variables > p <- ggplot(dsmall, aes(carat, price)) + geom_point() > p # or print(p) Returns information about data and aesthetic mappings followed by each layer > summary(p) Prints dots with dierent sizes and colors
> bestfit <- geom_smooth(methodw = "lm", se = F, color = alpha("steelblue", 0.5), > p + bestfit # Plot with custom regression line Syntax to pass on other data sets > p %+% diamonds[sample(nrow(diamonds), 100),] Saves plot stored in variable p to le > ggsave(p, file="myplot.pdf")
Graphics Environments
ggplot2
Slide 45/76
15000
color
price
10000
q q q q q q q
D E F G H I J
5000
carat
Graphics Environments
ggplot2
Slide 46/76
20000
q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q
15000
q q q q q q q q q
price
10000
5000
0.5
1.0
1.5
2.0
2.5
3.0
3.5
carat
Graphics Environments
ggplot2
Slide 47/76
20000
q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq q q qq qq q q q q q q qq qq q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq qq q qq qq q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq q q q q q q qq q q q qq q q q q q q qqq q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q qq q q q qq q q q q q q qq q q q q q q qq q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q qq q q q q qq q q q q q q qq q q q q q qq q q q q q q q q q q q q q q q q q q q qq q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q
15000
color
q q q q q q q q
D E F G H I J
price
10000
5000
0.5
1.0
1.5
2.0
2.5
3.0
3.5
carat
Graphics Environments
ggplot2
Slide 48/76
15000
q q
price
10000
5000
0.5
1.0
1.5
2.0
2.5
3.0
3.5
carat
Graphics Environments
ggplot2
Slide 49/76
2.0
Petal.Width
1.5
1.0
0.5
Petal.Length
Graphics Environments
ggplot2
Slide 50/76
ggplot: Faceting
> p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + + geom_line(aes(color=Species), size=1) + + facet_wrap(~Species, ncol=1) > print(p)
setosa 4.0 3.5 3.0 2.5 2.0 versicolor
Sepal.Width
4.0 3.5 3.0 2.5 2.0 virginica 4.0 3.5 3.0 2.5 2.0 5 6 7
Sepal.Length
Graphics Environments
ggplot2
Slide 51/76
virginica 50
Graphics Environments ggplot2 Slide 52/76
Graphics Environments
ggplot2
Slide 53/76
Values
virginica
Samples
Graphics Environments
ggplot2
Slide 54/76
Sepal.Width
Sepal.Length
Samples
Petal.Length
Values
Graphics Environments
ggplot2
Slide 55/76
Values
Samples
Graphics Environments
ggplot2
Slide 56/76
> p <- ggplot(df_mean, aes(Samples, Values, fill = Species)) + + geom_bar(position="dodge") + geom_errorbar(limits, position="dodge" > print(p)
Species
Values
Samples
Graphics Environments
ggplot2
Slide 57/76
Species
Values
Samples
Graphics Environments
ggplot2
Slide 58/76
Species
Values
Samples
Graphics Environments
ggplot2
Slide 59/76
Graphics Environments
ggplot2
Slide 60/76
df <- convertDF(y, mycolnames=c("Position", "Values", "Samples")) p <- ggplot(df, aes(Position, Values)) + geom_line(aes(color=Samples)) + facet_wrap(~Samples, ncol=1) print(p) ## Represent same data in box plot ## ggplot(df, aes(Samples, Values, fill=Samples)) + geom_boxplot()
Sample1 2 0 2 Sample2 2 0 2 Sample3 2 0 2 Sample4 2 0 2 Sample5 2 0 2 Samples Sample1 Sample2 Sample3 Sample4 Sample5
Values
Graphics Environments
ggplot2
Slide 61/76
12000
price/carat
2000
color
Graphics Environments
ggplot2
Slide 62/76
12000
q q q q q q q q q q q q q q q q q q q q
10000
color D E F G
price/carat
8000
6000
H I J
4000
2000
color
Graphics Environments
ggplot2
Slide 63/76
1.5
color 1.0 D E
density
F G H I 0.5 J
carat
Graphics Environments
ggplot2
Slide 64/76
1.5
color 1.0 D E
density
F G H I 0.5 J
carat
Graphics Environments
ggplot2
Slide 65/76
ggplot: Histograms
> p <- ggplot(iris, aes(x=Sepal.Width)) + geom_histogram(aes(y = ..density.., + fill = ..count..), binwidth=0.2) + geom_density() > print(p)
1.2
1.0
density
0.2
Sepal.Width
Graphics Environments
ggplot2
Slide 66/76
Pie Chart
10 12
""
value
Graphics Environments
ggplot2
Slide 67/76
Pie Chart
3
variable
bird
value
Graphics Environments
ggplot2
Slide 68/76
library(grid) a <- ggplot(dsmall, aes(color, price/carat)) + geom_jitter(size=4, alpha = I(1 / 1.5), aes(color=color)) b <- ggplot(dsmall, aes(color, price/carat, color=color)) + geom_boxplot() c <- ggplot(dsmall, aes(color, price/carat, fill=color)) + geom_boxplot() + opts(legend.position = "none" grid.newpage() # Open a new page on grid device pushViewport(viewport(layout = grid.layout(2, 2))) # Assign to device viewport with 2 by 2 grid layout print(a, vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2)) print(b, vp = viewport(layout.pos.row = 2, layout.pos.col = 1)) print(c, vp = viewport(layout.pos.row = 2, layout.pos.col = 2, width=0.3, height=0.3, x=0.8, y=0.8))
Graphics Environments
ggplot2
Slide 69/76
price/carat
color
q q
10000
8000
q q
price/carat
6000
F G
price/carat
6000
4000
H I J
4000
2000
2000
color
color
Graphics Environments
ggplot2
Slide 70/76
color
q
10000 10000
D E F G H I
price/carat
q q
8000
D EF G HIJ
color
J color D E
price/carat
6000
F G H I J
4000
2000
color
Graphics Environments
ggplot2
Slide 71/76
Outline
Overview
Specialty Graphics
Specialty Graphics
Slide 72/76
Row ZScore
g17 g5 g90 g6 g100 g45 g92 g73 g36 g2 g99 g35 g95 g77 g4 g74 g55 g69 g65 g49 g24 g28 g13 g75 g30 g89 g53 g46 g64 g29 g62 g82 g63 g26 g78 g58 g98 g9 g51 g48 g20 g12 g3 g15 g10 g70 g19 g7 g52 g72 g59 g47 g34 g50 g96 g43 g84 g32 g60 g40 g23 g42 g37 g80 g31 g56 g39 g87 g76 g81 g27 g68 g61 g88 g21 g54 g22 g85 g83 g97 g25 g91 g57 g1 g38 g14 g93 g86 g16 g8 g79 g11 g71 g67 g44 g33 g94 g41 g66 g18
t4
t1
t3
t2
Specialty Graphics
t5
Slide 73/76
setlist5 <- list(A=sample(letters, 18), B=sample(letters, 16), C=sample(letters, 20), D=sample(letters, 2 OLlist5 <- overLapper(setlist=setlist5, sep="_", type="vennsets") counts <- sapply(OLlist5$Venn_List, length) # pdf("venn.pdf") vennPlot(counts=counts, ccol=c(rep(1,30),2), lcex=1.5, ccex=c(rep(1.5,5), rep(0.6,25),1.5)) # dev.off()
Specialty Graphics
Slide 74/76
A 0 E
0 0 0 0 1 1 2 0 0
B 0
0
0 1 1
2 2
5
0 1 1 2 3 2 0
2 0 0 0
0 D
Specialty Graphics
Slide 75/76
CMP1
N q O q
H q N q
O q
O q
N q O q O q
O q
N q H q
Specialty Graphics
Slide 76/76