Lec. 5: Customized Plot using Grid Packagestatistics.ssu.ac.kr/.../Lecture_PPT_files/lec5.pdf · 5....

Post on 31-Aug-2020

2 views 0 download

Transcript of Lec. 5: Customized Plot using Grid Packagestatistics.ssu.ac.kr/.../Lecture_PPT_files/lec5.pdf · 5....

Lec. 5: Customized Plot using Grid Package

Instructor: SANG-HOON CHO

DEPT. OF STATISTICS AND ACTUARIAL SCIENCES

Soongsil University

1 / 39

5. Controlling the Appearance of Output

All graphical primitives functions (and the viewport() function) havea gp argument that can be used to provide a set of graphicalparameters to control the appearance of the graphical output

There is a fixed set of graphical parameters summarized in thenext page, all of which can be specified for all types of graphicaloutput

get.gpar() #show current graphical parameter settings

get.gpar(c("lty", "fill"))

gpar(col="red", lty="dashed")

2 / 39

5. Controlling the Appearance of Output

3 / 39

5. Controlling the Appearance of Output

Exercise 1

다음코드를실행해보세요

grid.rect(gp=gpar(col="grey50", lwd = 20))

pushViewport(viewport(gp=gpar(fontsize=10, background = "transparent")))

grid.rect(x=0.33, height=0.7, width=0.2, gp=gpar(fill="black"))

grid.rect(x=0.66, height=0.7, width=0.2)

grid.text("grid.rect()", x=0.66, rot=90)

grid.text("grid.rect(gp=gpar(fill="black"))", x=0.33, rot=90,gp=gpar(fontsize=8, col="white"))

popViewport()

4 / 39

5. Controlling the Appearance of Output

grid.rect()

grid.rect(gp=gpar(fill="black"))

5 / 39

5. Controlling the Appearance of Output

The values that can be specified for colors, line types, line widths,line ends, line joins, and fonts are mostly the same as for thetraditional graphics system

In many cases, the graphical parameter in grid also has the samename as the traditional graphics state setting (e.g., col), thoughseveral of the grid parameters are slightly more verbose (e.g.lineend and fontfamily)

Some other differences in the specification of graphical parametervalues in the grid graphics system are described below

fontface: fontface value can be a string instead of an integer

cex: cex value is cumulative, i.e., multiplied by the previous cexvalue to obtain a current cex value

6 / 39

5. Controlling the Appearance of Output

7 / 39

5. Controlling the Appearance of Output

Exercise 2

다음코드를실행해보세요

pushViewport(viewport(gp=gpar(cex=0.5)))

grid.text("How small do you think?", gp=gpar(cex=0.5))

grid.newpage() #erases the current device or moves to a new page.

pushViewport(viewport(gp=gpar(cex=3)))

grid.text("How small do you think?", gp=gpar(cex=0.5))

8 / 39

5. Controlling the Appearance of Output

All graphical parameter settings may be vector values

grid.rect(gp=gpar(col="grey50", lwd = 10))

levels <- round(seq(90, 10, length=25))

greys <- paste("grey", c(levels, rev(levels)), sep="")

grid.circle(x=seq(0.1, 0.9, length=100),y=0.5 + 0.4*sin(seq(0, 2*pi, length=100)),r=abs(0.1*cos(seq(0, 2*pi, length=100))),gp=gpar(col=greys))

9 / 39

5. Controlling the Appearance of Output

10 / 39

5. Viewports

A viewport is a rectangular region that provides a context fordrawing

A viewport provides a drawing context consisting of both ageometric context and a graphical context

A geometric context consists of a set of coordinate systems forlocating and sizing output and all of the coordinate systemsareavailable within every viewport

A graphical context consists of explicit graphical parameter settingsfor controlling the appearance of output

This is specified as a gpar object via the gp argument

11 / 39

5. Viewports

A new viewport is created using the viewport() function

A viewport has a location (given by x and y), a size (given by widthand height), and it is justified relative to its location (according tothe value of the just argument)

The result of the viewport() function is an object of classviewport

No region has actually been created on a graphics device

In order to create regions on a graphics device, a viewport objectmust be pushed onto the device

> viewport(x=unit(0.4, "npc"), y=unit(1, "cm"),width=stringWidth("very very snug indeed"),height=unit(6, "lines"),just=c("left", "bottom"))

viewport[GRID.VP.17]

12 / 39

5. Viewports

The location and size of a viewport are specified in units, so aviewport can be positioned and sized within another viewport in avery flexible manner

vp1 <-viewport(x=unit(0.4, "npc"), y=unit(1, "cm"),

width=stringWidth("very very snug indeed"),height=unit(6, "lines"),just=c("left", "bottom")) #graphical object

