Panel functions
DATA VISU AL IZATION W ITH L ATTIC E IN R
Deepayan Sarkar
Associate Professor, Indian Statistical Institute
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() ,
DATA VISU AL IZATION W ITH L ATTIC E IN R
Deepayan Sarkar
Associate Professor, Indian Statistical Institute
DATA VISUALIZATION WITH LATTICE IN R
Examples so far use built-in high-level functions such as histogram() , xyplot() , etc.
But not possible to anticipate all variants Need some system to design new visualizations
DATA VISUALIZATION WITH LATTICE IN R
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
DATA VISUALIZATION WITH LATTICE IN R
Example: Regular density histogram, with Kernel density estimate overlaid
DATA VISUALIZATION WITH LATTICE IN R
DATA VISUALIZATION WITH LATTICE IN R
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")
DATA VISUALIZATION WITH LATTICE IN R
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)
DATA VISUALIZATION WITH LATTICE IN R
DATA VISUALIZATION WITH LATTICE IN R
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
DATA VISUALIZATION WITH LATTICE IN R
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
DATA VISUALIZATION WITH LATTICE IN R
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"
DATA VISU AL IZATION W ITH L ATTIC E IN R
DATA VISU AL IZATION W ITH L ATTIC E IN R
Deepayan Sarkar
Associate Professor, Indian Statistical Institute
DATA VISUALIZATION WITH LATTICE IN R
histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", panel = panel.histdens, type = "density")
DATA VISUALIZATION WITH LATTICE IN R
histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", panel = panel.histdens, type = "density", ylim = c(NA, 8.5))
DATA VISUALIZATION WITH LATTICE IN R
prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) }
DATA VISUALIZATION WITH LATTICE IN R
histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", type = "density panel = panel.histdens, prepanel = prepanel.histdens.1)
DATA VISUALIZATION WITH LATTICE IN R
prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) }
DATA VISUALIZATION WITH LATTICE IN R
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 }
DATA VISUALIZATION WITH LATTICE IN R
histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", type = "density panel = panel.histdens, prepanel = prepanel.histdens.2)
DATA VISU AL IZATION W ITH L ATTIC E IN R
DATA VISU AL IZATION W ITH L ATTIC E IN R
Deepayan Sarkar
Associate Professor, Indian Statistical Institute
DATA VISUALIZATION WITH LATTICE IN R
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.
DATA VISUALIZATION WITH LATTICE IN R
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)
DATA VISUALIZATION WITH LATTICE IN R
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
DATA VISUALIZATION WITH LATTICE IN R
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
DATA VISUALIZATION WITH LATTICE IN R
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 ...
DATA VISUALIZATION WITH LATTICE IN R
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)))
DATA VISUALIZATION WITH LATTICE IN R
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
DATA VISU AL IZATION W ITH L ATTIC E IN R