Marius Hofert
2010-Jan-15 15:01 UTC
[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,cols<rows) 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") )
Peter Ehlers
2010-Jan-17 07:56 UTC
[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,cols<rows) > 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 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. > >-- Peter Ehlers University of Calgary 403.202.3921 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: mypanel_pairs.R URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100117/fbaff4f1/attachment.pl>