[R] Condition layer across panels in lattice

Duncan Mackay dulcalma at bigpond.com
Tue Feb 16 05:15:42 CET 2016


Sorry forgot to reply to list.

In addition (untested) modifying demo(lattice::intervals) to suit may help

Duncan 

-----Original Message-----
From: Duncan Mackay [mailto:dulcalma at bigpond.com] 
Sent: Tuesday, 16 February 2016 12:12
To: 'Jeff Stevens'
Subject: RE: [R] Condition layer across panels in lattice

Hi Jeff

Try this

mypanel <-
function(x, y, ...,  groups, type, lty){

    pnl = panel.number()

    panel.xyplot(x, y, ..., groups = groups, type = type, lty = lty)

    panel.average(x, y, horizontal = FALSE,
                  col = "black", lwd = 3)

   # print(summary_data[,"lci"][summary_data$cond2==LETTERS[1:2][pnl]])
   # print(summary_data[,"uci"][summary_data$cond2==LETTERS[1:2][pnl]])

    panel.segments(x0 =
summary_data[,"cond1"][summary_data$cond2==LETTERS[1:2][pnl]],
                   y0 =
summary_data[,"lci"][summary_data$cond2==LETTERS[1:2][pnl]],
                   x1 =
summary_data[,"cond1"][summary_data$cond2==LETTERS[1:2][pnl]],
                   y1 =
summary_data[,"uci"][summary_data$cond2==LETTERS[1:2][pnl]])
    panel.segments(x0 =
summary_data[,"cond1"][summary_data$cond2==LETTERS[1:2][pnl]],
                   y0 =
summary_data[,"lci"][summary_data$cond2==LETTERS[1:2][pnl]],
                   x1 =
summary_data[,"cond1"][summary_data$cond2==LETTERS[1:2][pnl]],
                   y1 =
summary_data[,"uci"][summary_data$cond2==LETTERS[1:2][pnl]])

}

    stripplot(response ~ cond1 | cond2, data = raw_data,
              groups = subject,
              #lci = summary_data$lci,
              #uci = summary_data$uci,
              #scond1 = summary_data$cond1,
              type = "b",lty = 2,
              panel = mypanel

    )

It's a bit kludgy - I need some coffee to sort out the subscripting.

I used the panel.number to group the panels because the grouping factor for
the raw_data (subjects) is different from the summary_data (cond2)

I tried to keep it within lattice itself without having to go to
latticeExtra:::layer as you had. layering can have adverse changes to the
graphics appearance sometimes.

For the basis on why I did it this way see
 https://stat.ethz.ch/pipermail/r-help/2009-November/412966.html

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff Stevens
Sent: Tuesday, 16 February 2016 03:59
To: R-help Forum
Subject: [R] Condition layer across panels in lattice

I would like to plot individual subject means for two different
conditions in a lattice stripplot with two panels. I would also like
to add within-subject confidence intervals that I have calculated and
stored in separate data frame. I am trying to overlay these confidence
intervals with latticeExtra's layer function. When I add the layer,
either both sets of intervals display on both panels (as illustrated
in code) or both sets of intervals display on only the first panel if
I add [subscripts] to the x's and y's in the layer command
(illustrated in second code clip). How do I get the appropriate
intervals to display on the appropriate panel?

Produces stripplot with both sets of intervals on both panels:
raw_data <- data.frame(subject = rep(1:6, 4), cond1 =
as.factor(rep(1:2, each = 12)), cond2 = rep(rep(c("A", "B"), each =
6), 2), response = c(2:7, 6:11, 3:8, 7:12))
summary_data <- data.frame(cond1 = as.factor(rep(1:2, each = 2)),
cond2 = rep(c("A", "B"), times = 2), mean = aggregate(response ~ cond2
* cond1, raw_data, mean)$response, within_ci = c(0.57, 0.54, 0.6,
0.63))
summary_data$lci <- summary_data$mean - summary_data$within_ci
summary_data$uci <- summary_data$mean + summary_data$within_ci

subject_stripplot <- stripplot(response ~ cond1 | cond2, groups =
subject, data = raw_data,
  panel = function(x, y, ...) {
    panel.stripplot(x, y, type = "b", lty = 2, ...)
    panel.average(x, y, fun = mean, lwd = 2, col = "black", ...)    #
plot line connecting means
  }
)
addWithinCI <- layer(panel.segments(x0 = cond1, y0 = lci, x1 = cond1,
y1 = uci, subscripts = TRUE), data = summary_data, under = FALSE)
plot(subject_stripplot + addWithinCI)

Produces stripplot with both sets of intervals on only the first panel:
addWithinCI2 <- layer(panel.segments(x0 = cond1[subscripts], y0 =
lci[subscripts], x1 = cond1[subscripts], y1 = uci[subscripts],
subscripts = TRUE), data = summary_data, under = FALSE)
plot(subject_stripplot + addWithinCI2)

Thanks,
Jeff

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list