Grid Graphics The grid graphics system is provided - - PowerPoint PPT Presentation

grid graphics
SMART_READER_LITE
LIVE PREVIEW

Grid Graphics The grid graphics system is provided - - PowerPoint PPT Presentation

Grid Graphics The grid graphics system is provided by the grid package. Fundamental ideas 1 You start with nothing. 2 Anything goes. 3 You might end up with a plot.


slide-1
SLIDE 1

Grid Graphics

The grid graphics system is provided by the grid package.

Fundamental ideas

1 You start with nothing. 2 Anything goes. 3 You might end up with a plot.

  • Once upon a time ...

Date Amphipods

50 100 150 1998 2000 2002 2004

h

1998 2000 2002 2004

  • ● ●
  • ● ●
  • l

1998 2000 2002 2004

  • ● ●
  • ● ● ● ●
  • ● ● ●
  • ● ● ●
  • ● ● ● ● ● ● ●

m LN LS T B M

  • Graphical Primitives

You can draw the normal set of basic shapes, plus a couple of plot-specific ones. grid.arrows() grid.circle() grid.curve() grid.grill() grid.lines() grid.line.to() grid.move.to() grid.points() grid.polygon() grid.polyline() grid.rect() grid.segements() grid.text() grid.xaxis() grid.xspline() grid.yaxis() All functions are“low-level”and you start with the entire page to draw on (there are no plot regions or margins defined). If you want to start a new page, you must use grid.newpage().

slide-2
SLIDE 2

Graphical Parameters

You can control the appearance of output using graphical parameters. col fill alpha lty lwd lex lineend linejoin linemitre fontsize cex fontfamily fontface lineheight font

gpar() and gp

The gpar() function is used to create a graphical context and the gp argument is used to apply that context to output. grid.rect(gp=gpar(col="red", fill="white", lwd=3))

Units

Drawing can be positioned and sized relative to a number of different coordinate systems. npc, inches, cm, mm, points, char, lines.

The unit() function

The unit() function is used to associate a value with a coordinate system. unit(1, "mm") unit(1:4, "mm") unit(1:4, c("npc", "mm", "native", "lines"))

Working with units

It is possible to perform basic operations on units, such as addition, subtraction, calculating the maximum value, and so on.

Units

Unit example

grid.rect(x=unit(0.4, "npc"), y=unit(1, "inches"), width=stringWidth("very snug"), height=unit(1, "lines"), just=c("left", "bottom"))

very snug 1 inch 0.4npc

Viewports

Drawing is always relative to the current viewport, which starts

  • ff as the whole“page”

.

viewport() and pushViewport()

You can define new viewports using the viewport() function. You can make your viewport the current viewport using the pushViewport() function. It is possible to force drawing to be clipped to the viewport.

slide-3
SLIDE 3

pushViewport(viewport(width=0.8, height=0.5, angle=10, name="vp1")) grid.rect() 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() grid.text("top-left corner", x=unit(1, "mm"), y=unit(1, "npc") - unit(1, "mm"), just=c("left", "top"))

t

  • p

− l e f t c

  • r

n e r t

  • p

− l e f t c

  • r

n e r

Viewport Navigation

There are two ways to“leave”a viewport: one more destructive than the other. If you don’t destroy a viewport, you can revisit it later.

popViewport(), upViewport(), and downViewport()

If you“pop”a viewport, subsequent drawing occurs in the viewport that existed before you pushed the viewport. The same thing happens if you go“up”from a viewport, but in this case the viewport is left in place and you can go back“down”to it. Functions that draw complex plots may leave their viewports intact to assist in further annotation. upViewport() grid.text("bottom-right corner", y=unit(1, "mm"), x=unit(1, "npc") - unit(1, "mm"), just=c("right", "bottom")) downViewport("vp2") grid.rect(width=unit(1, "npc") + unit(2, "mm"), height=unit(1, "npc") + unit(2, "mm")) popViewport() # downViewport("vp2") gives error!

t

  • p

− l e f t c

  • r

n e r t

  • p

− l e f t c

  • r

n e r b

  • t

t

  • m

− r i g h t c

  • r

n e r

Layouts

A layout in grid is conceptually the same as a layout in traditional graphics, but grid layouts are more flexible and are applied in a different way.

grid.layout and layout.pos.col/row

A viewport can be given a layout using grid.layout; this divides up the viewport into rows and columns. Subsequent viewports can occupy cells in that layout using layout.pos.col/row. A layout in grid can make use of many different units to specify column widths and row heights.

slide-4
SLIDE 4

pushViewport(viewport(layout= grid.layout(3, 2, heights=unit(c(2, 0.5, 1), c("null", "lines", "null")), respect=TRUE))) pushViewport(viewport(layout.pos.col=1, layout.pos.row=1)) upViewport() pushViewport(viewport(layout.pos.row=3))

(1, 1) (3, 1:2)

Graphical Parameters in Viewports

As well as providing a geometric context for drawing, a viewport provides a graphical context for drawing.

The gp argument in viewport()

Graphical parameter settings in a viewport apply to all subsequent graphical primitives and viewports, unless explicitly overridden. Some graphical parameters, such as cex, lex, and alpha are “cumulative”(more accurately, multiplicative). pushViewport(viewport(gp=gpar(fill="grey"))) grid.rect(x=0.33, height=0.7, width=0.2) grid.rect(x=0.66, height=0.7, width=0.2, gp=gpar(fill="black")) popViewport()

Graphical Objects

Grid produces objects representing graphical output.

Graphical object names

All graphical primitives have a name argument that can be used to identify the object associated with the drawn output.

Modifying output

The grid.edit() function can be used to modify output by referring to output by the graphical object name. The grid.remove() function can be used to delete elements of a plot.

Positioning and sizing output based on other output

The grobX(), grobY(), grobWidth(), and grobHeight() functions can be used to specify the position or size of output based on existing output.

slide-5
SLIDE 5

Graphical Objects

A B C D

Grid and Lattice

Grid in Lattice

Grid functions, including viewport functions, can be used within a lattice panel function.

Lattice in Grid

Using print(xyplot(...), newpage=FALSE), you can draw a lattice plot within a grid viewport.

Notes Notes