[R] xyplot auto.key issue

Mike Lawrence Mike.Lawrence at Dal.Ca
Sun Jul 31 17:22:05 CEST 2005


Hi again,

Deepayan, I tried adding a "pch = c(1,2,3,4,5)" line in the auto key but the
"zero-length 'pch'" error still occurs.

Sundar, I tried your code using key instead of auto.key, and after tweaking it
to fit my design (i.e. from "x~y, groups = a" to "x~y | a, groups = b") it
works perfectly! Thanks!

Here's the code that finally worked (that is, the modifications to Sundar's
code, which might be clearer for others to follow than my own):

library(lattice)
set.seed(1)

z <- expand.grid(x=1:10, b = LETTERS[1:5], a = LETTERS[10:18])
z$y <- rnorm(nrow(z))

trellis.par.set(theme = col.whitebg())
par.line <- trellis.par.get("superpose.line")
par.symb <- trellis.par.get("superpose.symbol")
n <- seq(nlevels(z$b))

my.key <- list(
	space = "right",
	border = TRUE,
	cex.title = 1.2,
	title = "My Key",
	size = 7,
	lines = list(
		pch = par.symb$pch[n],
		lty = par.line$lty[n],
		col = par.line$col[n],
		type = "b"
	),
	text = list(
		levels(z$b)
	)
)

xyplot(
	y ~ x | a,
	data = z,
	groups = b,
	pch = par.symb$pch[n],
	type = "b",
	key = my.key
)










Quoting Sundar Dorai-Raj <sundar.dorai-raj at pdf.com>:

