# Why?

## August 15, 2011

### Paul Murrell – Introduction to Grid graphics (useR! 2011)

Filed under: Conferences, R, useR! 2011 — Tags: , , , , , — csgillespie @ 8:06 pm

Typically, I’m very bad at taking notes in conference. This time around, I intend to make notes for each some of the talks I attend at this year’s useR! 2011 conference. Below are my notes that I made during this afternoon’s tutorial. Note: these are just notes I made and aren’t meant to be a full introduction to Grid graphics. If you are interested further in grid graphics I recommend that you visit Paul’s website (look at his talks near the bottom of the page) or buy his book.

## Introduction to Grid Graphics: Paul Murrell

Grid provides tools to draw and arrange basic shapes. It is a very low level graphics package. Grid provides functions that allow the plot canvas to be accessed programmatically. Viewports create a context for drawing. The basic shapes available are polygons, curves, raster images, and data symbols.

ggplot2 and lattice use grid graphics.

### Shapes

The grid coordinate systems consist of a value and one of four systems:

• npc: Normalised parent coordinates;
• native: relative to current x-, y-scale;
• in or cm;
• lines: lines of text.

Every basic shape has a `gp` argument. Similar specifications to base graphics, i.e. col, lwd, lty, cex, fill, etc.

### Viewports

A viewport describes a rectangular region on the page. Viewports have both a location and size. The viewport function creates a rectangular region on the page. Drawing occurs via the push viewport function:

```pushViewport(vp) grid.rect(gp=gpar(fill="grey")) ```
Crucially, viewports are relative to their current viewports. For example: `pushViewport(vp); pushViewport(vp)` is a viewport within a viewport. To move back up the viewport tree, use: `popViewport(#no_of_viewports)`. However, this “destroys” the viewport. Usually better to use upViewport and downViewport.

### Layouts

A layout divides viewports into rows and columns. Use upViewport/downViewport to avoid destroying existing viewports.

### Grobs

A grob is a grapical object that contains a description of the shape. Grobs have names. `grid.ls()` lists grobs, while `grid.edit()` accesses and edits the grob.

### ggplot2 and lattice

ggplot2 can be manipulated via grid. However, this can be tricky. lattice plots are a bit easier to manipulate.