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

August 15, 2011
By

(This article was first published on Why? » R, and kindly contributed to R-bloggers)

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.

The last few tutorials I’ve attended at conferences have been a waste of time – poorly prepared presenters who didn’t know their audience. However, this tutorial was excellent. Paul kept the audience with him through “simple” exercises. Glad I attended.

Please note that the notes/talks section of this post is merely my notes on the presentation. I may have made mistakes: these notes are not guaranteed to be correct. Unless explicitly stated, they represent neither my opinions nor the opinions of my employers. Any errors you can assume to be mine and not the speaker’s. I’m happy to correct any errors you may spot – just let me know! The above paragraph was stolen from Allyson Lister who makes excellent notes when she attend conferences.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: , , , , , ,