title: Scatter plot matrices using pairs() in R
body: <p>I'm new to R and working on some code that outputs a scatter plot matrix. The data frame is in the following format:</p>
  
  <pre><code>A B C D
  2 3 0 5
  8 9 5 4
  0 0 5 3
  7 0 0 0
  </code></pre>
  
  <p>My data sets can run into the 100-1000s of rows and 10-100s of columns, with a wide scale of values (hence log transforming my data).  </p>
  
  <p>This bit of code gives me some partial success in enhancing the basic plot (see embedded image):</p>
  
  <pre><code>panel.cor &lt;- function(x, y, digits = 2, prefix = "", cex.cor, ...)
  {
    usr &lt;- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1), xlog = FALSE, ylog = FALSE)
    r &lt;- abs(cor(x, y))
    txt &lt;- format(c(r, 0.123456789), digits = digits)[1]
    txt &lt;- paste(prefix, txt)
    if(missing(cex.cor)) cex.cor &lt;- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
  }
  
  # Add regression line to plots.
  
  my_line &lt;- function(x,y,...){
    points(x,y,...)
    LR &lt;- lm(log(x) ~ log(y), data = SP)
    abline(LR, col = "red", untf = TRUE)
  }
  
  # Plot scatter plot matrices.
  
  pairs(mydataframe, pch = 20, main = "test",
        cex = 0.125, cex.labels = 1,
        xlim = c(100, 1e9),
        ylim = c(100, 1e9),
        upper.panel = panel.cor,
        lower.panel = my_line,
        log = "xy")'
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/Y5son.png" rel="nofollow">example</a></p>
  
  <p>Problem 1 - instead of getting R^2 values in the upper panel, I get NAs instead.   How can I correct this?<br>
  Problem 2 - I'd like to remove the function for adjusting text size of R^2 value in proportion to correlation. I know it's in panel.cor but not sure which part will need removal or adjustment.</p>
  
  <p>Many thanks in advance</p>
  
  <p>EDIT: 08/06/2016<br>
  I have found a work around which also simplifies the code:  </p>
  
  <pre><code>panel.cor &lt;- function(x, y, digits = 2, cex.cor, ...)
  {
    usr &lt;- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    # correlation coefficient
    r &lt;- cor(x, y)
    txt &lt;- format(c(r, 0.123456789), digits = digits)[1]
    txt &lt;- paste("r= ", txt, sep = "")
    text(0.5, 0.6, txt)
  }
  
  # add regression line to plots.
  
  my_line &lt;- function(x,y,...)
  {
    points(x,y,...)
    LR &lt;- lm(x ~ y, data = SP)
    abline(LR, col = "red", untf = TRUE)
  }
  
  # Plot scatterplot matrices.
  
  pairs(SP, pch = 20, main = "test",
        cex = 0.125, cex.labels = 1,
        upper.panel = panel.cor,
        lower.panel = my_line)
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/LGC4M.png" rel="nofollow">example 2</a></p>
  
  <p>The issue appears to be missing values i.e. 0's. I change these to NA's initially so I can use a log scale. This in combination with log transformation leads to missing R^2 values in the upper panel.  </p>
  
  <p>Ideally I'd like to have a log scale. Is there a way i can do this without introducing the aformentioned issue?  </p>
  
  <p>Clarification - I'd like a log (xy) scale in the scatter plots (lower panel) and for x-axis in the histograms (diagonal panel). I've been playing about with it today but can't quite get it as i want. Perhaps i'm asking too much from pairs. Any help would be appreciated.</p>
  
  <p>Edit: 10/06/2016  </p>
  
  <p>Success!....well approximately 99% happy.  </p>
  
  <p>I have made changes - added histograms to diagonal panel and p-value to upper panel (the base code in "pairs()" for adding the histogram needed adjustment due to the log scale used on the x-axis). Please feel free to correct my descriptions if they're not accurate or correct:</p>
  
  <pre><code>library(lattice)
  DF &lt;- read.csv("File location", header = TRUE)
  DF.1 &lt;- DF+1 # Added small epsilon to data frame otherwise plot errors arise due to missing values.
  
  # Function to calculate R^2 &amp; p-value for upper panels in pairs() - scatterplot matrices.
  
  panel.cor &lt;- function(x, y, digits = 3, cex.cor, ...)
  {
    usr &lt;- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1), xlog = FALSE, ylog = FALSE) # xlog/ylog: ensures that R^2 and p-values display in upper panel.
    # Calculate correlation coefficient and add to diagonal plot.
    r &lt;- cor(x, y)
    txt &lt;- format(c(r, 0.123456789), digits = digits)[1]
    txt &lt;- paste("r= ", txt, sep = "")
    text(0.5, 0.7, txt, cex = 1.25) # First 2 arguments determine postion of R^2-value in upper panel cells.
  
    # Calculate P-value and add to diagonal plot.
    p &lt;- cor.test(x, y)$p.value
    txt2 &lt;- format(c(p, 0.123456789), digits = digits)[1]
    txt2 &lt;- paste("p= ", txt2, sep = "")
    if(p&lt;0.01) txt2 &lt;- paste("p= ", "&lt;0.01", sep = "")
    text(0.5, 0.3, txt2, cex = 1.25) # First 2 arguments determine postion of p-value in upper panel cells.
  }
  
  # Function to calculate frequency distribution and plot histogram in diagonal plot.
  
  panel.hist &lt;- function(x, ...)
  {
    usr &lt;- par("usr"); on.exit(par(usr))
    par(usr = c(0.5, 1.5, 0, 1.75), xlog = TRUE, ylog = FALSE) # xlog argument allows log x-axis when called in pairs.
    h &lt;- hist(log(x), plot = FALSE, breaks = 20)
    breaks &lt;- h$breaks; nB &lt;- length(breaks)
    y &lt;- h$counts; y &lt;- y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col = "cyan")
  }
  
  # add regression line to plots.
  
  my_line &lt;- function(x,y, ...)
  {
    points(x,y,...)
    LR &lt;- lm(log(x) ~ log(y), data = DF.1)
    abline(LR, col = "red", untf = TRUE)
  }
  
  # Plot scatterplot matrices.
  
  pairs(DF.1, pch = 20, main = "Chart Title",
        cex = 0.75, cex.labels = 1.5, label.pos = 0.0001,
        upper.panel = panel.cor,
        lower.panel = my_line,
        diag.panel = panel.hist,
        log = ("xy"),
        xlim = c(5, 1e9),
        ylim = c(5, 1e9))
  </code></pre>
  
  <p>The fly in the ointment:</p>
  
  <p>1 - the text labels in the diagonal panel only partially appear. I used a decreasing value for the "label.pos" argument in "pairs()" which moved the label down until they appeared. However, they won't move anymore no matter how much I decrease that value. I've tried to coerce the position from the histogram function, but that doesn't work. I hope someone can see what I'm missing. Thanks in advance...I've not had any responses yet:(</p>
  
  <p>PS: I tried to link 3rd image with my successful plot but I was foiled by my lack of reputation...groan.</p>
  
  <p>EDIT: 13/06/2016</p>
  
  <p>Solved! I feel a bit foolish. The fix for the positioning of the main title in the diagonal panel was super simple and I spent a long time trying much more complex ways to do this. The "label.pos" argument in pairs should be negative! I used a small value of -0.0675 which placed it near the top of the cell containing the histogram.</p>
  
  <p>I hope someone else finds this useful. I'll mark as solved but I'd appreciate any comments regarding my code commenting or if someone sees a way of making the code more efficient. Thanks Alex</p>