grid.show.viewport(scale.col="grey", border.fill="white",vp.col="black", vp.fill="grey", vp1, newpage=FALSE)

pushViewport(viewport(.5, .5, .8, .8))

pushViewport(vp1)

grid.rect( gp = gpar(background = "transparent"))

grid.text("very very snug indeed",gp=gpar(col="white"))

popViewport(2) #Why need it?

13 / 39

5. Viewports

0 1

0

1 1strwidth

6lines

0.4npc

1cm

very very snug indeed

14 / 39

5. Viewports

The pushViewport() function takes a viewport object and uses itto create a region on the graphics device

This region becomes the drawing context for all subsequentgraphical output, until the region is removed or another region isdefined

grid.rect(gp=gpar(col="grey"))grid.text("top-left corner", x=unit(1, "mm"),

y=unit(1, "npc") - unit(1, "mm"),just=c("left", "top"))

pushViewport(viewport(width=0.8, height=0.5, angle=10,name="vp1"))

grid.rect(gp = gpar(background = "transparent"))grid.text("top-left corner", x=unit(1, "mm"),

y=unit(1, "npc") - unit(1, "mm"),just=c("left", "top"))

pushViewport(viewport(width=0.8, height=0.5, angle=10,name="vp2"))

grid.rect(gp = gpar(background = "transparent"))grid.text("top-left corner", x=unit(1, "mm"),

y=unit(1, "npc") - unit(1, "mm"),just=c("left", "top"))

15 / 39

5. Viewports

top-left corner

top-left corner

top-left corner

16 / 39

5. Viewports

The popViewport() function removes the current viewport and thedrawing context reverts to whatever it was before the currentviewport was pushed

The popViewport() function has an integer argument n thatspecifies how many viewports to pop

The default is 1, but several viewports can be popped at once byspecifying a larger value

The upViewport() function is similar to popViewport() in that thedrawing context reverts to whatever it was prior to the currentviewport being pushed

The difference is that upViewport() does not remove the currentviewport from the device

A viewport can be revisited using the downViewport() function

17 / 39

5. Viewports

Exercise 3

마지막으로실행했던코드에이어아래의코드를실행해보세요!

#popViewport()grid.text("bottom-right corner",

x=unit(1, "npc") - unit(1, "mm"),y=unit(1, "mm"), just=c("right", "bottom"))

#upViewport()grid.text("bottom-right corner",

x=unit(1, "npc") - unit(1, "mm"),y=unit(1, "mm"), just=c("right", "bottom"))

downViewport("vp1")grid.rect(width=unit(1, "npc") + unit(2, "mm"),

height=unit(1, "npc") + unit(2, "mm"))

18 / 39

5. Viewports

top-left corner

top-left corner

top-left corner

bottom-right corner

bottom-right corner

19 / 39

5. Viewports

Exercise 4

Drawing can be restricted to only the interior of the currentviewport (clipped to the viewport) by specifying the clip argumentto the viewport() function

three values: on, inherit, off

다음코드를실행해보고 clip arguement의각 value가의미하는것을생각해보세요!

grid.rect(gp=gpar(col="grey"))pushViewport(viewport(w=.5, h=.5, clip="on"))grid.rect()grid.circle(r=.7, gp=gpar(lwd=20))

pushViewport(viewport(clip="inherit"))grid.circle(r=.7, gp=gpar(lwd=10, col="grey"))

pushViewport(viewport(clip="off"))grid.circle(r=.7)popViewport(3)

20 / 39

5. Viewports

21 / 39

5. Viewports

A viewport can have a layout specified via the layout argument

It divides the viewport region into several columns and rows,where each column can have a different width and each row canhave a different height

For several reasons, layouts are very flexible in grid: there aremany more coordinate systems for specifying the widths ofcolumns and the heights of rows; viewports can occupyoverlapping areas within the layout; and each viewport within theviewport tree can have a layout (layouts can be nested)

22 / 39

5. Viewports

Exercise 5: Simple Layout

다음코드를실행해보세요!

vplay <- grid.layout(3, 3,respect=rbind(c(0, 0, 0),

c(0, 1, 0),c(0, 0, 0)))

grid.show.layout( vplay )

grid.newpage()pushViewport(viewport(layout=vplay))pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))grid.rect( gp = gpar( fill = 2) )upViewport()

pushViewport(viewport(layout.pos.row = 2, layout.pos.col = 2))grid.rect( gp = gpar( fill = 3) )upViewport()

pushViewport(viewport(layout.pos.row = 3, layout.pos.col = 3))grid.rect( gp = gpar( fill = 4) )upViewport()

23 / 39

5. Viewports

(1, 1)1null

