[R] Jitter in correlation matrix?
Mehmet Atif Ergun
mehmetaergun at gmail.com
Mon Feb 25 00:26:27 CET 2008
Hi,
I am just starting to use R for a graduate course, and I like how the
correlation matrix at
http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=137
I did something similar by copying from the examples(pairs), but it
seems that I need to jitter the bottom panel... and I have no idea how
to do that, and I mean no idea at all. I'd appreciate any help...
Here are the graphs:
http://socy602.pbwiki.com/f/dep_correlationmatrix.jpeg
http://socy602.pbwiki.com/f/indep_correlationmatrix.jpeg
And this is the code I used to produce the graphs (not that I
understand it, but):
######################
# Primary Component Analyses
######################
######################
# Dependent variables
######################
# List of these variables:
# langfamr , ethdifxx , catness , gc7 , gc8r , gc12 , culdifxx , poldifxx , ecdifxx
######################
# Look at correlation matrix for these
cor(MAR[,c("langfamr" , "ethdifxx" , "catness" , "gc7" , "gc8r" , "gc12" , "culdifxx" , "poldifxx" , "ecdifxx" )], use="complete.obs")
# Big time correlation matrix
panel.cor <- function(x, y, digits=2, prefix="", cex.cor)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex * r)
}
MAR.omitdep <- na.omit(MAR[,c("langfamr" , "ethdifxx" , "catness" , "gc7" , "gc8r" , "gc12" , "culdifxx" , "poldifxx" , "ecdifxx" )])
pairs(MAR.omitdep[,c("langfamr" , "ethdifxx" , "catness" , "gc7" , "gc8r" , "gc12" , "culdifxx" , "poldifxx" , "ecdifxx" )], lower.panel=panel.smooth, upper.panel=panel.cor)
# Save it
dev.copy(jpeg,filename="dep_correlationmatrix.jpeg",height=600, width=800,bg="white")
dev.off()
######################
# Independent variables
######################
# List of variables
# poldis , polres , ecdis , culres , gcc1
######################
# Look at the correlation matrix
cor(MAR[,c("poldis" , "polres" , "ecdis" , "culres" , "gcc1" )], use="complete.obs")
# Big time correlation matrix
panel.cor <- function(x, y, digits=2, prefix="", cex.cor)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex * r)
}
MAR.omitindep <- na.omit(MAR[,c("poldis" , "polres" , "ecdis" , "culres" , "gcc1" )])
pairs(MAR.omitindep[,c("poldis" , "polres" , "ecdis" , "culres" , "gcc1" )], lower.panel=panel.smooth, upper.panel=panel.cor)
# Save it
dev.copy(jpeg,filename="indep_correlationmatrix.jpeg",height=600, width=800,bg="white")
dev.off()
Thanks a lot in advance,
Sincerely,
Mehmet.
More information about the R-help
mailing list