> Hi, Mike,
>
> Mike Lawrence wrote:
>> Hi Deepayan,
>>
>> Thanks for the reply, but when I enter the " type = "b" " code into 
>> the auto.key
>> (see below) command I get the following message:
>>
>> Error in valid.pch(x$pch) : zero-length 'pch'
>>
>> Any suggestions?
>>
>
> Why not just ignore auto.key and use key? Personally, I use auto.key 
> only when I want the defaults. If I want something more customized, 
> then I use key. As in,
>
> library(lattice)
> set.seed(1)
> z <- expand.grid(x=1:10, g = LETTERS[1:5])
> z$y <- rnorm(nrow(z))
> trellis.par.set(theme = col.whitebg())
> par.line <- trellis.par.get("superpose.line")
> par.symb <- trellis.par.get("superpose.symbol")
> n <- seq(nlevels(z$g))
> my.key <- list(space = "right",
>                border = TRUE,
>                cex.title = 1.2,
>                title = "My Key",
>                size = 7,
>                lines = list(pch = par.symb$pch[n],
>                   lty = par.line$lty[n],
>                   col = par.line$col[n],
>                   type = "b"),
>                text = list(levels(z$g)))
> xyplot(y ~ x, z, groups = g,
>        pch = par.symb$pch[n], type = "b",
>        key = my.key)
>
>> xyplot(
>> 	#basic settings
>> 	bias ~ sample_size | measure,
>> 	data = bias,
>> 	groups = exp_tau,
>> 	type = "b",
>> 	pch = c(1,2,3,4,5),
>> 	xlab = "Sample Size",
>> 	ylab = "Bias (ms)",
>> 	#make strips transparent
>> 	strip = function(bg, ...) strip.default(bg = 'transparent', ...),
>> 	# tweak scales
>> 	scales=list(
>> 		x=list(
>> 			at = c(20, 40, 60),
>> 			tck = c(1,0),
>> 			alternating = F
>> 		),
>> 		y=list(
>> 			at = c(-50, -25, 0, 25, 50),
>> 			tck = c(1,0),
>> 			alternating = F
>> 		)
>> 	),
>> 	# tell key to match symbols to those used in the plot
>> 	par.settings = list(
>> 		superpose.symbol = list(
>> 			cex = .8,
>> 			pch = c(1,2,3,4,5)
>> 		)
>> 	),
>> 	# key settings
>> 	auto.key = list (
>> 		type = "b",
>> 		lines = T,
>> 		border = T,
>> 		cex.title = 1.2,
>> 		title = "Expected Tau",
>> 		text = c("30 ms", "80 ms", "130 ms", "180 ms", "230 ms"),
>> 		space = "right"
>> 	)
>> )
>>
>>
>> Quoting Deepayan Sarkar <deepayan.sarkar at gmail.com>:
>>
>>
>>> On 7/30/05, Mike Lawrence <Mike.Lawrence at dal.ca> wrote:
>>>
>>>> Quick correction:
>>>>
>>>> The lines "lines = T," & "type = "b"" in the "par.settings" section should
>>>> not
>>>> be there. They are remnants of my previous (failed) attempts at 
>>>> solving the
>>>> problem. Below is the correct code:
>>>>
>>>> xyplot(
>>>> 	#basic settings
>>>> 	bias ~ sample_size | measure,
>>>> 	data = bias,
>>>> 	groups = exp_tau,
>>>> 	type = "b",
>>>> 	pch = c(1,2,3,4,5),
>>>> 	#make strips transparent
>>>> 	strip = function(bg, ...) strip.default(bg = 'transparent', ...),
>>>> 	# tweak scales
>>>> 	scales=list(
>>>> 		x=list(
>>>> 			at = c(20, 40, 60),
>>>> 			tck = c(1,0),
>>>> 			alternating = F
>>>> 		),
>>>> 		y=list(
>>>> 			at = c(-50, -25, 0, 25, 50),
>>>> 			tck = c(1,0),
>>>> 			alternating = F
>>>> 		)
>>>> 	),
>>>> 	# tell key to match symbols to those used in the plot
>>>> 	par.settings = list(
>>>> 		superpose.symbol = list(
>>>> 			cex = .8,
>>>> 			pch = c(1,2,3,4,5)
>>>> 		)
>>>> 	),
>>>> 	# key settings
>>>> 	auto.key = list (
>>>> 		lines = T,
>>>> 		size = 7,
>>>
>>> You seem to be missing a 'type="b"' somewhere here. The type="b"
>>> argument to xyplot is actually handled by the panel function. The key
>>> has type="l" by default (see under 'key' in ?xyplot) and has to be
>>> changed explicitly.
>>>
>>>
>>>> 		border = T,
>>>> 		cex.title = 1.2,
>>>> 		title = "Expected Tau",
>>>> 		text = c("30 ms", "80 ms", "130 ms", "180 ms", "230 ms"),
>>>> 		space = "right",
>>>> 	)
>>>> )
>>>>
>>>>
>>>>
>>>> Quoting Mike Lawrence <Mike.Lawrence at dal.ca>:
>>>>
>>>>
>>>>> Hi all,
>>>>>
>>>>> I'm having a problem with the auto.key function in xyplot. I hate to
>>>>> bother the
>>>>> list like this and I'm positive I must be missing something very simple,
>>>>
>>>> yet
>>>>
>>>>> I've spent the last day searching for a solution to no avail.
>>>>>
>>>>> Essentially, I want a key that contains entries in which the plot points
>>>>
>>>> are
>>>>
>>>>> superimposed on a line of the same color as the points, like this:
>>>>
>>>> o--o--o
>>>>
>>>>> Now, given the presence of the default "divide" command, I assume this is
>>>>> simple; indeed, I get the impression that this representation is
>>>>> supposed to be
>>>>> produced automatically. Yet I can't seem to get it to work!
>>>>>
>>>>> Now, I've incorporated various other tweaks to my xyplot function, so I'm
>>>>> wondering if these tweaks are somehow hindering my efforts. The function
>>>>
>>>> is
>>>>
>>>>> pasted below; I am making a 3x3 plot, each panel contains 5 lines and it
>>>>
>>>> is
>>>>
>>>>> these lines that I want represented in the key. See the comments for
>>>>> descriptions of the modifications.
>>>>>
>>>>> Any help would be greatly appreciated.
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Mike
>>>>>
>>>>>
>>>>> xyplot(
>>>>> 	#basic settings
>>>>> 	bias ~ sample_size | measure,
>>>>> 	data = bias,
>>>>> 	groups = exp_tau,
>>>>> 	type = "b",
>>>>> 	pch = c(1,2,3,4,5),
>>>>> 	#make strips transparent
>>>>> 	strip = function(bg, ...) strip.default(bg = 'transparent', ...),
>>>>> 	# tweak scales
>>>>> 	scales=list(
>>>>> 		x=list(
>>>>> 			at = c(20, 40, 60),
>>>>> 			tck = c(1,0),
>>>>> 			alternating = F
>>>>> 		),
>>>>> 		y=list(
>>>>> 			at = c(-50, -25, 0, 25, 50),
>>>>> 			tck = c(1,0),
>>>>> 			alternating = F
>>>>> 		)
>>>>> 	),
>>>>> 	# tell key to match symbols to those used in the plot
>>>>> 	par.settings = list(
>>>>> 		superpose.symbol = list(
>>>>> 			cex = .8,
>>>>> 			pch = c(1,2,3,4,5)
>>>>> 		),
>>>>> 		lines = T,
>>>>> 		type = "b"
>>>>> 	),
>>>>> 	# key settings
>>>>> 	auto.key = list (
>>>>> 		lines = T,
>>>>> 		size = 7,
>>>>> 		border = T,
>>>>> 		cex.title = 1.2,
>>>>> 		title = "Expected Tau",
>>>>> 		text = c("30 ms", "80 ms", "130 ms", "180 ms", "230 ms"),
>>>>> 		space = "right",
>>>>> 	)
>>>>> )
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> Mike Lawrence, BA(Hons)
>>>>> Research Assistant to Dr. Gail Eskes
>>>>> Dalhousie University & QEII Health Sciences Centre (Psychiatry)
>>>>>
>>>>> Mike.Lawrence at Dal.Ca
>>>>>
>>>>> "The road to Wisdom? Well, it's plain and simple to express:
>>>>> Err and err and err again, but less and less and less."
>>>>> - Piet Hein
>>>>>
>>>>> ______________________________________________
>>>>> R-help at stat.math.ethz.ch mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide!
>>>>
>>>> http://www.R-project.org/posting-guide.html
>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> Mike Lawrence, BA(Hons)
>>>> Research Assistant to Dr. Gail Eskes
>>>> Dalhousie University & QEII Health Sciences Centre (Psychiatry)
>>>>
>>>> Mike.Lawrence at Dal.Ca
>>>>
>>>> "The road to Wisdom? Well, it's plain and simple to express:
>>>> Err and err and err again, but less and less and less."
>>>> - Piet Hein
>>>>
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide!
>>>> http://www.R-project.org/posting-guide.html
>>>>
>>>
>>
>>
>>
>



-- 

Mike Lawrence, BA(Hons)
Research Assistant to Dr. Gail Eskes
Dalhousie University & QEII Health Sciences Centre (Psychiatry)

Mike.Lawrence at Dal.Ca

"The road to Wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
- Piet Hein




More information about the R-help mailing list