문제

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="")
도움이 되었습니까?

해결책

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) {  

다른 팁

A bit of digging shows that a lot of the par commands may not make it to Lattice package graphics. For example, par(bty = 'n') won't work in this levelplot example.

Unlike base R graphs, lattice graphs are not effected by many of the options set in the par( ) function. To view the options that can be changed, look at help(xyplot). It is frequently easiest to set these options within the high level plotting functions ... you can write functions that modify the rendering of panels.

Try passing the axis color directly into the graphic ala the method suggested by Yangchen Lin here: R lattice 3d plot: ticks disappear when changing panel border thickness

axis.line = list(col='transparent')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top