I am using codes from this website to plot scatter and correlation matrix: http://www.sthda.com/english/wiki/scatter-plot-matrices-r-base-graphs
panel.cor <- function(x, y){
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- round(cor(x, y), digits=2)
txt <- paste0("R = ", r)
cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
# Customize upper panel
upper.panel<-function(x, y){
points(x,y, pch = 19, col = my_cols[iris$Species])
}
# Create the plots
pairs(iris[,1:4],
lower.panel = panel.cor,
upper.panel = upper.panel)
How to calculate p value and add stars next to the correlation coefficient? I find "PerformanceAnalytics" package can do this but I want to use smoothScatter function in the upper panel scatterplot. I don't know how to use smoothScatter function in PerformanceAnalytics. Currently my upper panel function looks like this. It is copied from another website
pairs(df, lower.panel = panel.cor,
upper.panel = function(...) smoothScatter(..., nrpoints = 0, add = TRUE), gap = 0.2)
r2 <- cor.test(x, y)$p.value
r3 <- symnum(r2, cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***","**","*",""))
can be used to obtain p value.
so the whole panel.cor function looks like this:
panel.cor <- function(x, y){
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- round(cor(x, y), digits=2)
r2 <- cor.test(x, y)$p.value
r3 <- symnum(r2, cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***","**","*",""))
txt <- paste(r, r3, sep = " ")
cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor*r)
}