本文介绍了将背景图像添加到水平图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有背景图像的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的合适方法?

I'd like to make a levelplot which has a background image. The following code promts the error message Error in rasterImage(image, x[1], y[1], x[length(x)], y[length(y)]) : plot.new has not been called yet - apparently rasterImage does not recognize printed levelplot object as a plot. What's the appropriate method instead of 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)])

推荐答案

结合使用+.trellislayergrid.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)

这篇关于将背景图像添加到水平图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 22:20