[R] xyplot: subscripts, groups and subset

From: Jim Price <price_ja_at_hotmail.com>
Date: Fri, 16 May 2008 12:56:32 -0700 (PDT)

I have stumbled across something in the Lattice package that is vexing me. Consider the code below:


library(lattice)

myData <- expand.grid(sub = factor(1:16), time = 1:10)

myData$observed <- rnorm(nrow(myData))
myData$fitted <- with(myData, ave(observed, sub, FUN = mean))
myData$event.time <- with(myData, ave(observed, sub, FUN = function(.x) 10 *
runif(1)))

myData <- myData[order(myData$sub, myData$time),]

# This version works...
xyplot(

	fitted + observed ~ time | sub,
	data = myData,
	subscripts = TRUE,
	panel = function(..., groups = groups, subscripts = subscripts)
	{
		panel.xyplot(..., groups = groups, subscripts = subscripts)

		event.time <- unique(myData$event.time[subscripts])
		panel.abline(v = event.time, lty = 2, col = 'green')
	},
	type = c('l','p'),
	distribute.type = TRUE,
	as.table = TRUE

)

# ...but when you add the subset parameter it produces multiple index lines per subject
xyplot(

	fitted + observed ~ time | sub,
	data = myData,
	subset = sub %in% sample(unique(sub), 9),
	subscripts = TRUE,
	panel = function(..., groups = groups, subscripts = subscripts)
	{
		panel.xyplot(..., groups = groups, subscripts = subscripts)

		event.time <- unique(myData$event.time[subscripts])
#		print(event.time)
		panel.abline(v = event.time, lty = 2, col = 'green')
	},
	type = c('l','p'),
	distribute.type = TRUE,
	as.table = TRUE

)

The (commented out) print statement I think indicates that there is a data reordering going on for the second example, that is causing the multiple index lines issue. Is there a neat solution to get the correct index lines per subject, or do I need a workaround? Or am I missing something fundamental in the code above that is causing issues?

> sessionInfo()
R version 2.7.0 (2008-04-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] lattice_0.17-6

loaded via a namespace (and not attached): [1] grid_2.7.0

Thanks,
Jim Price
Cardiome Pharmaceutical Corporation.

-- 
View this message in context: http://www.nabble.com/xyplot%3A-subscripts%2C-groups-and-subset-tp17282996p17282996.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
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.
Received on Fri 16 May 2008 - 20:23:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 17 May 2008 - 00:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive