Panel f u nctions DATA VISU AL IZATION W ITH L ATTIC E IN R - - PowerPoint PPT Presentation

panel f u nctions
SMART_READER_LITE
LIVE PREVIEW

Panel f u nctions DATA VISU AL IZATION W ITH L ATTIC E IN R - - PowerPoint PPT Presentation

Panel f u nctions DATA VISU AL IZATION W ITH L ATTIC E IN R Deepa y an Sarkar Associate Professor , Indian Statistical Instit u te Introd u ction E x amples so far u se b u ilt - in high - le v el f u nctions s u ch as histogram() , xyplot() ,


slide-1
SLIDE 1

Panel functions

DATA VISU AL IZATION W ITH L ATTIC E IN R

Deepayan Sarkar

Associate Professor, Indian Statistical Institute

slide-2
SLIDE 2

DATA VISUALIZATION WITH LATTICE IN R

Introduction

Examples so far use built-in high-level functions such as histogram() , xyplot() , etc.

  • ptional arguments for common variants

But not possible to anticipate all variants Need some system to design new visualizations

slide-3
SLIDE 3

DATA VISUALIZATION WITH LATTICE IN R

Two standard approaches

Declarative: You provide set of specications System decides how to meet requirements Approach used by ggplot2 Procedural or algorithmic: You provide set of instructions Executed sequentially Used by base R graphics and lattice

slide-4
SLIDE 4

DATA VISUALIZATION WITH LATTICE IN R

Custom displays in base graphics

Example: Regular density histogram, with Kernel density estimate overlaid

slide-5
SLIDE 5

DATA VISUALIZATION WITH LATTICE IN R

Custom displays in base graphics

slide-6
SLIDE 6

DATA VISUALIZATION WITH LATTICE IN R

Custom displays in base graphics

Start with histogram data(USCancerRates, package = "latticeExtra") log.rate.male <- log(USCancerRates$rate.male) hist(log.rate.male, freq = FALSE) Add kernel density estimate lines(density(log.rate.male, na.rm = TRUE), col = "blue")

slide-7
SLIDE 7

DATA VISUALIZATION WITH LATTICE IN R

Custom displays in lattice

panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) } histogram(~ rate.male + rate.female, USCancerRates, type = "density", layout = c(1, 2), xlab = "Rate (per 100,000)", scales = list(x = list(log = TRUE, equispaced.log = FALSE)), panel = panel.histdens)

slide-8
SLIDE 8

DATA VISUALIZATION WITH LATTICE IN R

Custom displays in lattice

slide-9
SLIDE 9

DATA VISUALIZATION WITH LATTICE IN R

The panel function

panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) }

Data is accessed through placeholder argument x Same panel function used for both panels Can be re-used, not specic to dataset

slide-10
SLIDE 10

DATA VISUALIZATION WITH LATTICE IN R

The panel function

panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) }

Base graphics function lines() and curve() Replaced by panel.lines() and panel.curve() All common functions have laice analogues

slide-11
SLIDE 11

DATA VISUALIZATION WITH LATTICE IN R

The panel function

panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) }

Default panel display must be included if needed Should include the ... argument Needed for optional arguments like type = "density"

slide-12
SLIDE 12

Let's practice!

DATA VISU AL IZATION W ITH L ATTIC E IN R

slide-13
SLIDE 13

Prepanel functions to control limits

DATA VISU AL IZATION W ITH L ATTIC E IN R

Deepayan Sarkar

Associate Professor, Indian Statistical Institute

slide-14
SLIDE 14

DATA VISUALIZATION WITH LATTICE IN R

Panel limits

histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", panel = panel.histdens, type = "density")

slide-15
SLIDE 15

DATA VISUALIZATION WITH LATTICE IN R

Panel limits

histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", panel = panel.histdens, type = "density", ylim = c(NA, 8.5))

slide-16
SLIDE 16

DATA VISUALIZATION WITH LATTICE IN R

Prepanel function

prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) }

slide-17
SLIDE 17

DATA VISUALIZATION WITH LATTICE IN R

Prepanel function

histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", type = "density panel = panel.histdens, prepanel = prepanel.histdens.1)

slide-18
SLIDE 18

DATA VISUALIZATION WITH LATTICE IN R

A more robust prepanel function

prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) }

slide-19
SLIDE 19

DATA VISUALIZATION WITH LATTICE IN R

