+ All Categories
Home > Documents > R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics...

R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics...

Date post: 01-Apr-2015
Category:
Upload: kendra-rydell
View: 213 times
Download: 1 times
Share this document with a friend
Popular Tags:
25
R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January 2006
Transcript
Page 1: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

R Graphicsusing the

Grid Package

Sigal BlayStatistical Genetics Working Group

Dept. Of Statistics and Actuarial ScienceSimon Fraser University

January 2006

Page 2: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Grid Low level graphics system Produces editable graphical components

(not just graphics output) Object Oriented - graphical components

can be reused and recombined

Page 3: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

A standard set of graphical primitives:

grid.rect(...) grid.lines(...) grid.polygon(...)grid.circle(...)grid.text(...)

library(help = grid) for details

text

0 0.25 0.5 0.75 1

Page 4: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

For every function of the form grid.* there is anequivalent function *Grob that returns a graphical objectbut doesn't draw it on the graphics device:grid.rect(...) rectGrob(...)grid.lines(...) linesGrob(...)grid.polygon(...) polygonGrob(...)grid.circle(...) circleGrob(...)grid.text(...) textGrob(..)

Usage:Rect <- rectGrob(...) grid.draw(Rect)

Page 5: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Graphical output can be positioned and sized

relative to a large number of coordinate systems

grid.rect(x = unit(0, “native"), y = unit(1.5, “npc"), height = unit(0.5, "inches"), width = unit(0.8, “lines”))

help(unit) for details

Page 6: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Can specify various graphical parametersgrid.rect(gp=gpar(col="red", lwd=2,

fill="yellow", lty="dotted"))grid.text(gp=gpar(col="red", fontsize=10,

fontface="italic"))

help(gpar)for details

text

0 0.25 0.5 0.75 1

Page 7: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Working with viewportsAll drawing occurs in the current grid viewport,a defined rectangular region with it's own definedcoordinate system:vp <- viewport(width=0.5, height=0.2, angle=45)

grid.show.viewport(vp)

help(viewport)for details 0.5npc

0.5n

pc

Page 8: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

vp <- viewport(width=0.5, height=0.2, angle=45, name="VP")pushViewport(vp)grid.rect()popViewport()

Page 9: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

vp <- viewport(width=0.5, height=0.2, angle=45, name="VP")pushViewport(vp)grid.rect() grid.xaxis()grid.yaxis()

00.

20.

40.

60.

81

00.2

0.6

1

Page 10: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

vp <- viewport(width=0.5, height=0.2, angle=45, name="VP")pushViewport(vp)grid.rect()grid.xaxis()grid.yaxis()grid.text("viewport region", y = 0.9)

00.

20.

40.

60.

81

00.2

0.6

1vie

wport

regio

n

Page 11: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

vp <- viewport(width=0.5, height=0.2, angle=45, name="VP")pushViewport(vp)grid.rect()grid.xaxis()grid.yaxis()grid.text("viewport region", y = 0.9)upViewport()grid.text("root region",y = 0.1)

00.

20.

40.

60.

81

00.2

0.6

1vie

wport

regio

n

root region

Page 12: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

vp <- viewport(width=0.5, height=0.2, angle=45, name="VP")pushViewport(vp)grid.rect()grid.xaxis()grid.yaxis()grid.text("viewport region", y = 0.9)upViewport()grid.text("root region",y = 0.1)grid.text("more text", vp="VP“ y=0.7) 0

0.2

0.4

0.6

0.8

1

00.2

0.6

1vie

wport

regio

n

root region

mor

e te

xt

Page 13: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Query the viewport tree

> current.viewport()viewport[ROOT] > current.vpTree()viewport[ROOT]->(viewport[VP]) > downViewport("VP")> popViewport()> current.vpTree()viewport[ROOT]

Page 14: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Can push multiple viewport

pushViewport(vpList(vp1, vp2, vp3))

Can push nested viewports pushViewport(vpStack(vp1, vp2, vp3)) pushViewport(vpTree(vp1, vpList(vp2, vp3)))

Can integrate with a layout

Page 15: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

plotViewport(...)a convenience function for producing a viewport

with a central plot region surrounded by margins given in terms of a number of lines of text.

dataViewport(...)a convenience function for producing a viewport

with x- and/or y-scales based on numeric values passed to the function.

Page 16: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

grid creates graphical objects (grobs):

grid.rect(..., name="box")grid.circle(...)grid.edit("box", gp=gpar(fill="yellow"))grid.remove("box")

Page 17: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

gTree – a tree of grobsvp<-viewport(..., name="view")x <- xaxisGrob(name = "axis1")y <- yaxisGrob(name = "axis2")points <- pointsGrob(1:9, 1:9,

name="dataPoints")title <- textGrob(..., name="myTitle")

tree <- gTree(name="Tree”, vp=vp, children=gList(x,y,title,points))grid.draw(tree)

Page 18: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

0 2 4 6 8 10

0

2

4

6

8

10gTree - a tree of

graphical objects

Page 19: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Query and edit a gTree> getNames() # list all top-level grobs[1] "Tree“

> childNames(tree)[1] "axis1" "axis2" "myTitle“ "dataPoints"> childNames(grid.get("Tree"))[1] "axis1" "axis2" "myTitle“ "dataPoints"

> grid.add("Tree", grid.rect())> grid.edit(gPath("Tree","dataPoints"), pch=2)

Page 20: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

0 2 4 6 8 10

0

2

4

6

8

10gTree - a tree of

graphical objects

Page 21: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Pairwise LD

Physical Length:8.9kb

*

*

rs2283092

rs6979287

Color Key

0 0.2 0.4 0.6 0.8 1

Example:LDheatmap package

Plots measures of pairwise linkage disequilibria for SNPs

Page 22: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

> getNames()[1] "ldheatmap"

> childNames(grid.get("ldheatmap"))[1] "heatMap" "geneMap" "Key“

> childNames(grid.get("heatMap"))[1] "heatmap" "title"

> childNames(grid.get("geneMap"))[1] "diagonal" "segments" "title" "symbols“ "SNPnames"

> childNames(grid.get("Key"))[1] "colorKey" "title" "labels" "ticks” "box"

Pairwise LD

Physical Length:8.9kb

*

*

rs2283092

rs6979287

Color Key

0 0.2 0.4 0.6 0.8 1

Page 23: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

grid.edit("symbols", pch=20, gp=gpar(cex=2))grid.edit(gPath("ldheatmap","heatMap","title"),

gp=gpar(col="red"))grid.edit(gPath("ldheatmap","heatMap","heatmap“), gp=gpar(col="white", lwd=2))

Pairwise LD

Physical Length:8.9kb

*

*

rs2283092

rs6979287

Color Key

0 0.2 0.4 0.6 0.8 1

Pairwise LD

Physical Length:8.9kb

rs2283092

rs6979287

Color Key

0 0.2 0.4 0.6 0.8 1

Page 24: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Conclusion

the R grid package

enables the production of

reusable and flexiblegraphical components

Page 25: R Graphics using the Grid Package Sigal Blay Statistical Genetics Working Group Dept. Of Statistics and Actuarial Science Simon Fraser University January.

Further reading

R Graphics / Paul Murrell


Recommended