[R] normal curve on the diagonal of pairs

Castro, Salvador ft122310 at ohio.edu
Thu Nov 28 17:46:56 CET 2013


Hi
I am trying to add the normal curve on the diagonal of a matrix scatter plot. The code below works if I plot a single histogram with the normal curve but not placed on the diagonal of the matrix. Plots attached. Thank you for your time. 
-SC

This code doesn’t produce normal curves correctly:
# Matrix Scatter Plot for the test multicollinearity and linear relationship between each pair of dependent variables
# ********************************************************************************************************
# panel.smooth function is built in.
# panel.cor puts correlation in upper panels, size proportional to correlation
panel.cor <- function(x, y, digits=3, 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, col = "red", cex = cex.cor * r)
}

## put histograms on the diagonal
panel.hist <- function(x, ...)
{
  usr <- par("usr")
  on.exit(par(usr))
  par(usr = c(usr[1:2], 0, 1.5) )
  h <- hist(x, plot = F)
  breaks <- h$breaks
  nB <- length(breaks)
  y <- h$counts 
  y <- y/max(y)
  rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)

  # Add a Normal Curve
  xfit <- seq(min(x, na.rm = T), max(x, na.rm = T), length = 40) 
  yfit <- dnorm(xfit, mean = mean (x, na.rm = T), sd = sd(x, na.rm = T)) 
  yfit <- yfit*diff(h$mids[1:2])*length(x) 
  lines(xfit, yfit, col = "blue", lwd = 2)
}

quartz(title="", 8, 8)
pairs(~achieve + evaluation + aptitude + affect, data = components, 
      lower.panel = panel.smooth, upper.panel = panel.cor, diag.panel = panel.hist,
      main="Scatterplot Matrix with Correlations", na.action = stats::na.pass, pch = 21,
      bg = c("#1E90FF", "#ADFF2F")[unclass(components$passfail)])


However, this code produces the normal curve correctly:

quartz(title="Histograms")
layout(matrix(c(1,2,3,4),2,2)) #divide graph  
h <- hist(na.omit(components$affect), breaks = "Sturges", xlab="achievement", include.lowest = TRUE, 
             right = TRUE, density = NULL, angle = 45, border = NULL,
             main="Histogram with Normal Curve", col="red",
             axes = TRUE, plot = TRUE, labels = FALSE, warn.unused = TRUE) 

xfit <- seq(min(components$affect, na.rm = T), max(components$affect, na.rm = T), length=40) 
yfit<-dnorm(xfit, mean = mean (components$affect, na.rm = T),sd = sd(components$affect, na.rm = T)) 
yfit <- yfit*diff(h$mids[1:2])*length(components$affect) 
lines(xfit, yfit, col="blue", lwd=2)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rplot.pdf
Type: application/pdf
Size: 460885 bytes
Desc: Rplot.pdf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131128/d561701c/attachment.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rplot03.pdf
Type: application/pdf
Size: 4983 bytes
Desc: Rplot03.pdf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131128/d561701c/attachment-0001.pdf>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ATT00001.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131128/d561701c/attachment.txt>


More information about the R-help mailing list