Szumiloski, John
2015-Oct-07 17:40 UTC
[R] [lattice::xyplot] Using (panel:.)abline with panel.superpose?
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}}
Duncan Mackay
2015-Oct-08 00:12 UTC
[R] [lattice::xyplot] Using (panel:.)abline with panel.superpose?
Forgot to send to list -----Original Message----- From: Duncan Mackay [mailto:dulcalma at bigpond.com] Sent: Thursday, 8 October 2015 08:44 To: 'Szumiloski, John' Subject: RE: [R] [lattice::xyplot] Using (panel:.)abline with panel.superpose? Hi John I only got grid lines on your # code 2 with same lattice version on win 7 and blank for the first code Try 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)), col=c('red', 'blue'), pch=c(16,17), panel= panel.superpose, panel.groups = function(x,y, ...){ panel.abline(h=ygrid, v=xgrid, col = gray(0.9) ) panel.xyplot(x,y, ...) }# panel ) # xyplot It produces grid lines over the lines and points so one way around it is to use alpha settings. Not all devices will accept alpha. xyplot(Y~X|Trt, data=dat, groups=Sbj, type='b', lty=1, cex=2, lwd=3, alpha = 1, scales=list(x=list(at=xgrid), y=list(at=ygrid)), col=c('red', 'blue'), pch=c(16,17), panel= panel.superpose, panel.groups = function(x,y,alpha, ...){ panel.abline(h=ygrid, v=xgrid, col=1, alpha = 0.1) panel.xyplot(x,y, alpha = 1, ...) }# panel ) # xyplot If you use par.settings it will give correct colours lines etc to auto key xyplot(Y~X|Trt, data=dat, groups=Sbj, type='b', par.settings = list(strip.background = list(col = "transparent"), superpose.line = list(col = c('red', 'blue'), alpha = 1, lty=1,lwd=3), superpose.symbol = list(col=c('red', 'blue'), alpha = 1, pch=c(16,17), cex=2)), scales=list(x=list(at=xgrid), y=list(at=ygrid)), auto.key = T, panel= panel.superpose, panel.groups = function(x,y,alpha, ...){ panel.abline(h=ygrid, v = xgrid, col=1, alpha = 0.1) panel.xyplot(x,y, alpha = 1, ...) }# panel ) # xyplot 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 Szumiloski, John Sent: Thursday, 8 October 2015 03:40 To: r-help at r-project.org Subject: [R] [lattice::xyplot] Using (panel:.)abline with panel.superpose? 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}}