Dear useRs,
I recently had a query concerning how to customize the graphics parameters in
lattice::xyplot to change not only the color but also the pch symbols, lty and
lwd line parameters, etc., within each grouping variable in the plot.
(https://stat.ethz.ch/pipermail/r-help/2015-July/430285.html). Many thanks to
Mark Leeds who described the solution using panel.superpose as the panel
function and defining a custom panel.groups function using the subscripts
argument.
Here is a crude example. Note how the pch parameter varies with grouping
variable as well as color. I have commented out two lines which are not
important for the example but are for my main question.
#### begin code 1
# R version 3.2.2 release, lattice version 0.20-33, Windows 7.
dat <- data.frame(Trt=rep(c('1','2'), each=10),
Sbj=rep(c('1','2'), each=5),
X=rep(c(0,1,2,3,4), times=4),
Y=c(1,3,3,3,1, 2,4,4,4,2,
3,1,1,1,3, 4,2,2,2,4)
)
xgrid <- seq(0L, 4L)
ygrid <- seq(0L, 5L)
require(lattice)
xyplot(Y ~ X | Trt, data=dat, groups=Sbj, type='b', lty=1, cex=2, lwd=3,
scales=list(x=list(at=xgrid), y=list(at=ygrid)),
### abline=list(h=ygrid, v=xgrid, col=gray(0.8)),
mycol=c('red', 'blue'), mypch=c(16,17),
panel=panel.superpose,
panel.groups=function(x, y, subscripts,
mycol, mypch, col.line, col.symbol, pch, ...)
{
### panel.abline(h=ygrid, v=xgrid,
col=gray(0.8))
panel.xyplot(x, y, ...,
pch =
mypch[dat[['Sbj']][subscripts]],
col.symbol =
mycol[dat[['Sbj']][subscripts]],
col.line =
mycol[dat[['Sbj']][subscripts]]
)
} # function
) # xyplot
### end code 1
My question involves the commented out lines. I would like to draw a light
grid in the panels. If I used panel.grid() it would be impossible to get the
gridlines into arbitrary positions, as it seems to pick values similar to
pretty() (I would love to be corrected on this). So we can use an abline
argument to the main xyplot call, or a panel.abline() call in the panel.groups
function.
But either way leads to a problem. The ablines seem to be rerendered for each
level of the grouping variable, thus only the final level is plotted without
being the grid rendered on top of it. Try it by uncommenting either line.
This is not totally surprising as it does mention somewhere in the
documentation that panel.groups() is called for each value of the grouping
variable. So my question: How do I render the grid just once, before actually
rendering the data, so as to avoid this problem?
I realized a good place to start might be to include panel.abline in the panel
function itself, rather in the panel.groups function called several times per
panel. Thus something like this:
### begin code 2
xyplot(Y~X|Trt, data=dat, groups=Sbj, type='b', lty=1, cex=2, lwd=3,
scales=list(x=list(at=xgrid), y=list(at=ygrid)),
mycol=c('red', 'blue'), mypch=c(16,17),
panel=function(x, y, ...) { ########## <---------------- new
panel function
panel.abline(h=ygrid, v=xgrid, col=gray(0.9))
panel.superpose(x, y, ...)
},
panel.groups=function(x, y, subscripts,
mycol, mypch, col.line, col.symbol, pch, ...)
{
panel.xyplot(x, y, ...,
pch =
mypch[dat[['Sbj']][subscripts]],
col.symbol =
mycol[dat[['Sbj']][subscripts]],
col.line =
mycol[dat[['Sbj']][subscripts]]
)
} # function
) # xyplot
### end code 2
Of course this won't work as written, I need to replace the ... arguments with
the right ones. Here is where I am having trouble. I have tried all kinds of
permutations of the mycol etc., xgrid etc., subscripts etc., and never got the
plot to render the ablines once, then the data correctly.
Any assistance greatly appreciated.
John
John Szumiloski, Ph.D.
Principal Scientist, Statistician
Analytical and Bioanalytical Development
NBR105-1-1411
Bristol-Myers Squibb
P.O. Box 191
1 Squibb Drive
New Brunswick, NJ
08903-0191
USA
(732) 227-7167
________________________________
This message (including any attachments) may contain co...{{dropped:8}}
______________________________________________
[email protected] 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.