Conditioning DATA VISU AL IZATION W ITH L ATTIC E IN R Deepa y an Sarkar Associate Professor , Indian Statistical Instit u te
Comparison Goal : identif y so u rces of v ariabilit y Compare di � erent s u bgro u ps of data DATA VISUALIZATION WITH LATTICE IN R
Small m u ltiples ( conditioning / faceting ) Goal : identif y so u rces of v ariabilit y Comparing di � erent s u bgro u ps of data DATA VISUALIZATION WITH LATTICE IN R
Conditioning in lattice data(USCancerRates, package = "latticeExtra") xyplot(rate.female ~ rate.male | state, data = USCancerRate grid = TRUE, abline = c(0, 1)) DATA VISUALIZATION WITH LATTICE IN R
Another conditioned plot histogram(~ rate.male | state, USCancerRates, nint = 15) DATA VISUALIZATION WITH LATTICE IN R
Another w a y to condition histogram(~ rate.male + rate.female, data = USCancerRates, nint = 30, outer = TRUE, layout = c(1, 2), xlab = "Rate (per 100,000)") DATA VISUALIZATION WITH LATTICE IN R
Ne w arg u ments histogram(~ rate.male + rate.female, USCancerRates, nint = 30, outer = TRUE, layout = c(1, 2), xlab = "Rate (per 100,000)") outer = TRUE | FALSE Controls ho w v ariables separated b y + are interpreted More details in e x ercises layout = c(ncol, nrow, npages) Controls arrangement of panels in a matri x- t y pe la y o u t DATA VISUALIZATION WITH LATTICE IN R
S u mmar y: Conditioning u sing form u la E x plicit : y ~ x | g or ~ x | g More than one conditioning v ariable also allo w ed : ~ x | g1 + g2 Implicit : ~ x1 + x2 DATA VISUALIZATION WITH LATTICE IN R
Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R
Data s u mmar y and transformation , gro u ping DATA VISU AL IZATION W ITH L ATTIC E IN R Deepa y an Sarkar Associate Professor , Indian Statistical Instit u te
Data s u mmar y S u mmari z ing or transforming data can be u sef u l Especiall y for reporting We ha v e seen plots of death rates per co u nt y Can also s u mmari z e to get rates per state Usef u l f u nction : tapply(X, INDEX, FUN, ...) Di v ide n u meric data X for each u niq u e v al u e of INDEX Appl y f u nction FUN for each s u bset of X DATA VISUALIZATION WITH LATTICE IN R
Data s u mmar y USCancerRates.state <- with(USCancerRates, { rmale <- tapply(rate.male, state, median, na.rm= TRUE) rfemale <- tapply(rate.female, state, median, na.rm= TRUE) data.frame(Rate = c(rmale, rfemale), State = rep(names(rmale), 2), Gender = rep(c("Male", "Female"), each = length(rmale))) }) USCancerRates.state <- dplyr::mutate(USCancerRates.state, State = reorder(State, Rate)) DATA VISUALIZATION WITH LATTICE IN R
USCancerRates.state Rate State Gender 1 286.00 Alabama Male 2 237.95 Alaska Male 3 209.30 Arizona Male 4 284.10 Arkansas Male 5 221.30 California Male 6 204.40 Colorado Male 7 228.55 Connecticut Male ... 95 164.85 Washington Female 96 183.10 West Virginia Female 97 158.35 Wisconsin Female 98 160.40 Wyoming Female DATA VISUALIZATION WITH LATTICE IN R
Plotting s u mmar y data xyplot(State ~ Rate | Gender, USCancerRates.state, grid = TRUE) DATA VISUALIZATION WITH LATTICE IN R
Plotting s u mmar y data xyplot(State ~ Rate, data = USCancerRates.state, grid = TRUE, groups = Gender) DATA VISUALIZATION WITH LATTICE IN R
Ne w concept : gro u ps Ne w arg u ment : groups De � nes s u bgro u ps plo � ed w ithin same panel Di � erentiated u sing color ( or other graphical parameters ) E x pression e v al u ated in data DATA VISUALIZATION WITH LATTICE IN R
Gro u ps and legends Gro u ps are sho w n u sing di � erent colors Usef u l to ha v e legend matching colors and gro u ps Not present b y defa u lt Use auto.key = TRUE DATA VISUALIZATION WITH LATTICE IN R
Gro u ps and legends xyplot(State ~ Rate, data = USCancerRates.state, grid = TRUE, groups = Gender, auto.key = TRUE) DATA VISUALIZATION WITH LATTICE IN R
Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R
Incorporating e x ternal data so u rces DATA VISU AL IZATION W ITH L ATTIC E IN R Deepa y an Sarkar Associate Professor , Indian Statistical Instit u te
Comparison diffic u lt w ith too man y panels DATA VISUALIZATION WITH LATTICE IN R
Possible sol u tions Di v ide u p the panels into m u ltiple pages Aggregate panels into fe w er gro u ps DATA VISUALIZATION WITH LATTICE IN R
Aggregating states Premise : neighbo u ring states sho u ld sho w similar beha v ior Can u se o u r o w n gro u ping , or u se some pre - de � ned gro u ping DATA VISUALIZATION WITH LATTICE IN R
E x ample : state . di v ision Use the b u ilt - in dataset state.division data.frame(state.name, state.division) state.name state.division 1 Alabama East South Central 2 Alaska Pacific 3 Arizona Mountain 4 Arkansas West South Central 5 California Pacific 6 Colorado Mountain 7 Connecticut New England 8 Delaware South Atlantic 9 Florida South Atlantic ... DATA VISUALIZATION WITH LATTICE IN R
Adding di v ision for each co u nt y index <- match(USCancerRates$state, state.name) USCancerRates$division <- state.division[index] USCancerRates <- dplyr::mutate(USCancerRates, division.ordered = reorder(division, rate.male + rate.female, median, na.rm = TRUE)) DATA VISUALIZATION WITH LATTICE IN R
Using the ne w v ariable densityplot(~ rate.male + rate.female | division.ordered, data = USCancerRates, outer = TRUE, plot.points = FALSE, ref = TRUE) DATA VISUALIZATION WITH LATTICE IN R
A better la y o u t densityplot(~ rate.male + rate.female | division.ordered, data = USCancerRates, outer = TRUE, plot.points = FALSE, layout = c(3, 6)) DATA VISUALIZATION WITH LATTICE IN R
Adding separation bet w een ro w s and col u mns densityplot(~ rate.male + rate.female | division.ordered, USCancerRates, outer = TRUE, plot.points = FALSE, layout = c(3, 6), between = list(y = c(0,0,1,0,0))) DATA VISUALIZATION WITH LATTICE IN R
Gro u ping instead of conditioning densityplot(~ rate.male + rate.female | division.ordered, USCancerRates, outer = FALSE, plot.points = FALSE, ref = TRUE, layout = c(3, 3), auto.key = TRUE) DATA VISUALIZATION WITH LATTICE IN R
Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R
The " trellis " object DATA VISU AL IZATION W ITH L ATTIC E IN R Deepa y an Sarkar Associate Professor , Indian Statistical Instit u te
Plots as objects # Create trellis object tplot <- densityplot(~ rate.male + rate.female | division.ordered, data = USCancerRates, outer = TRUE, plot.points = FALSE, as.table = TRUE) class(tplot) "trellis" DATA VISUALIZATION WITH LATTICE IN R
summary(tplot) Call: densityplot(~rate.male + rate.female | division.ordered, data = USCancerRates, outer = TRUE, plot.points = FALSE, as.table = TRUE) Number of observations: division.ordered rate.male rate.female Mountain 254 254 West North Central 582 582 Pacific 151 151 Middle Atlantic 150 150 East North Central 437 437 New England 67 67 West South Central 451 451 South Atlantic 587 587 East South Central 362 362 DATA VISUALIZATION WITH LATTICE IN R
Dra w ing " trellis " objects tplot DATA VISUALIZATION WITH LATTICE IN R
Updating " trellis " objects update(tplot, layout = c(6, 3)) DATA VISUALIZATION WITH LATTICE IN R
" trellis " objects as arra y s dimnames(tplot) $division.ordered [1] "Mountain" "West North Central" [3] "Pacific" "Middle Atlantic" [5] "East North Central" "New England" [7] "West South Central" "South Atlantic" [9] "East South Central" [[2]] [1] "rate.male" "rate.female" DATA VISUALIZATION WITH LATTICE IN R
" trellis " objects as arra y s Transposed tplot # Transpose tplot # like a matrix t(tplot) Original tplot DATA VISUALIZATION WITH LATTICE IN R
Let ' s practice ! DATA VISU AL IZATION W ITH L ATTIC E IN R
Recommend
More recommend