As a follow-up on Adding a background image to a levelplot I'm exporting the levelplot
to a png file, and I'd like to know how I can access the width and height of that trellis object to pass it as parameters to png
.
library("png")
library("lattice")
library("latticeExtra")
MyFunction <- function(x,y){
return(
dnorm(sqrt(x^2+y^2)))}
meshstep <- 0.2
x<- seq(-30,30,meshstep)
y <-seq(-20,20,meshstep)
image <- readPNG(system.file("img", "Rlogo.png", package="png"))
grid <- expand.grid(x=x, y=y)
grid$z<- MyFunction(grid$x,grid$y)
MyPalette <- colorRampPalette(c('white','yellow', 'red'))
levels <- 10
p<- levelplot(z~x*y, grid, cuts = levels, xlab="",
ylab="",
colorkey = TRUE, region = TRUE,col.regions=MyPalette(levels+1),
alpha.regions=0.5)
+ layer(grid.raster(as.raster(image)), under=TRUE)
trellis.device(device="png", filename="MWE.png")
print(p)
dev.off()
For the resulting png file, the width and height are 480px, since this is the default. I'd like to write trellis.device(device="png", filename="MWE.png", width=width(p), height=height(p))
or something similar to keep the x and y axes evenly spaced.