[R] scatter plot matrix with multiple trend lines and p values

tianshu twboiteau at gmail.com
Sat Feb 28 18:07:30 CET 2015


I am trying to create a scatter plot matrix in which the lower panels
contain
scatter plots colored by group with trend lines by group, and the upper
panels contain r and p values. Using pairs() I was able to get close (ie,
scatter plot matrix on bottom with one trend line and correlation
coefficients and p values in the upper panel) but was unable to add two
lines. Using spm() I was able to get the multiple trend lines, but not the
upper
panel statistics. Does anyone know of a way to accomplish this?

My code using pairs:

pairs(~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data = iris,
bg=c("red","blue","green")[iris$Species],
pch = 22,
lower.panel  =my_line <- function(x,y,...){
    points(x,y,...)
    abline(a = lm(y ~ x)$coefficients[1] , b = lm(y ~ x)$coefficients[2] ,
...)
},
upper.panel = 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)
})




--
View this message in context: http://r.789695.n4.nabble.com/scatter-plot-matrix-with-multiple-trend-lines-and-p-values-tp4703996.html
Sent from the R help mailing list archive at Nabble.com.
	[[alternative HTML version deleted]]



More information about the R-help mailing list