[R] xyplot() with segments() superposed?
Yuelin Li
liy12 at mskcc.org
Mon Aug 13 20:46:15 CEST 2007
Thanks for the quick response. The general use is like the following.
Suppose I have another variable, tkmark, which for cohort "1970" is
coded 1.0 at x = c(7, 19) and 0 otherwise. For cohort "1980", tkmark
is coded 1.0 at x = c(2, 11, 12, 19) and 0 otherwise. For each
"cohort", I want to mark with a short vertical bar the observed Y at
(x[tkmark == 1 & source == "obs"], y[tkmark == 1 & source == "obs"]).
I may have up to 65 different "cohorts". Different cohorts have
different numbers of tkmark positions (some have none).
The real data come from a computerized cigarette smoking reduction
program. I want to plot each smoker's tapering of cigarettes (y) over
days (x), and mark the quit attempts. Some participants try to quit
on days 3, 7, and 17 but resume smoking the next day, some follow the
programmed tapering exactly with no interim quit attempts, and some
ignored the tapering program. I want to plot each participant's
**scheduled** and **observed** smoking pattern over days and mark the
quit attempts. Hope this is clear.
Yuelin.
-- Deepayan Sarkar wrote --|Mon (Aug/13/2007)[09:35]|--:
On 8/11/07, Yuelin Li <liy12 at mskcc.org> wrote:
> In the hypothetical example below, how do I add two segments() into
> the two panels, respectively? Say segments(x0=5, y0=10, x1=5, y1=20)
> on the left and segments(x0=15, y0=-10, x1=15, y1=-2) on the right?
> Many thanks in advance,
>
> Yuelin Li.
>
> ps. part of the code came from a solution given by Deepayan Sarkar.
>
> -------------------
> library(lattice)
> set.seed(12345)
> x <- 0:20
> y.male.obs <- - 1.2 * x + 22 + rnorm(length(x), sd = 3)
> y.male.prd <- - 1.2 * x + 22
> y.fema.obs <- - 2.2 * x + 30 + rnorm(length(x), sd = 2)
> y.fema.prd <- - 2.2 * x + 30
> tdat <- data.frame(x = rep(x, 8),
> y = rep(c(y.male.obs, y.male.prd, y.fema.obs, y.fema.prd), 2),
> sex = rep(rep(c("m", "f"), each = 2*length(x)), 2),
> cohort = rep(c("1970", "1980"), each = 4*length(x)),
> source = rep(rep(c("obs", "prd"), each = length(x)), 4) )
> xyplot(y ~ x | as.factor(cohort), data = tdat,
> groups = interaction(sex, source),
> type = c("p", "p", "l", "l"), distribute.type = TRUE)
If this is a one-off requirement, the simplest solution is:
xyplot(y ~ x | as.factor(cohort), data = tdat,
groups = interaction(sex, source),
type = c("p", "p", "l", "l"), distribute.type = TRUE,
panel = function(...) {
panel.xyplot(...)
switch(panel.number(),
panel.segments(x0=5, y0=10, x1=5, y1=20),
panel.segments(x0=15, y0=-10, x1=15, y1=-2))
})
This is not generalizable, but you haven't told us your general use case.
-Deepayan
=====================================================================
Please note that this e-mail and any files transmitted with it may be
privileged, confidential, and protected from disclosure under
applicable law. If the reader of this message is not the intended
recipient, or an employee or agent responsible for delivering this
message to the intended recipient, you are hereby notified that any
reading, dissemination, distribution, copying, or other use of this
communication or any of its attachments is strictly prohibited. If
you have received this communication in error, please notify the
sender immediately by replying to this message and deleting this
message, any attachments, and all copies and backups from your
computer.
More information about the R-help
mailing list