title: Scatterplot matrix with logarithmic axes in R
body: <p>I am trying to create a scatterplot matrix from my dataset so that in the resulting matrix:</p>
  
  <ul>
  <li>I have two different groups based on 
  
  <ul>
  <li>Quarter of the year (distinguished as the colours of points)</li>
  <li>Day type (shape of points indicating, is it weekend or casual day between Monday and Friday)</li>
  </ul></li>
  <li>Logarithmic-scaled x and y axes.</li>
  <li>Values on axis tick labels are not logarithmic i.e. values should be shown on axes as integers between 0 to 350, not their log10 counterparts.</li>
  <li>Upper panel has correlation values for each quarter.</li>
  </ul>
  
  <p>So far I've tried using functions:</p>
  
  <ol>
  <li>pairs()</li>
  <li>ggpairs() [from GGally package]</li>
  <li>scatterplotMatrix()</li>
  <li>splom()</li>
  </ol>
  
  <p>But I haven't been able to get decent results with these packages, and every time it seems that one or more of my requirements are missing. </p>
  
  <ul>
  <li>With pairs(), I'm able to create the scatterplot matrix, but the parameter <em>log="xy"</em> somehow removes the variable names from the diagonal of the resulting matrix.</li>
  <li>ggpairs() doesn't support logarithmic scales directly, but I created a function that goes through the scatterplot matrix's diagonal and lower plane based on <a href="https://stackoverflow.com/questions/6598174/what-package-is-to-be-installed-in-r-for-scatter-plots-with-logarithmic-binning">this</a> answer. Though the logarithmic scaling works on lower plane, it messes up the variable labels and value ticks.</li>
  </ul>
  
  <p>Function is created and used as follows:</p>
  
  <pre><code>ggpairs_logarithmize &lt;- function(a) { # parameter a is a ggpairs sp-matrix
          max_limit &lt;- sqrt(length(a$plots))
          for(row in 1:max_limit) { # index 1 is used to go through the diagonal also
                  for(col in j:max_limit) {
                          subsp &lt;- getPlot(a,row,col)
                          subspnew &lt;- subsp + scale_y_log10() + scale_x_log10()
                          subspnew$type &lt;- 'logcontinous'
                          subspnew$subType &lt;- 'logpoints'
                          a &lt;- putPlot(a,subspnew,row,col)
                  }
          }
          return(a)
  }
  scatplot &lt;- ggpairs(...)
  scatplot_log10 &lt;- ggpairs_logarithmize(scatplot)
  scatplot_log10
  </code></pre>
  
  <ul>
  <li>scatterplotMatrix() didn't seem to support two groupings. I was able to do this separately for season and day type though, but I need both groups in the same plot.</li>
  <li>splom() somehow labels the axis tick values also to logarithmic values, and these should be kept as they are (between integers 0 and 350).</li>
  </ul>
  
  <p>Are there any simple solutions available to create a scatterplot matrix with logarithmic axes with the requirements I have?</p>
  
  <p><strong>EDIT (13.7.2012):</strong> Example data and output were asked. Here's some code snippets to produce a demo dataset:</p>
  
  <p><em>Declare necessary functions</em></p>
  
  <pre><code>logarithmize &lt;- function(a)
  {
          max_limit &lt;- sqrt(length(a$plots))
          for(j in 1:max_limit) {
                  for(i in j:max_limit) {
                          subsp &lt;- getPlot(a,i,j)
                          subspnew &lt;- subsp + scale_y_log10() + scale_x_log10()
                          subspnew$type &lt;- 'logcontinous'
                          subspnew$subType &lt;- 'logpoints'
                          a &lt;- putPlot(a,subspnew,i,j)
                  }
          }
          return(a)
  }
  
  add_quarters &lt;- function(a,datecol,targetcol) {
      for(i in 1:nrow(a)) {
          month &lt;- 1+as.POSIXlt(as.Date(a[i,datecol]))$mon
          if ( month &lt;= 3 ) { a[i,targetcol] &lt;- "Q1" }
          else if (month &lt;= 6 &amp;&amp; month &gt; 3) { a[i,targetcol] &lt;- "Q2" }
          else if ( month &lt;= 9 &amp;&amp; month &gt; 6 ) { a[i,targetcol] &lt;- "Q3" }
          else if ( month &gt; 9 ) { a[i,targetcol] &lt;- "Q4" }
      }
      return(a)
  }
  </code></pre>
  
  <p><em>Create dataset:</em></p>
  
  <pre><code>days &lt;- seq.Date(as.Date("2010-01-01"),as.Date("2012-06-06"),"day")
  bananas &lt;- sample(1:350,length(days), replace=T)
  apples &lt;- sample(1:350,length(days), replace=T)
  oranges &lt;- sample(1:350,length(days), replace=T)
  weekdays &lt;- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
  fruitsales &lt;- data.frame(Date=days,Dayofweek=rep(weekdays,length.out=length(days)),Bananas=bananas,Apples=apples,Oranges=oranges)
  fruitsales[5:6,"Quarter"] &lt;- NA
  fruitsales[6:7,"Daytype"] &lt;- NA
  fruitsales$Daytype &lt;- fruitsales$Dayofweek
  levels(fruitsales$Daytype) # Confirm the day type levels before assigning new levels
  levels(fruitsales$Daytype) &lt;- c("Casual","Casual","Weekend","Weekend","Casual","Casual","Casual
  ")
  fruitsales &lt;- add_quarters(fruitsales,1,6)
  </code></pre>
  
  <p><em>Excecute (NOTE! Windows/Mac users, change x11() according to what OS you have)</em></p>
  
  <pre><code># install.packages("GGally")
  require(GGally)
  x11(); ggpairs(fruitsales,columns=3:5,colour="Quarter",shape="Daytype")
  x11(); logarithmize(ggpairs(fruitsales,columns=3:5,colour="Quarter",shape="Daytype"))
  </code></pre>