panel f u nctions
play

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() ,


  1. 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

  2. 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() , etc . optional arg u ments for common v ariants B u t not possible to anticipate all v ariants Need some s y stem to design ne w v is u ali z ations DATA VISUALIZATION WITH LATTICE IN R

  3. T w o standard approaches Declarati v e : Yo u pro v ide set of speci � cations S y stem decides ho w to meet req u irements Approach u sed b y ggplot2 Proced u ral or algorithmic : Yo u pro v ide set of instr u ctions E x ec u ted seq u entiall y Used b y base R graphics and lattice DATA VISUALIZATION WITH LATTICE IN R

  4. C u stom displa y s in base graphics E x ample : Reg u lar densit y histogram , w ith Kernel densit y estimate o v erlaid DATA VISUALIZATION WITH LATTICE IN R

  5. C u stom displa y s in base graphics DATA VISUALIZATION WITH LATTICE IN R

  6. C u stom displa y s in base graphics Start w ith histogram data(USCancerRates, package = "latticeExtra") log.rate.male <- log(USCancerRates$rate.male) hist(log.rate.male, freq = FALSE) Add kernel densit y estimate lines(density(log.rate.male, na.rm = TRUE), col = "blue") DATA VISUALIZATION WITH LATTICE IN R

  7. C u stom displa y s 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) DATA VISUALIZATION WITH LATTICE IN R

  8. C u stom displa y s in lattice DATA VISUALIZATION WITH LATTICE IN R

  9. The panel f u nction panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) } Data is accessed thro u gh placeholder arg u ment x Same panel f u nction u sed for both panels Can be re -u sed , not speci � c to dataset DATA VISUALIZATION WITH LATTICE IN R

  10. The panel f u nction panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) } Base graphics f u nction lines() and curve() Replaced b y panel.lines() and panel.curve() All common f u nctions ha v e la � ice analog u es DATA VISUALIZATION WITH LATTICE IN R

  11. The panel f u nction panel.histdens <- function(x, ...) { panel.histogram(x, ...) panel.lines(density(x, na.rm = TRUE)) } Defa u lt panel displa y m u st be incl u ded if needed Sho u ld incl u de the ... arg u ment Needed for optional arg u ments like type = "density" DATA VISUALIZATION WITH LATTICE IN R

  12. Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R

  13. Prepanel f u nctions to control limits DATA VISU AL IZATION W ITH L ATTIC E IN R Deepa y an Sarkar Associate Professor , Indian Statistical Instit u te

  14. Panel limits 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

  15. 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)) DATA VISUALIZATION WITH LATTICE IN R

  16. Prepanel f u nction prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) } DATA VISUALIZATION WITH LATTICE IN R

  17. Prepanel f u nction 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

  18. A more rob u st prepanel f u nction prepanel.histdens.1 <- function(x, ...) { d <- density(x, na.rm = TRUE) list(ylim = c(0, max(d$y))) } DATA VISUALIZATION WITH LATTICE IN R

  19. A more rob u st prepanel f u nction 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

  20. A more rob u st prepanel f u nction histogram(~ rate.male + rate.female, USCancerRates, scales = list(x = list(log = 10)), xlab = "Rate", type = "density panel = panel.histdens, prepanel = prepanel.histdens.2) DATA VISUALIZATION WITH LATTICE IN R

  21. Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R

  22. Optional arg u ments of defa u lt 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

  23. Optional arg u ments Some are part of the la � ice frame w ork : xlab , ylab , main : controls labels layout , between : controls m u lti - panel la y o u t scales : controls a x is comp u tation and displa y Others are f u nction - speci � c : 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

  24. Handling of optional arg u ments High - le v el f u nctions handle arg u ments of the � rst t y pe ( frame w ork ) Remaining arg u ments are passed to panel f u nction Can be u sed to c u stomi z e displa y w itho u t u sing e x plicit panel f u nction grid = TRUE is act u all y an arg u ment of panel.xyplot() in xyplot(y ~ x, data, grid = TRUE) DATA VISUALIZATION WITH LATTICE IN R

  25. Usef u l arg u ments of panel .xy plot () grid : Adds a reference grid u sing panel.grid() abline : Adds a reference line u sing panel.abline() jitter.x , jitter.y : Ji � ers the data u sing jitter() u sef u l w hen points o v erlap factor , amount : Controls amo u nt of ji � ering DATA VISUALIZATION WITH LATTICE IN R

  26. Usef u l arg u ments of panel .xy plot () The type arg u ment pro v ides a lot of � e x ibilit y. Usef u l v al u es are : "p" : plot (x,y) data as points "l" : join (x,y) data b y lines "r" : add linear regression line ( panel.lmline() ) "smooth" : add LOESS smooth ( panel.loess() ) "a" : join a v erage y v al u es for each u niq u e x v al u e ( panel.average() ) M u ltiple type v al u es can be speci � ed as a v ector DATA VISUALIZATION WITH LATTICE IN R

  27. E x ample : USRegionalMortalit y data Similar to USMortality data Death rate d u e to leading ca u ses 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

  28. E x ample : 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))) DATA VISUALIZATION WITH LATTICE IN R

  29. E x ample : interaction plot Optional arg u ments u sed : grid = list(h = -1, v = 0) : dra w s hori z ontal reference lines b u t omits v ertical ones type = c("p", "a") : dra w s points as w ell as lines joining a v erage y-v al u es jitter.x = TRUE : ji � ers the x-v al u es to address o v er - plo � ing DATA VISUALIZATION WITH LATTICE IN R

  30. Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R

Recommend


More recommend