[R] Lattice: splom plots for different factors with correlation in lower.panel
Tom Porteus
porteus at zoology.ubc.ca
Tue May 10 00:00:27 CEST 2011
Hello list,
I am looking to create a figure for my dataset using splom, where there is a
splom subplot for each level of a factor within, for example, a 2x2 layout.
For each subplot, I wish to put the r-value between each variable pair in
the lower panel. The code I have thus far is below, using the iris dataset
so that it is reproducible. This almost does what I want, but for some
reason overlays the r-values for each level in each subplot.
I think the problem is something to do with the lower.panel function call
within my custom function, but I am not a huge whizz using lattice and can't
figure out what it is. If I put the panel.text call in the lower.panel
function, it only causes a similar problem. Apologies if there is something
obvious I have missed, I hope there is someone out there who can help me!
var.cor <- list()
for(i in 1:3){
var.cor[[i]] <- abs(cor(subset(iris[1:4], iris$Species ==
levels(iris$Species)[i]))) #calculate variable correlation for each species
var.cor[[i]] <- data.frame(values = as.vector(var.cor[[i]]), row =
as.vector(row(var.cor[[i]])), col = as.vector(col(var.cor[[i]])))
var.cor[[i]] <- subset(var.cor[[i]], col<row) #create data frame with value
and row & col indexes for values with each panel
}
panel.pairs.cor <- function(z, ...){
panel.pairs(z, upper.panel=panel.splom, lower.panel=function(){}, ...)
for(i in 1:3){
panel.text(x=var.cor[[i]]$row, y=var.cor[[i]]$col,
labels=round(var.cor[[i]]$values, 2))
}
}
splom(~iris[1:4]|iris$Species, layout=c(2,2), pscales=0, col="black",
xlab="", ylab="", as.table=TRUE, superpanel=panel.pairs.cor)
Many thanks,
Tom
--
View this message in context: http://r.789695.n4.nabble.com/Lattice-splom-plots-for-different-factors-with-correlation-in-lower-panel-tp3510452p3510452.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list