R: Levelplot gives extraneous whitespace when range of row.values and column.values is small

1.3k views Asked by At

When using the levelplot (from lattice package) in R, I noticed that there is extra whitespace around the edges of the graph if the range of column.values and row.values is small (e.g range is less than 1). This problem disappears if the range of column.values and row.values is bigger (e.g. range is 10).

Here's an example that illustrates the problem:

library(gplots) # for colorpanel()
library(lattice) # for levelplot()

#generate data
myData = runif(100) #generate vector of 100 random numbers
myMatrix=matrix(myData, sqrt(length(myData)), sqrt(length(myData)))

#generate vectors that will be used for col.values and row.values
labelIncrement= 1/10
labelLow = -0.5
labelHigh = 0.5 - labelIncrement

dataForRow.values <- c(seq(labelLow, labelHigh, labelIncrement))
dataForColumn.values <- c(seq(labelLow, labelHigh, labelIncrement))

myLevelsArbitrary <- c(0, 0.2, 0.4, 0.6, 0.8, 1.0)
myColors = topo.colors(length(myLevelsArbitrary)+1)

myLevelPlot = levelplot(myMatrix,
    at=myLevelsArbitrary,
    col.regions=myColors,
    column.values=dataForColumn.values,
    row.values=dataForRow.values,
    main="Title", 
    xlab="Distance from center (cm)", 
    ylab="Distance from center (cm)",
    colorkey=list(width=3))

plot(myLevelPlot) 

This image is myLevelPlot.

Now, let's tweak the above code to have a bigger range for row.values and column.values:

labelIncrement= 1
labelLow = -5
labelHigh = 5 - labelIncrement

This myLevelPlot image was generated by re-running the code with the bigger range of row.values and column.values. Notice that the extraneous whitespace does not occur in the following image.

So, how do I avoid having this extraneous whitespace when I have a small range for row.values and column.values?

1

There are 1 answers

2
Pierre Lapointe On BEST ANSWER

That's a tough one. This works: scales = "sliced"

myLevelPlot = levelplot(myMatrix,
    at=myLevelsArbitrary,
    col.regions=myColors,
    column.values=dataForColumn.values,
    row.values=dataForRow.values,
    main="Title",
    xlab="Distance from center (cm)",
    ylab="Distance from center (cm)",
    scales = "sliced",
    colorkey=list(width=3)
    )
plot(myLevelPlot)

I don't like the two warnings, but sometimes warnings can be ignored...