Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R levelplot remove outer border (adjust plot border)

I'm creating a correlation heatmap in R with levelplot (lattice). I'd like borders between the boxes, but not along the outside since it interferes with the plot border. How can I remove the outer borders from the boxes?

Here is my code:

levelplot(matrix, border="black", 
          colorkey=list(height=.25, space="right", at=seq(-1, 1, .25), cuts=7), 
          scales=list(y=(list(cex=1)), tck = c(1,0), x=list(cex=1, rot=90)),
          main="Leaf Correlations", xlab="", ylab="", 
          col.regions=scalebluered)

and here is what it looks like.. I don't like the double lines on the edges..

enter image description here

EDIT: here is a reproducible example:

data(mtcars)
cars.matrix <- as.matrix(mtcars[c(2:8)])
cars.corr <- cor(cars.matrix)
levelplot(cars.corr, border="black", colorkey=list(height=.25, space="right", 
          at=seq(-1, 1, .25), cuts=7), 
          scales=list(y=(list(cex=1)), tck = c(1,0), x=list(cex=1, rot=90)), 
          xlab="", ylab="")
like image 714
Paul Avatar asked Nov 01 '13 17:11

Paul


1 Answers

OK, the fix for this is simple if a bit obscure.

Just use lattice.options() to reset the value of axis.padding used for factors, changing it from its default value of 0.6 (a little padding) to 0.5 (no padding), and you should be fine:

lattice.options(axis.padding=list(factor=0.5))

## An example to show that this works
data(mtcars)
cars.matrix <- as.matrix(mtcars[c(2:8)])
cars.corr <- cor(cars.matrix)
levelplot(cars.corr, border="black", colorkey=list(height=.25, space="right", 
          at=seq(-1, 1, .25), cuts=7), 
          scales=list(y=(list(cex=1)), tck = c(1,0), x=list(cex=1, rot=90)), 
          xlab="", ylab="")

enter image description here

For possibly-useful-future-reference, I figured this out by taking a quick look at the code used by prepanel.default.levelplot(). (The various prepanel.*** functions are responsible, among other things, for determining the coordinates and minimal area that should be allocated to each panel so that the objects to be plotted into it will all fit nicely.)

head(prepanel.default.levelplot, 4)

1 function (x, y, subscripts, ...)                    
2 {                                                   
3     pad <- lattice.getOption("axis.padding")$numeric
4     if (length(subscripts) > 0) {  
like image 102
Josh O'Brien Avatar answered Sep 24 '22 04:09

Josh O'Brien