我正在使用晶格制作散点图矩阵,并在面板的上半部分绘制12个变量的相关系数。我还想在相关系数或星形下面添加p值,表明它们的显着性水平。这是我的R代码。我怎样才能做到这一点?非常感谢提前!
以下是我的数据样本
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)
答案 0 :(得分:9)
另一种选择是使用panel.text
两次,使用不同的adj
参数。
例如:
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')
},
答案 1 :(得分:4)
您的问题的基本图形解决方案如下所示。
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)
我通过修改`pairs'函数的示例提供了这个。
答案 2 :(得分:1)
鉴于您没有提供任何数据,我将假设您计划在图表外部进行这些计算。假设您的p值位于名为p_vals
的向量中。而不是round(cor(x, y),2)
作为文本的第三个参数,使用:
paste( round(cor(x, y),2), "\n", p_vals)
使用相同的策略,您可以使用数据在格子中完成所有操作:
paste( round(cor(x, y),2) ,"\n", round( cor.test(x,y)$p.value, 2) )