A more robust prepanel function

prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) } prepanel.histdens.2 <- function(x, ...) { h <- prepanel.default.histogram(x, ...) d <- density(x, na.rm = TRUE) list(xlim = quantile(x, c(0.005, 0.995), na.rm = TRUE), ylim = c(0, max(d$y, # kernel density estimate h$ylim[2]))) # histogram (default }

slide-20
SLIDE 20

DATA VISUALIZATION WITH LATTICE IN R

A more robust prepanel function

histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", type = "density panel = panel.histdens, prepanel = prepanel.histdens.2)

slide-21
SLIDE 21

Let's practice!

DATA VISU AL IZATION W ITH L ATTIC E IN R

slide-22
SLIDE 22

Optional arguments

  • f default panel

functions

DATA VISU AL IZATION W ITH L ATTIC E IN R

Deepayan Sarkar

Associate Professor, Indian Statistical Institute

slide-23
SLIDE 23

DATA VISUALIZATION WITH LATTICE IN R

Optional arguments

Some are part of the laice framework:

xlab , ylab , main : controls labels layout , between : controls multi-panel layout scales : controls axis computation and display

Others are function-specic:

nint , type in histogram() plot.points , ref in densityplot() grid , abline in xyplot()

graphical parameters: col , lwd , cex , pch , etc.

slide-24
SLIDE 24

DATA VISUALIZATION WITH LATTICE IN R

Handling of optional arguments

High-level functions handle arguments of the rst type (framework) Remaining arguments are passed to panel function Can be used to customize display without using explicit panel function

grid = TRUE is actually an argument of panel.xyplot() in xyplot(y ~ x, data, grid = TRUE)

slide-25
SLIDE 25

DATA VISUALIZATION WITH LATTICE IN R

Useful arguments of panel.xyplot()

grid : Adds a reference grid using panel.grid() abline : Adds a reference line using panel.abline() jitter.x , jitter.y : Jiers the data using jitter()

useful when points overlap

factor , amount : Controls amount of jiering

slide-26
SLIDE 26

DATA VISUALIZATION WITH LATTICE IN R

Useful arguments of panel.xyplot()

The type argument provides a lot of exibility. Useful values are:

"p" : plot (x,y) data as points "l" : join (x,y) data by lines "r" : add linear regression line ( panel.lmline() ) "smooth" : add LOESS smooth ( panel.loess() ) "a" : join average y values for each unique x value ( panel.average() )

Multiple type values can be specied as a vector

slide-27
SLIDE 27

DATA VISUALIZATION WITH LATTICE IN R

Example: USRegionalMortality data

Similar to USMortality data Death rate due to leading causes for 10 separate HHS regions

USRegionalMortality <- dplyr::mutate(USRegionalMortality, Cause = reorder(Cause, Rate)) str(USRegionalMortality) 'data.frame': 400 obs. of 6 variables: $ Region: Factor w/ 10 levels "HHS Region 01",..: 1 1 1 1 2 2 2 2 $ Status: Factor w/ 2 levels "Rural","Urban": 2 1 2 1 2 1 2 1 2 1 $ Sex : Factor w/ 2 levels "Female","Male": 2 2 1 1 2 2 1 1 2 2 $ Cause : Factor w/ 10 levels "Nephritis","Suicide",..: 10 10 10 . $ Rate : num 188 199 115 124 227 ... $ SE : num 1 2.6 0.6 1.7 0.8 3.3 0.5 2.3 0.8 2 ...

slide-28
SLIDE 28

DATA VISUALIZATION WITH LATTICE IN R

Example: interaction plot

xyplot(log2(Rate) ~ Cause, data = USRegionalMortality, groups = Sex grid = list(h = -1, v = 0), type = c("p", "a"), jitter.x = TRUE auto.key = list(lines = TRUE, points = TRUE, columns = 2), scales = list(x = list(rot = 45)))

slide-29
SLIDE 29

DATA VISUALIZATION WITH LATTICE IN R

Example: interaction plot

Optional arguments used:

grid = list(h = -1, v = 0) :

draws horizontal reference lines but omits vertical ones

type = c("p", "a") :

draws points as well as lines joining average y-values

jitter.x = TRUE :

jiers the x-values to address over-ploing

slide-30
SLIDE 30

Let's practice!

DATA VISU AL IZATION W ITH L ATTIC E IN R