2

I'd like to build a correlogram using R function pairs(). In the lower panel I want to represent high density plots generated with hexbin(). However, I have problems to locate the hexbin plots in proper position of lower panels.

This is the function to generate each hexbin plot:

density_pts<- function(x,y,...){
  bin <- hexbin(x, y, xbins=50)
  plot(bin, main="", xlab="", ylab="",legend=F,newpage = FALSE)
}

The problem is that each plot generated by density_pts does not fit lower panel boxes. Which parameters have I to use to correctly locate them?

Here and example of the problem:

data<-data.frame(A = sample(1:1000),B = sample(1:1000),C = sample(1:1000),D = sample(1:1000))

density_pts<- function(x,y,...){
  bin <- hexbin(x, y, xbins=50)
  plot(bin, main="", xlab="", ylab="",legend=F)
}

# default scatterplot
pairs(data,upper.panel = NULL)

# custom high density plot ::: problem :::
pairs(data,lower.panel=density_pts,upper.panel = NULL)

Many thanks in advance!

  • Please include a [small but complete example reproducing your problem](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – digEmAll Jul 26 '14 at 12:34
  • The plot method of hexbin object is dispatched through the S4 system and produces a grid plot object. The pairs function is a base graphics function so it's not surprising that you are having problems. – IRTFM Jul 26 '14 at 18:09

1 Answers1

0

You can just use hexplom() function, which is the hexbin version of splom():

density_pts<- function(x,y,...){
  bin <- hexbin(x, y, xbins=50)
  plot(bin, main="", xlab="", ylab="",legend=F,newpage = FALSE)
}
data<-data.frame(A = sample(1:1000),B = sample(1:1000),C = sample(1:1000),D = sample(1:1000))

density_pts<- function(x,y,...){
  bin <- hexbin(x, y, xbins=50)
  plot(bin, main="", xlab="", ylab="",legend=F)
}

# default scatterplot
pairs(data,upper.panel = NULL)

# custom high density plot ::: problem solved :::
hexplom(data, upper.panel = NULL)

Resulting in this:

enter image description here

Gimelist
  • 791
  • 1
  • 10
  • 25