Top Banner
Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012
42

Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Dec 18, 2015

Download

Documents

Dinah Watkins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Using Lattice to Plot Grouped DataRichard Pugh4th December 2012

Page 2: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Agenda

• What are Lattice Graphics?• Panel Functions• Grouped data• The mechanics of grouped plots• Plotting more than 1 group• Summary

Page 3: Using Lattice to Plot Grouped Data 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: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 5: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

What are Lattice Graphics?

Page 6: Using Lattice to Plot Grouped Data 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: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 8: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 9: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Panel Functions

Page 10: Using Lattice to Plot Grouped Data 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 11: Using Lattice to Plot Grouped Data 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 12: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Arguments for styling

Jitter & other less used bits

Let’s write our own panel function …….

Page 13: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 14: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 15: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 16: Using Lattice to Plot Grouped Data 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 17: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 18: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 19: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Grouped Data

Page 20: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Grouped Data

• Many datasets will have inherit groupings• Examples may include:

• “Stock” for finance data• “Subject” for pharma data

• We *always* want to take this into account when working with graphics

Page 21: Using Lattice to Plot Grouped Data 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 22: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 23: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 24: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

The Mechanics of Grouped Data

Page 25: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 26: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 27: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 28: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Plotting more than 1 group of data

Page 29: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Plotting more than 1 group of data• Something I want to a do a lot• Examples:

• Group by sector when plotting stock data• Group by dose group when plotting subject data

Grouping Variable “Base” Grouping Variable

Page 30: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 31: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 32: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 33: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 34: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 35: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 36: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 37: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 38: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 39: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.
Page 40: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Summary

Page 41: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Summary

• Lattice is a great framework to use when your data is highly structured

• We can use the “panel.groups” structure in order to ensure a “base” grouping variable is always taken into account

• Slides, Data and Scripts will be at LondonR.org soon

Page 42: Using Lattice to Plot Grouped Data Richard Pugh 4 th December 2012.

Questions?