1null

(1, 2)

1null

(1, 3) 1null

1null

(2, 1)1null (2, 2) (2, 3) 1null

(3, 1)1null

1null

(3, 2)

1null

(3, 3)

1null

1null

24 / 39

5. Viewports

25 / 39

5. Viewports

Exercise 6: Layout with Units

다음코드를실행해보세요!

unitlay <-grid.layout(3, 3,

widths=unit(c(1, 1, 2),c("inches", "null", "null")),

heights=unit(c(3, 1, 1),c("lines", "null", "null")))

grid.show.layout( unitlay )

26 / 39

5. Viewports

(1, 1)3lines

1inches

(1, 2)

1null

(1, 3) 3lines

2null

(2, 1)1null (2, 2) (2, 3) 1null

(3, 1)1null

1inches

(3, 2)

1null

(3, 3)

2null

1null

27 / 39

5. Viewports

Exercise 7

다음코드를실행해보세요!

labelvp <- function(name, col="grey", tcol="white", clipOff=TRUE) {seekViewport(name)if (clipOff)

pushViewport(viewport(clip="off"))grid.rect(gp=gpar(col=col, lwd=5))grid.rect(x=0, y=1, width=unit(1, "strwidth", name) + unit(2, "mm"),

height=unit(1, "lines"), just=c("left", "top"),gp=gpar(fill=col, col=NULL))

grid.text(name, x=unit(1, "mm"), y=unit(1, "npc") - unit(1, "mm"),just=c("left", "top"), gp=gpar(col=tcol))

upViewport(0)}

vplay <- grid.layout(3, 3,respect=rbind(c(0, 0, 0),

c(0, 1, 0),c(0, 0, 0)))

28 / 39

5. Viewports

pushViewport(viewport(width=0.95, height=0.95))grid.rect(gp=gpar(col="light grey"))pushViewport(viewport(layout=vplay))

pushViewport(viewport(layout.pos.col=2, name="col2"))upViewport()pushViewport(viewport(layout.pos.row=2, name="row2"))

labelvp("col2", "black")labelvp("row2")

29 / 39

5. Viewports

col2

row2

30 / 39

5. Viewports

Exercise 8

다음코드를실행해보세요!

gridfun <- function() {pushViewport(viewport(layout=grid.layout(1, 2)))pushViewport(viewport(layout.pos.col=1))grid.rect()grid.text("black")grid.text("&", x=1)popViewport()pushViewport(viewport(layout.pos.col=2, clip="on"))grid.rect(gp=gpar(fill="black"))grid.text("white", gp=gpar(col="white"))grid.text("&", x=0, gp=gpar(col="white"))popViewport(2)

}

31 / 39

5. Viewports

grid.rect(gp=gpar(col="grey"))w <- unit(1, "npc") - unit(15, "mm")x <- unit.c(unit(5, "mm"),

unit(5, "mm") + 1/3*w,unit(5, "mm") + 1/3*w + unit(5, "mm"),unit(1, "npc") - unit(5, "mm"))

y <- unit.c(unit(5, "mm"),unit(5, "mm") + 2/3*w,unit(5, "mm") + 2/3*w + unit(5, "mm"),unit(1, "npc") - unit(5, "mm"))

grid.segments(x, 0, x, 1,gp=gpar(col="grey", lty="dashed"))

grid.segments(0, y, 1, y,gp=gpar(col="grey", lty="dashed"))

pushViewport(viewport(

layout=grid.layout(5, 5,widths=unit(c(5, 1, 5, 2, 5),

c("mm", "null", "mm","null", "mm")),

heights=unit(c(5, 1, 5, 2, 5),c("mm", "null", "mm",

"null", "mm")))))pushViewport(viewport(layout.pos.col=2, layout.pos.row=2))gridfun()popViewport()pushViewport(viewport(layout.pos.col=4, layout.pos.row=4))gridfun()popViewport(2)

32 / 39

5. Viewports

black & white&

black & white&

33 / 39

5. Viewports

Exercise 9

Vignettes from package ‘grid’ packagehttps://stat.ethz.ch/R-manual/R-devel/library/grid/doc/index.html

Read “Writing grid Code” and try the codes in the vignette!

Read “Rotated Viewports” and try the codes in the vignette!

34 / 39

5. Viewports

35 / 39

5. Viewports

36 / 39

5. Viewports

Exercise 10

Read Vignettes of “grImport” packagehttps://cran.r-project.org/web/packages/grImport/index.html

Then try the code in “figure.R” in the zip file “special.zip” (아래의.eps그림파일을인터넷에서파운로드받아그림을그려본예)

37 / 39

5. Viewports

38 / 39

5. Viewports

39 / 39