将背景图像添加到水平图

时间:2013-11-18 17:51:19

标签: r lattice levelplot

我想制作一张有levelplot的背景图片。以下代码提示错误消息Error in rasterImage(image, x[1], y[1], x[length(x)], y[length(y)]) : plot.new has not been called yet - 显然rasterImage无法将打印的levelplot对象识别为绘图。什么是适当的方法,而不是rasterImage

library("png")
library("lattice")
library("latticeExtra")

MyFunction <- function(x,y){
  return(
    dnorm(sqrt(x^2+y^2))
    )
}

meshstep <- 0.2
x<- seq(-20,20,meshstep)
y <-seq(-20,20,meshstep)

image <- readPNG("imagepath\\image.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.3)


plot(p)
rasterImage(image, x[1], y[1],x[length(x)],y[length(y)])

1 个答案:

答案 0 :(得分:3)

使用+.trellislayer结合grid.raster

library(grid)
library(latticeExtra)
library(png)

image <- readPNG(system.file("img", "Rlogo.png", package="png"))

p + layer(grid.raster(as.raster(image)), under=TRUE)