56
Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Embed Size (px)

Citation preview

Page 1: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Introduction to Lattice GraphicsRichard Pugh4th December 2012

Page 2: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Agenda

• Overview of Lattice Functions• Creating basic graphics• Panelled Graphics• Grouped Data• Multiple Variables• Writing Panel Functions• Summary

Page 3: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

The Data We Will Use

• Something relevant and sector independent • London Tube Performance Data from the TFL

website• Excess Travel Hours by Line

http://data.london.gov.uk/datastore/package/tube-network-performance-data http://en.wikipedia.org/wiki/London_Underground

Page 4: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 5: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Overview of Lattice Graphics

Page 6: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Overview of Lattice Graphics

• One of the graphic systems of R (others include “Traditional” and “GGPlot”)

• An implementation of the S+ “Trellis” Graphics

• Written by Deepayan Sarkar, Fred Hutchinson Cancer Research Center

Page 7: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

List of Lattice Graphic Functions

Function Description Graph Typexyplot Scatter plot Bivariate

histogram Univariate histogram Univariatedensityplot Univariate density line plot Univariatebarchart Bar chart Univariatebwplot Box and whisker plot Bivariate

qq Normal QQ plot Univariatedotplot Label dot plot Bivariatecloud 3D scatter plot 3D

wireframe 3D surface plot 3Dsplom Scatter matrix plot Data Frame

parallel Multivariate parallel plot Data Frame

Page 8: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Key Function Arguments

Argument Descriptionx Plot definition, typically as a formula

data The data frame used for the graphicsubset Any subsets to be applied to the datapanel Function used to draw data in each “panel”groups Grouping variable for the plot

Type of graph Formula Y axis X axis Z axisUnivariate ~ Y Y - -Bivariate Y ~ X Y X -

3D Z ~ X*Y Y X ZData Frame ~ Data Data - -

Page 9: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Creating Basic Graphics

Page 10: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 11: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 12: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 13: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 14: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 15: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 16: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Panelled Graphics

Page 17: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Specifying Panels

• We can use the vertical pipe symbol “|” in order to specify “panels” to be plotted

• This allows us to create the graphics “by” one or more variables

Page 18: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 19: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 20: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 21: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 22: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Panel Variables

• During the plot creation, lattice builds a plot data frame containing the variables to plot

• By this time, any categorical “by” variables need to be factors

• So, either change them to characters or factors beforehand

Page 23: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 24: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 25: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Panel Ordering!

• If you have a factor variable, the ordering of the panels is in “graph” and not “table” order (!!)

• We can resolve using as.table = TRUE• Note that the converting of characters to

factors uses alphabetical ordering

Page 26: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 27: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 28: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 29: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Grouped Data

Page 30: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Grouped Data

• We can specify groupings within our data in order to plot these groupings separately

• By default, lattice will vary styling of the groups specified

• The key argument is the “groups” argument

Page 31: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Take care!

Page 32: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 33: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Controlling Styles

• Best done via the underlying lattice style templates

• Use of par.settings argument which takes a list of styles

• We usually use a basic template and change elements of it

• The show.settings function lets you see the current (or adapted) styles

Page 34: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 35: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 36: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 37: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 38: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Multiple Variables

Page 39: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Multiple Variables

• We can specify one or more X or Y axis variable in our lattice formula

• This can overwrite the “groups” input• The “outer” argument controls how the

multiple X/Y variables should be plotted

Page 40: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 41: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 42: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 43: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 44: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 45: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Writing Panel Functions

Page 46: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Panel Functions

• For each lattice graph, R performs the following actions:• Partitions the data• Draws the graph “outline” (i.e. the “panels”)• Passes the data for each panel into the “panel”

function

• We can overwrite this panel function and supply our own …

Page 47: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Panel Functions

• The default “panel” function for a lattice function is “panel.NameOfFunction”

• Let’s look at panel.xyplot …

Page 48: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Arguments for styling

Jitter & other less used bits

Let’s write our own panel function …….

Page 49: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 50: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 51: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 52: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Now what?

• Now we can insert other functions into a panel function which will then impact each individual plot panel

• We can call:• Lattice “low level functions” (lpoints, ltext, llines, lpolygon etc)

• Existing “panel.” functions that exist for this purpose

Page 53: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 54: Introduction to Lattice Graphics Richard Pugh 4 th December 2012
Page 55: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Summary

Page 56: Introduction to Lattice Graphics Richard Pugh 4 th December 2012

Summary

• This was just a quick overview of lattice graphics

• I hope it was useful• For more info, look in the Lattice book, find

examples online or contact Mango …