I am making a scatterplot matrix using lattice and plotting the correlation coefficients of 12 variables in the upper half of the panel. I would also like to add the p values beneath the correlation coeffiecients or stars indicating their level of significance. Here is my R code. How can I achieve this? Many thanks in advance!
Here is a sample of my data
d.corr1 = structure(list(maxt1.res = c(-0.944678376630112, 0.324463929632583,
-1.18820341118942, -0.656600399095673, 0.332432965913295, 0.696656683837386
), maxt2.res = c(1.81878373188327, -0.437581385609662, 0.305933316224282,
-3.20946216261864, 0.629812177862245, -1.49044366233353), maxt3.res = c(-1.21422295698813,
-1.31516252550763, 0.570370111383564, 1.73177495368256, 2.18742200139099,
0.413531254505875), mint1.res = c(0.783488332204165, 0.35387082927864,
-0.528584845400234, 0.772682308165534, 0.421127289975828, 1.06059010003109
), mint2.res = c(0.262876147753049, 0.588802881606123, 0.745673830291112,
-1.22383100619312, -1.01594162784602, -0.135018034667641), mint3.res = c(0.283732674541107,
-0.406567031719476, 0.390198644741853, 0.860359703924238, 1.27865614582901,
0.346477970454206), sr1.res = c(1.7258974480523, -1.71718783477085,
3.98573602228491, -4.42153098079411, 0.602511156003456, -3.07683756735513
), sr2.res = c(9.98631829246284, -6.91757809846195, 0.418977023594041,
-6.10811634134865, 14.6495418067316, 2.44365146778955), sr3.res = c(-3.8809447886743,
2.35230122374257, 2.8673756880306, 7.1449786041902, 2.07480997224678,
4.93316979213985), rain1.res = c(0.112986181584307, 0.0445969189874017,
-0.446757191502526, 1.76152475011467, -0.395540856161192, -0.175756810329735
), rain2.res = c(-0.645121126413379, 1.74415111794381, -0.122876137090066,
1.68048850848576, -0.570490345329031, 0.00308540146622738), rain3.res = c(-0.202762644577954,
0.0528174267822909, -0.0616752465852931, -0.167769364680304,
-0.152822027502996, -0.139253335052929)), .Names = c("maxt1.res",
"maxt2.res", "maxt3.res", "mint1.res", "mint2.res", "mint3.res",
"sr1.res", "sr2.res", "sr3.res", "rain1.res", "rain2.res", "rain3.res"
), row.names = c(NA, 6L), class = "data.frame")
attach(d.corr1)
library(lattice)
library(RColorBrewer)
splom(~d.corr1[seq(1:12)], lower.panel = panel.splom,
upper.panel = function(x, y, ...) {
panel.fill(col = brewer.pal(9, "RdBu")[ round(cor(x, y) * 4 + 5)])
cpl <- current.panel.limits()
panel.text(mean(cpl$xlim), mean(cpl$ylim), round(cor(x, y),2), font=2)
},
scales = list(x = list( draw = TRUE, cex=0.1)), type = c("g", "p", "smooth"),layout = c(1, 1), pscales=0, pch=".",
main="correlation between the weather variables after removing district F.E and yearly trends")
dev.off()
detach(d.corr1)
Another option is to use panel.text
twice , with different adj
parameter.
For example :
splom(~d.corr1[seq(1:12)], lower.panel = panel.splom,
upper.panel = function(x, y, ...) {
panel.fill(col = brewer.pal(9, "RdBu")[ round(cor(x, y) * 4 + 5)])
cpl <- current.panel.limits()
## translate upward
panel.text(mean(cpl$xlim), mean(cpl$ylim), round(cor(x, y),2), font=2,
adj=c(0.5,-0.6))
## translate downward
panel.text(mean(cpl$xlim), mean(cpl$ylim), round( cor.test(x,y)$p.value, 2), font=1,
adj=c(0.5,0.6),col='blue')
},
Base graphics solution for your question is given below.
panel.cor <- function(x, y, digits = 2, cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
# correlation coefficient
r <- cor(x, y)
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste("r= ", txt, sep = "")
text(0.5, 0.6, txt)
# p-value calculation
p <- cor.test(x, y)$p.value
txt2 <- format(c(p, 0.123456789), digits = digits)[1]
txt2 <- paste("p= ", txt2, sep = "")
if(p<0.01) txt2 <- paste("p= ", "<0.01", sep = "")
text(0.5, 0.4, txt2)
}
pairs(iris, upper.panel = panel.cor)
I made this by modifying example provide for `pairs' function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With