[R] interaction plot with SE bar
Gerrit Eichner
Gerrit.Eichner at math.uni-giessen.de
Wed Dec 4 11:25:39 CET 2013
Hi, Kristi,
I have adapted the interaction plot example of function panel.average() of
the lattice package and modified the code of panel.average() to a new
function panel.loc_and_scale(). See below. (There might be a much simpler
solution, though.)
Remark on the side: I can't resist to point you to Tatsuki Koyama's very
instructive explanation on why plots of SE bars are not quite
recommendable:
http://biostat.mc.vanderbilt.edu/wiki/pub/Main/TatsukiRcode/Poster3.pdf
Nevertheless, here's the code (note that it also allows to graph other
location +/- scale bars if you provide fun1 and fun2 with respective
functions):
Regards -- Gerrit
panel.loc_and_scale <- function(x, y, fun1 = mean, fun2 = sd,
horizontal = TRUE,
lwd = add.line$lwd, lty = add.line$lty,
col, col.line = add.line$col, ...) {
x <- as.numeric(x)
y <- as.numeric(y)
add.line <- trellis.par.get( "add.line")
if (!missing(col)) {
if (missing(col.line))
col.line <- col
}
if (horizontal) {
vals <- unique(sort(y))
yy <- seq_along(vals)
xx1 <- xx2 <- numeric(length(yy))
for (i in yy) {
xx1[i] <- fun1(x[y == vals[i]])
xx2[i] <- fun2(x[y == vals[i]])
}
panel.arrows( x0 = xx1 - xx2, y0 = vals[yy],
x1 = xx1 + xx2, y1 = vals[yy],
code = 3, angle = 90, length = 0.1,
col = col.line, lty = lty, lwd = lwd)
panel.points( x = xx1, y = vals[yy], col = col, pch = 18)
}
else {
vals <- unique(sort(x))
xx <- seq_along(vals)
yy1 <- yy2 <- numeric(length(xx))
for (i in xx) {
yy1[i] <- fun1(y[x == vals[i]])
yy2[i] <- fun2(y[x == vals[i]])
}
panel.arrows( x0 = vals[xx], y0 = yy1 - yy2,
x1 = vals[xx], y1 = yy1 + yy2,
code = 3, angle = 90, length = 0.1,
col = col.line, lty = lty, lwd = lwd)
panel.points( x = vals[xx], y = yy1, col = col, pch = 18)
}
}
stripplot(yield ~ site, barley, groups = year, # fun1 = median, fun2 = IQR,
panel = function(x, y, groups, subscripts, ...) {
panel.grid(h = -1, v = 0)
panel.stripplot(x, y, ..., jitter.data = TRUE,
groups = groups, subscripts = subscripts)
panel.superpose(x, y, ..., panel.groups = panel.loc_and_scale,
groups = groups, subscripts = subscripts)
panel.superpose(x, y, ..., panel.groups = panel.average,
groups = groups, subscripts = subscripts)
},
auto.key = list(points = FALSE, lines = TRUE, columns = 2))
> Hi R user,
> I am just wondering how I can add Standard error bar in the interaction
> plot. I used the following code but I don't know how i can edit this to
> put a started error's bar on the mean.
> Would you give me some hints? or do other packages provide the
> information about plotting the SE bar for interaction.plot?
>
> require(graphics)
> with(OrchardSprays, {
> interaction.plot(treatment, rowpos, decrease)
> interaction.plot(rowpos, treatment, decrease, cex.axis = 0.8)
> ## order the rows by their mean effect
> rowpos <- factor(rowpos,
> levels = sort.list(tapply(decrease, rowpos, mean)))
> interaction.plot(rowpos, treatment, decrease, col = 2:9, lty = 1)
> })
>
> thanks
More information about the R-help
mailing list