Sunday 15 April 2012

R - Lattice xyplot - How do you add error bars to groups and summary lines? -



R - Lattice xyplot - How do you add error bars to groups and summary lines? -

i'm posting question because similar question here has not been answered until now.

i have been asked plot mean +/- sem of whole cohort of patients on xyplot() depicts values of patients. info used represents intraoperative cardiovascular findings patients undergoing surgery.

this data.frame called df

dput(df) structure(list(name = structure(c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 5l, 5l, 5l, 5l, 5l, 5l, 6l, 6l, 6l, 6l, 6l, 6l, 6l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 4l, 4l, 4l, 4l, 4l, 4l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l), .label = c("de", "js", "kg", "mk", "tg", "wt"), class = "factor"), time = structure(c(1l, 2l, 3l, 4l, 5l, 7l, 8l, 1l, 2l, 3l, 4l, 7l, 8l, 1l, 2l, 3l, 4l, 5l, 7l, 8l, 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 2l, 3l, 4l, 5l, 7l, 8l, 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l), .label = c("t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8"), class = "factor"), dobut = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 1l), .label = c("no", "yes" ), class = "factor"), dobutdose = c(na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, 4l, 6l, 8l, 8l, 8l, 8l, 8l, na, na, na, na, na, na, na, na, na, na, na, 5l, 5l, na), ci = c(1.4, 2.3, 1.3, 1.8, 2.1, 2, 2.1, 2.1, 2.3, 1.9, 1.6, 2, 2.4, 2.7, 2.6, 2.7, 2.6, 2.3, 2.4, 2.6, 0.9, 2.5, 2.1, 1.6, 1.5, 1.8, 2, 2, 1.9, 2.1, 2.3, 2, 2.4, 2.3, 2.6, 2.4, 2, 2.2, 1.6, 2.1, 2.5, 2.8), svo2 = c(57l, 65l, 47l, 45l, 51l, 60l, 56l, 70l, 85l, 75l, 79l, 82l, 73l, 77l, 78l, 73l, 71l, 73l, 80l, 74l, 41l, 66l, 51l, 51l, 49l, 54l, 68l, 48l, 80l, 70l, 71l, 69l, 74l, 79l, 77l, 77l, 75l, 74l, 70l, 79l, 80l, 79l), svri = c(4000l, 1983l, 4000l, 2444l, 1981l, 2120l, 2514l, 2971l, 2157l, 3747l, 4300l, 3200l, 2867l, 1778l, 1169l, 1215l, 1262l, 1461l, 1600l, 1692l, 4978l, 1760l, 2019l, 2650l, 2827l, 2356l, 1800l, 2840l, 2063l, 2248l, 1948l, 2160l, 1733l, 2296l, 2677l, 2100l, 2640l, 2655l, 3950l, 2210l, 2848l, 2543l), map = c(80l, 65l, 86l, 74l, 67l, 65l, 74l, 90l, 70l, 90l, 96l, 94l, 100l, 82l, 60l, 61l, 62l, 62l, 69l, 71l, 70l, 71l, 77l, 73l, 75l, 77l, 61l, 85l, 65l, 74l, 70l, 67l, 69l, 74l, 92l, 71l, 88l, 93l, 89l, 79l, 97l, 97l), cvp = c(10l, 8l, 21l, 19l, 15l, 12l, 8l, 12l, 8l, 11l, 10l, 14l, 14l, 22l, 22l, 20l, 21l, 20l, 21l, 16l, 14l, 16l, 24l, 20l, 22l, 24l, 16l, 14l, 16l, 15l, 14l, 13l, 17l, 8l, 5l, 8l, 22l, 20l, 20l, 21l, 8l, 8l), pap = c(23l, 22l, 36l, 36l, 34l, 32l, 22l, 33l, 28l, 36l, 36l, 40l, 37l, 37l, 40l, 35l, 35l, 34l, 38l, 36l, 45l, 43l, 55l, 49l, 52l, 54l, 43l, 47l, 27l, 25l, 23l, 22l, 28l, 21l, 20l, 25l, 33l, 33l, 38l, 35l, 33l, 29l), pcwp = c(15l, 11l, 28l, 26l, 23l, 21l, 11l, 26l, na, na, 25l, 25l, na, 27l, na, na, na, na, na, na, 30l, na, na, na, na, na, na, na, 19l, na, na, na, na, na, 16l, na, na, na, na, na, na, na)), .names = c("name", "time", "dobut", "dobutdose", "ci", "svo2", "svri", "map", "cvp", "pap", "pcwp" ), class = "data.frame", row.names = c(na, -42l))

now first xyplot made variable ci looks this

require(lattice) xyplot(ci~time, groups=name, data=df, ty=c("l", "p"), + ,xlab="measurement time point", ylab=expression("ci"~(l/min/m^"2")), main="cardiac index")

now able add together mean (black line) of whole cohort, doing following

xyplot(ci~time, groups=name, data=df, ty=c("l", "p"), panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.linejoin(x, y, horizontal = false,..., col="black", lty=1, lwd=4) } ,xlab="measurement time point", ylab=expression("ci"~(l/min/m^"2")), main="cardiac index")

now i'd add together +/- se mean line above/below mean, can find how this.

what can using latticeextra bundle add together loess line +/- se, below, that's not right mathematical function i'm looking for. i've left mean line in there illustrate difference between two.

require(latticeextra) xyplot(ci~time, groups=name, data=df, ty=c("l", "p"), + panel = function(x, y, ...) { + panel.xyplot(x, y, ...) + panel.linejoin(x, y, horizontal = false,..., col="black", lty=1, lwd=4) + panel.smoother(x,y,se=true, col.se="grey") + } + ,xlab="measurement time point", ylab=expression("ci"~(l/min/m^"2")), main="cardiac index")

i have performed extensive search through , internet, haven't been able find right function this.

help much appreciated! thanks.

you create own panel function plot +/- sd region. example

#new panel function panel.se <- function(x, y, col.se=plot.line$col, alpha.se=.25, ...) { plot.line <- trellis.par.get("plot.line") xs <- if(is.factor(x)) { factor(c(levels(x) , rev(levels(x))), levels=levels(x)) } else { xx <- sort(unique(x)) c(xx, rev(xx)) } means <- tapply(y,x, mean, na.rm=t) vars <- tapply(y,x, var, na.rm=t) ns <- tapply(!is.na(y),x, sum) ses <- sqrt(vars/ns) panel.polygon(xs, c(means+ses, rev(means-ses)), col=col.se, alpha=alpha.se) }

and can utilize like

#include new panel function xyplot(ci~time, groups=name, data=df, ty=c("l", "p"), panel = function(x, y, ...) { panel.se(x,y, col.se="grey") panel.xyplot(x, y, ...) panel.linejoin(x, y, horizontal = false,..., col="black", lty=1, lwd=4) } ,xlab="measurement time point", ylab=expression("ci"~(l/min/m^"2")), main="cardiac index")

which results in

r lattice standard-error

No comments:

Post a Comment