obtaining numeric correlations within scatterplot matrix(pairs)?

#1
Hi all,
I have created scatterplot matrixes for my data but cannot for the life of me figure out how to obtain and then insert the correlations in the matrix. I had read posts online but I am generating errors. My pairs formula is
pairs(~glass_sponge+Depth+S+M+Sm+P,data=Gs)


I looked at the R help file and just tried to apply what they wrote, and tried...
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.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
pairs(Gs, upper.panel=panel.cor)


this doesnt work...which isnt surprising because I do not know what all the number mean in the example...what do I need to modify to make this error not occur? Or better yet, just make my own correlations not using this R example?

I have attached the plot. Your help is appreciated, I have spent an afternoon trying to sort this out and I am sure it cannot be so difficult, but I am definitely missing something here.

Best Regards.
 

trinker

ggplot2orBust
#2
Hi snowsoc11 :welcome:

You haven't provided a data set to work with and thus it's more difficult to assist with your problem. For more about how to provide a reproducible example check out:

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

It's also much easir to help when you place code inside of code tags. You can do this by either clicking the pound (#) sign icon or wrap with [NOPARSE]
Code:
some code
[/NOPARSE]
which produces:
Code:
some code
Once you do that it'll be easier to help you. In the mean time may I suggest the pairs.panels function from the psych package. I think it's more informative visually and gives you the correlations you're after. Here's an example:
Code:
library(psych)
pairs.panels(attitude)
 
#3
thank you for your reply! I am sorry it is so obvious this is my first time on the forum. I actually tried the library pysch package and my R (r studio console) said the psych package doesnt exist...not sure what is going on there.
I did my best with trying to understand the link you sent, I attached my dput output of my data set, maybe that will help? The main goal of this is to find correlations in environmental variables (predict) in regards to species densities (response). I am sorry if the attachment is not at all what you meant in providing data, I hope it is!
 

Jake

Cookie Scientist
#4
You need to install the psych package first before you can load it, using:
Code:
install.packages("psych")
 
#5
Great thank you. After trying the psych method and pasting the formula I posted above originally, I get the error Error in plot.new() : figure margins too large and that is kind of the root of my problem, if that helps determine what I must adjust.
 
#7
Hi Lazar. I put dev.off() at the end of my
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.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
dev.off()
}
pairs(Gs, upper.panel=panel.cor)

and it is still generating the error...I dont think R and I get along too well. I may just go do all of the correlations manually, blah.
 

trinker

ggplot2orBust
#8
@snowsoc11

It's a little bit of a learning curve but so worth it.

We're having a hard time helping you because we don't have a data set to work with (and less detrimental but still hindering your response you're not using code tags for code and data sets). Please see my comments above on how to put your code in code tags. To give us a data set to work with use the following code and post it back here:

Code:
dput(head(Gs))
I assume your data set is called Gs because that's what you're feeding to the pairs function.
 

Lazar

Phineas Packard
#9
Sorry snow I did not mean for you to add dev.off to your code. What sometimes happens is that if you have used some device previously (e.g. pdf, svg, etc.) and forgot to use dev.off you will get the error you got. I was suggesting you use dev.off to reset the plots.