Re: [R] Lattice: How to color the data points in splom() according to the panel they are plotted?
Dear Deepayan, this is in reply to a message almost 6 months ago : Deepayan Sarkar deepayan.sar...@gmail.com on Sun, 17 Jan 2010 01:39:21 -0800 writes: On Sat, Jan 16, 2010 at 11:56 PM, Peter Ehlers ehl...@ucalgary.ca wrote: Marius Hofert wrote: Dear ExpeRts, I have the scatter plot matrix as given below. I would like the different sub-plots in the scatter plot matrix to be colored differently. How do I get all points shown in the upper-left plot (on position (1,1) in the scatter plot matrix) to be plotted in blue, and the points shown in the plot to the right (on position (1,2) in the scatter plot matrix) to be plotted in red? More generally, how can I provide a matrix of colors to be used by splom() such that all data points in the corresponding sub-plot of the scatter plot matrix are shown in the specified color? Cheers, Marius Here is the code: library(lattice) entrymat=matrix(0,nrow=3,ncol=3) entrymat[1,2]=black entrymat[1,3]=blue entrymat[2,3]=red entrymat=t(entrymat) splom(~iris[,1:3],superpanel=function(z,...){ mymat.df=data.frame(rows=as.vector(row(entrymat)),cols=as.vector(col(entrymat)),entries=as.vector(entrymat)) mymat.df=subset(mymat.df,colsrows) with(mymat.df,{ panel.text(x=rows,y=cols,labels=entries) }) panel.pairs(z,upper.panel=panel.splom,lower.panel=function(...){},...) },varnames=c(1,2,3) ) I think that you will have to modify panel.pairs to get what you want. But I must admit that I can't see why you would want such a plot. What's achieved by having different colours in different subpanels? And you would lose the ability to colour groups differently (or things would become really complicated and messy). Thanks, I was going to say the same thing, except that it would be (1) conceptually simpler just to add the 'i' and 'j' values as arguments to the panel function (the 'pargs' variable). The colors could then be passed through as part of the ... arguments, and the relevant entry extracted in the panel function. (2) The other option is to keep a global counter and increment it inside the panel function, and choosing colors based on that counter and knowledge of the order in which panels are drawn. Not very elegant, but the least intrusive solution I can think of. -Deepayan Against the R-forge version of lattice, the following very small patch to panel.pairs would allow users to use '(1)' i.e., provide panel functions with (i,j) arguments directly. I'm pretty sure that the change could not easily break existing splom() usage. --- R/splom.R (revision 619) +++ R/splom.R (working copy) @@ -291,7 +291,8 @@ y = z[subscripts, i]), ##panel.number = panel.number, ##packet.number = packet.number), - list(...)) + list(...), + list(i = i, j = j)) else c(list(x = z[subscripts, j], y = z[subscripts, i], @@ -299,7 +300,8 @@ subscripts = subscripts), ##panel.number = panel.number, ##packet.number = packet.number), - list(...)) + list(...), + list(i = i, j = j)) if (!(... %in% names(formals(panel pargs - pargs[intersect(names(pargs), names(formals(panel)))] With the above change, a user could use a panel function with (i,j) arguments, and e.g. say Cmat - outer(1:6,1:6, function(i,j) rainbow(11, start=.12, end=.5)[i+j-1]) splom(~diag(6), ## for testing: superpanel=mypanel.pairs, panel=function(x,y,i,j,...){ panel.fill(Cmat[i,j]); panel.splom(x,y,...) panel.text(.5,.5, paste((,i,,,j,),sep=)) }) I think that would allow quite a bit more flexibility without the need to explicitly hack panel.pairs (and having to maintain such a hack against the ever-enhancing lattice). Martin Maechler, ETH Zurich __ R-help@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.
Re: [R] Lattice: How to color the data points in splom() according to the panel they are plotted?
On Mon, Jun 14, 2010 at 6:24 PM, Martin Maechler maech...@stat.math.ethz.ch wrote: Dear Deepayan, this is in reply to a message almost 6 months ago : Deepayan Sarkar deepayan.sar...@gmail.com [...] Thanks, I was going to say the same thing, except that it would be (1) conceptually simpler just to add the 'i' and 'j' values as arguments to the panel function (the 'pargs' variable). The colors could then be passed through as part of the ... arguments, and the relevant entry extracted in the panel function. (2) The other option is to keep a global counter and increment it inside the panel function, and choosing colors based on that counter and knowledge of the order in which panels are drawn. Not very elegant, but the least intrusive solution I can think of. -Deepayan Against the R-forge version of lattice, the following very small patch to panel.pairs would allow users to use '(1)' i.e., provide panel functions with (i,j) arguments directly. I'm pretty sure that the change could not easily break existing splom() usage. --- R/splom.R (revision 619) +++ R/splom.R (working copy) @@ -291,7 +291,8 @@ y = z[subscripts, i]), ## panel.number = panel.number, ## packet.number = packet.number), - list(...)) + list(...), + list(i = i, j = j)) else c(list(x = z[subscripts, j], y = z[subscripts, i], @@ -299,7 +300,8 @@ subscripts = subscripts), ## panel.number = panel.number, ## packet.number = packet.number), - list(...)) + list(...), + list(i = i, j = j)) if (!(... %in% names(formals(panel pargs - pargs[intersect(names(pargs), names(formals(panel)))] Done in r-forge svn. -Deepayan With the above change, a user could use a panel function with (i,j) arguments, and e.g. say Cmat - outer(1:6,1:6, function(i,j) rainbow(11, start=.12, end=.5)[i+j-1]) splom(~diag(6), ## for testing: superpanel=mypanel.pairs, panel=function(x,y,i,j,...){ panel.fill(Cmat[i,j]); panel.splom(x,y,...) panel.text(.5,.5, paste((,i,,,j,),sep=)) }) I think that would allow quite a bit more flexibility without the need to explicitly hack panel.pairs (and having to maintain such a hack against the ever-enhancing lattice). Martin Maechler, ETH Zurich __ R-help@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.
Re: [R] Lattice: How to color the data points in splom() according to the panel they are plotted?
On Sat, Jan 16, 2010 at 11:56 PM, Peter Ehlers ehl...@ucalgary.ca wrote: Marius Hofert wrote: Dear ExpeRts, I have the scatter plot matrix as given below. I would like the different sub-plots in the scatter plot matrix to be colored differently. How do I get all points shown in the upper-left plot (on position (1,1) in the scatter plot matrix) to be plotted in blue, and the points shown in the plot to the right (on position (1,2) in the scatter plot matrix) to be plotted in red? More generally, how can I provide a matrix of colors to be used by splom() such that all data points in the corresponding sub-plot of the scatter plot matrix are shown in the specified color? Cheers, Marius Here is the code: library(lattice) entrymat=matrix(0,nrow=3,ncol=3) entrymat[1,2]=black entrymat[1,3]=blue entrymat[2,3]=red entrymat=t(entrymat) splom(~iris[,1:3],superpanel=function(z,...){ mymat.df=data.frame(rows=as.vector(row(entrymat)),cols=as.vector(col(entrymat)),entries=as.vector(entrymat)) mymat.df=subset(mymat.df,colsrows) with(mymat.df,{ panel.text(x=rows,y=cols,labels=entries) }) panel.pairs(z,upper.panel=panel.splom,lower.panel=function(...){},...) },varnames=c(1,2,3) ) I think that you will have to modify panel.pairs to get what you want. But I must admit that I can't see why you would want such a plot. What's achieved by having different colours in different subpanels? And you would lose the ability to colour groups differently (or things would become really complicated and messy). Thanks, I was going to say the same thing, except that it would be conceptually simpler just to add the 'i' and 'j' values as arguments to the panel function (the 'pargs' variable). The colors could then be passed through as part of the ... arguments, and the relevant entry extracted in the panel function. The other option is to keep a global counter and increment it inside the panel function, and choosing colors based on that counter and knowledge of the order in which panels are drawn. Not very elegant, but the least intrusive solution I can think of. -Deepayan __ R-help@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.
Re: [R] Lattice: How to color the data points in splom() according to the panel they are plotted?
Deepayan Sarkar wrote: On Sat, Jan 16, 2010 at 11:56 PM, Peter Ehlers ehl...@ucalgary.ca wrote: Marius Hofert wrote: Dear ExpeRts, I have the scatter plot matrix as given below. I would like the different sub-plots in the scatter plot matrix to be colored differently. How do I get all points shown in the upper-left plot (on position (1,1) in the scatter plot matrix) to be plotted in blue, and the points shown in the plot to the right (on position (1,2) in the scatter plot matrix) to be plotted in red? More generally, how can I provide a matrix of colors to be used by splom() such that all data points in the corresponding sub-plot of the scatter plot matrix are shown in the specified color? Cheers, Marius Here is the code: library(lattice) entrymat=matrix(0,nrow=3,ncol=3) entrymat[1,2]=black entrymat[1,3]=blue entrymat[2,3]=red entrymat=t(entrymat) splom(~iris[,1:3],superpanel=function(z,...){ mymat.df=data.frame(rows=as.vector(row(entrymat)),cols=as.vector(col(entrymat)),entries=as.vector(entrymat)) mymat.df=subset(mymat.df,colsrows) with(mymat.df,{ panel.text(x=rows,y=cols,labels=entries) }) panel.pairs(z,upper.panel=panel.splom,lower.panel=function(...){},...) },varnames=c(1,2,3) ) I think that you will have to modify panel.pairs to get what you want. But I must admit that I can't see why you would want such a plot. What's achieved by having different colours in different subpanels? And you would lose the ability to colour groups differently (or things would become really complicated and messy). Thanks, I was going to say the same thing, except that it would be conceptually simpler just to add the 'i' and 'j' values as arguments to the panel function (the 'pargs' variable). The colors could then be passed through as part of the ... arguments, and the relevant entry extracted in the panel function. The other option is to keep a global counter and increment it inside the panel function, and choosing colors based on that counter and knowledge of the order in which panels are drawn. Not very elegant, but the least intrusive solution I can think of. Now why didn't I think of that? The global counter seems to me the way to go. But, of course, I still see very little need for this option. -Peter __ R-help@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.
Re: [R] Lattice: How to color the data points in splom() according to the panel they are plotted?
Marius Hofert wrote: Dear ExpeRts, I have the scatter plot matrix as given below. I would like the different sub-plots in the scatter plot matrix to be colored differently. How do I get all points shown in the upper-left plot (on position (1,1) in the scatter plot matrix) to be plotted in blue, and the points shown in the plot to the right (on position (1,2) in the scatter plot matrix) to be plotted in red? More generally, how can I provide a matrix of colors to be used by splom() such that all data points in the corresponding sub-plot of the scatter plot matrix are shown in the specified color? Cheers, Marius Here is the code: library(lattice) entrymat=matrix(0,nrow=3,ncol=3) entrymat[1,2]=black entrymat[1,3]=blue entrymat[2,3]=red entrymat=t(entrymat) splom(~iris[,1:3],superpanel=function(z,...){ mymat.df=data.frame(rows=as.vector(row(entrymat)),cols=as.vector(col(entrymat)),entries=as.vector(entrymat)) mymat.df=subset(mymat.df,colsrows) with(mymat.df,{ panel.text(x=rows,y=cols,labels=entries) }) panel.pairs(z,upper.panel=panel.splom,lower.panel=function(...){},...) },varnames=c(1,2,3) ) I think that you will have to modify panel.pairs to get what you want. But I must admit that I can't see why you would want such a plot. What's achieved by having different colours in different subpanels? And you would lose the ability to colour groups differently (or things would become really complicated and messy). Attached is a modified panel.pairs function (but don't let Deepayan see it; he'll probably be horrified by the kludginess). Source the file and then try: splom(~iris[1:3], superpanel = mypanel.pairs, clrmat = matrix(1:9,3,3)) splom(~iris[1:3], superpanel = mypanel.pairs, clrmat = matrix(c(NA,1,1,2,NA,4,2,6,NA),3,3)) theclrs - matrix(c(NA,1,1,1,2,NA,3,3,2,4,NA,4,2,5,5,NA),4,4) splom(~iris[1:4], superpanel = mypanel.pairs, clrmat = theclrs) -Peter Ehlers __ R-help@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. -- Peter Ehlers University of Calgary 403.202.3921 ## modified panel.pairs to allow different colours in different ## subpanels. Doesn't work if data includes factors. ## overrides 'groups' colouring. mypanel.pairs - function (z, panel = lattice.getOption(panel.splom), lower.panel = panel, upper.panel = panel, diag.panel = diag.panel.splom, as.matrix = FALSE, groups = NULL, panel.subscripts, subscripts, pscales = 5, clrmat, ## added argument: matrix of colours, one for each subpanel (matrix includes diags, but these colours are not used) prepanel.limits = function(x) if (is.factor(x)) levels(x) else range(as.numeric(x), finite = TRUE), varname.col = add.text$col, varname.cex = add.text$cex, varname.font = add.text$font, varname.fontfamily = add.text$fontfamily, varname.fontface = add.text$fontface, axis.text.col = axis.text$col, axis.text.cex = axis.text$cex, axis.text.font = axis.text$font, axis.text.fontfamily = axis.text$fontfamily, axis.text.fontface = axis.text$fontface, axis.line.col = axis.line$col, axis.line.lty = axis.line$lty, axis.line.lwd = axis.line$lwd, axis.line.alpha = axis.line$alpha, axis.line.tck = 1, ...) { lower.panel - if (is.function(lower.panel)) lower.panel else if (is.character(lower.panel)) get(lower.panel) else eval(lower.panel) upper.panel - if (is.function(upper.panel)) upper.panel else if (is.character(upper.panel)) get(upper.panel) else eval(upper.panel) diag.panel - if (is.function(diag.panel)) diag.panel else if (is.character(diag.panel)) get(diag.panel) else eval(diag.panel) add.text - trellis.par.get(add.text) axis.line - trellis.par.get(axis.line) axis.text - trellis.par.get(axis.text) n.var - ncol(z) if (n.var == 0) return() lim - vector(list, length = n.var) for (i in seq_len(n.var)) lim[[i]] - if (is.list(pscales) !is.null(pscales[[i]]$lim)) pscales[[i]]$lim else prepanel.limits(z[, i]) if (length(subscripts)) { draw - is.list(pscales) || (is.numeric(pscales) pscales != 0) splom.layout - grid.layout(nrow = n.var, ncol = n.var) pushViewport(viewport(layout = splom.layout, name = pairs)) for (i in 1:n.var) for (j in 1:n.var) { if (as.matrix) pushViewport(viewport(layout.pos.row = i, layout.pos.col = j, name = paste(subpanel, j, i, sep = .), clip = trellis.par.get(clip)$panel, xscale = if
[R] Lattice: How to color the data points in splom() according to the panel they are plotted?
Dear ExpeRts, I have the scatter plot matrix as given below. I would like the different sub-plots in the scatter plot matrix to be colored differently. How do I get all points shown in the upper-left plot (on position (1,1) in the scatter plot matrix) to be plotted in blue, and the points shown in the plot to the right (on position (1,2) in the scatter plot matrix) to be plotted in red? More generally, how can I provide a matrix of colors to be used by splom() such that all data points in the corresponding sub-plot of the scatter plot matrix are shown in the specified color? Cheers, Marius Here is the code: library(lattice) entrymat=matrix(0,nrow=3,ncol=3) entrymat[1,2]=black entrymat[1,3]=blue entrymat[2,3]=red entrymat=t(entrymat) splom(~iris[,1:3],superpanel=function(z,...){ mymat.df=data.frame(rows=as.vector(row(entrymat)),cols=as.vector(col(entrymat)),entries=as.vector(entrymat)) mymat.df=subset(mymat.df,colsrows) with(mymat.df,{ panel.text(x=rows,y=cols,labels=entries) }) panel.pairs(z,upper.panel=panel.splom,lower.panel=function(...){},...) },varnames=c(1,2,3) ) __ R-help@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.